dasel

package module
v2.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 14, 2024 License: MIT Imports: 9 Imported by: 1

README

dasel

Gitbook Go Report Card PkgGoDev Test Build codecov Mentioned in Awesome Go GitHub All Releases Downloads GitHub License GitHub tag (latest by date) Homebrew tag (latest by date)

Dasel (short for data-selector) allows you to query and modify data structures using selector strings.

Comparable to jq / yq, but supports JSON, YAML, TOML, XML and CSV with zero runtime dependencies.

One tool to rule them all

Say good bye to learning new tools just to work with a different data format.

Dasel uses a standard selector syntax no matter the data format. This means that once you learn how to use dasel you immediately have the ability to query/modify any of the supported data types without any additional tools or effort.

Update Kubernetes Manifest

Table of contents

Quickstart

Dasel is available on homebrew, ASDF, scoop, docker, Nix or as compiled binaries from the latest release.

brew install dasel

You can also install a development version with:

go install github.com/tomwright/dasel/v2/cmd/dasel@master

For more information see the installation documentation.

Select
echo '{"name": "Tom"}' | dasel -r json 'name'
"Tom"

See select documentation.

Convert json to yaml
echo '{"name": "Tom"}' | dasel -r json -w yaml
name: Tom

See select documentation.

Put
echo '{"name": "Tom"}' | dasel put -r json -t string -v 'contact@tomwright.me' 'email'
{
  "email": "contact@tomwright.me",
  "name": "Tom"
}

See put documentation.

Delete
echo '{
  "email": "contact@tomwright.me",
  "name": "Tom"
}' | dasel delete -r json '.email'
{
  "name": "Tom"
}

See delete documentation.

Completion

If you want to use completion from the terminal you can do the following (using zsh in this example):

Add the following to ~/.zshrc and reload your terminal.

export fpath=(~/zsh/site-functions $fpath)
mkdir -p ~/zsh/site-functions
dasel completion zsh > ~/zsh/site-functions/_dasel
compinit

Pre-Commit

Add dasel hooks to .pre-commit-config.yaml file

- repo: https://github.com/TomWright/dasel
  rev: v1.25.1
  hooks:
    - id: dasel-validate

for a native execution of dasel, or use:

  • dasel-validate-docker pre-commit hook for executing dasel using the official Docker images
  • dasel-validate-bin pre-commit hook for executing dasel using the official binary

Issue vs Discussion

I have enabled discussions on this repository.

I am aware there may be some confusion when deciding where you should communicate when reporting issues, asking questions or raising feature requests so this section aims to help us align on that.

Please raise an issue if:

  • You find a bug.
  • You have a feature request and can clearly describe your request.

Please open a discussion if:

  • You have a question.
  • You're not sure how to achieve something with dasel.
  • You have an idea but don't quite know how you would like it to work.
  • You have achieved something cool with dasel and want to show it off.
  • Anything else!

Features

Documentation

The official dasel docs can be found at daseldocs.tomwright.me.

Playground

You can test out dasel commands using the playground.

Source code for the playground can be found at github.com/TomWright/daselplayground.

Benchmarks

In my tests dasel has been up to 3x faster than jq and 15x faster than yq.

See the benchmark directory.

Stargazers over time

Stargazers over time

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AllFunc = BasicFunction{
	// contains filtered or unexported fields
}
View Source
var AndFunc = BasicFunction{
	// contains filtered or unexported fields
}
View Source
var AppendFunc = BasicFunction{
	// contains filtered or unexported fields
}
View Source
var CountFunc = BasicFunction{
	// contains filtered or unexported fields
}
View Source
var EqualFunc = BasicFunction{
	// contains filtered or unexported fields
}
View Source
var ErrMissingPreviousNode = errors.New("missing previous node")

ErrMissingPreviousNode is returned when findValue doesn't have access to the previous node.

View Source
var FilterFunc = BasicFunction{
	// contains filtered or unexported fields
}
View Source
var FilterOrFunc = BasicFunction{
	// contains filtered or unexported fields
}
View Source
var FirstFunc = BasicFunction{
	// contains filtered or unexported fields
}
View Source
var IndexFunc = BasicFunction{
	// contains filtered or unexported fields
}
View Source
var JoinFunc = BasicFunction{
	// contains filtered or unexported fields
}
View Source
var KeyFunc = BasicFunction{
	// contains filtered or unexported fields
}
View Source
var KeysFunc = BasicFunction{
	// contains filtered or unexported fields
}
View Source
var LastFunc = BasicFunction{
	// contains filtered or unexported fields
}
View Source
var LenFunc = BasicFunction{
	// contains filtered or unexported fields
}
View Source
var LessThanFunc = BasicFunction{
	// contains filtered or unexported fields
}
View Source
var MapOfFunc = BasicFunction{
	// contains filtered or unexported fields
}
View Source
var MergeFunc = BasicFunction{
	// contains filtered or unexported fields
}
View Source
var MetadataFunc = BasicFunction{
	// contains filtered or unexported fields
}
View Source
var MoreThanFunc = BasicFunction{
	// contains filtered or unexported fields
}
View Source
var NotFunc = BasicFunction{
	// contains filtered or unexported fields
}
View Source
var NullFunc = BasicFunction{
	// contains filtered or unexported fields
}
View Source
var OrDefaultFunc = BasicFunction{
	// contains filtered or unexported fields
}
View Source
var OrFunc = BasicFunction{
	// contains filtered or unexported fields
}
View Source
var ParentFunc = BasicFunction{
	// contains filtered or unexported fields
}
View Source
var PropertyFunc = BasicFunction{
	// contains filtered or unexported fields
}
View Source
var StringFunc = BasicFunction{
	// contains filtered or unexported fields
}
View Source
var ThisFunc = BasicFunction{
	// contains filtered or unexported fields
}
View Source
var TypeFunc = BasicFunction{
	// contains filtered or unexported fields
}
View Source
var UninitialisedPlaceholder interface{} = "__dasel_not_found__"

Functions

func IsTruthy

func IsTruthy(value interface{}) bool

Types

type BasicFunction

type BasicFunction struct {
	// contains filtered or unexported fields
}

func (BasicFunction) AlternativeSelector

func (bf BasicFunction) AlternativeSelector(part string) *Selector

func (BasicFunction) Name

func (bf BasicFunction) Name() string

func (BasicFunction) Run

func (bf BasicFunction) Run(c *Context, s *Step, args []string) (Values, error)

type Context

type Context struct {
	// contains filtered or unexported fields
}

Context has scope over the entire query. Each individual function has its own step within the context. The context holds the entire data structure we're accessing/modifying.

func (*Context) CreateWhenMissing

func (c *Context) CreateWhenMissing() bool

CreateWhenMissing returns true if the internal createWhenMissing value is true.

func (*Context) Data

func (c *Context) Data() Value

Data returns the root element of the context.

func (*Context) Metadata

func (c *Context) Metadata(key string) interface{}

func (*Context) Next

func (c *Context) Next() (*Step, error)

Next returns the next Step, or nil if we have reached the final Selector.

func (*Context) Run

func (c *Context) Run() (Values, error)

Run calls Next repeatedly until no more steps are left. Returns the final Step.

func (*Context) Step

func (c *Context) Step(i int) *Step

Step returns the step at the given index.

func (*Context) WithCreateWhenMissing

func (c *Context) WithCreateWhenMissing(create bool) *Context

WithCreateWhenMissing updates c with the given create value. If this value is true, elements (such as properties) will be initialised instead of return not found errors.

func (*Context) WithMetadata

func (c *Context) WithMetadata(key string, value interface{}) *Context

func (*Context) WithSelector

func (c *Context) WithSelector(s string) *Context

WithSelector updates c with the given selector.

type ErrBadSelectorSyntax

type ErrBadSelectorSyntax struct {
	Part    string
	Message string
}

func (ErrBadSelectorSyntax) Error

func (e ErrBadSelectorSyntax) Error() string

func (ErrBadSelectorSyntax) Is

func (e ErrBadSelectorSyntax) Is(other error) bool

type ErrIndexNotFound

type ErrIndexNotFound struct {
	Index int
}

func (ErrIndexNotFound) Error

func (e ErrIndexNotFound) Error() string

func (ErrIndexNotFound) Is

func (e ErrIndexNotFound) Is(other error) bool

type ErrInvalidType added in v2.2.0

type ErrInvalidType struct {
	ExpectedTypes []string
	CurrentType   string
}

func (*ErrInvalidType) Error added in v2.2.0

func (e *ErrInvalidType) Error() string

func (*ErrInvalidType) Is added in v2.2.0

func (e *ErrInvalidType) Is(other error) bool

type ErrPropertyNotFound

type ErrPropertyNotFound struct {
	Property string
}

func (ErrPropertyNotFound) Error

func (e ErrPropertyNotFound) Error() string

func (ErrPropertyNotFound) Is

func (e ErrPropertyNotFound) Is(other error) bool

type ErrUnexpectedFunctionArgs

type ErrUnexpectedFunctionArgs struct {
	Function string
	Args     []string
	Message  string
}

func (ErrUnexpectedFunctionArgs) Error

func (ErrUnexpectedFunctionArgs) Is

func (e ErrUnexpectedFunctionArgs) Is(other error) bool

type ErrUnknownFunction

type ErrUnknownFunction struct {
	Function string
}

func (ErrUnknownFunction) Error

func (e ErrUnknownFunction) Error() string

func (ErrUnknownFunction) Is

func (e ErrUnknownFunction) Is(other error) bool

type Function

type Function interface {
	Name() string
	Run(c *Context, s *Step, args []string) (Values, error)
	AlternativeSelector(part string) *Selector
}

type FunctionCollection

type FunctionCollection struct {
	// contains filtered or unexported fields
}

func (*FunctionCollection) Add

func (fc *FunctionCollection) Add(fs ...Function)

func (*FunctionCollection) Get

func (fc *FunctionCollection) Get(name string) (SelectorFunc, error)

func (*FunctionCollection) GetAll

func (fc *FunctionCollection) GetAll() map[string]SelectorFunc

func (*FunctionCollection) ParseSelector

func (fc *FunctionCollection) ParseSelector(part string) *Selector

type InvalidIndexErr

type InvalidIndexErr struct {
	Index string
}

InvalidIndexErr is returned when a selector targets an index that does not exist.

func (InvalidIndexErr) Error

func (e InvalidIndexErr) Error() string

Error returns the error message.

func (InvalidIndexErr) Is

func (e InvalidIndexErr) Is(err error) bool

Is implements the errors interface, so the errors.Is() function can be used.

type Selector

type Selector struct {
	// contains filtered or unexported fields
}

type SelectorFunc

type SelectorFunc func(c *Context, step *Step, args []string) (Values, error)

SelectorFunc is a function that can be executed in a selector.

type SelectorResolver

type SelectorResolver interface {
	Original() string
	Next() (*Selector, error)
}

func NewSelectorResolver

func NewSelectorResolver(selector string, functions *FunctionCollection) SelectorResolver

type Step

type Step struct {
	// contains filtered or unexported fields
}

Step is a single step in the query. Each function call has its own step. Each value in the output is simply a pointer to the actual data point in the context data.

func (*Step) Index

func (s *Step) Index() int

func (*Step) Output

func (s *Step) Output() Values

func (*Step) Selector

func (s *Step) Selector() Selector

type UnexpectedPreviousNilValue

type UnexpectedPreviousNilValue struct {
	Selector string
}

UnexpectedPreviousNilValue is returned when the previous node contains a nil value.

func (UnexpectedPreviousNilValue) Error

Error returns the error message.

func (UnexpectedPreviousNilValue) Is

Is implements the errors interface, so the errors.Is() function can be used.

type UnhandledCheckType

type UnhandledCheckType struct {
	Value interface{}
}

UnhandledCheckType is returned when the a check doesn't know how to deal with the given type

func (UnhandledCheckType) Error

func (e UnhandledCheckType) Error() string

Error returns the error message.

func (UnhandledCheckType) Is

func (e UnhandledCheckType) Is(err error) bool

Is implements the errors interface, so the errors.Is() function can be used.

type UnknownComparisonOperatorErr

type UnknownComparisonOperatorErr struct {
	Operator string
}

UnknownComparisonOperatorErr is returned when

func (UnknownComparisonOperatorErr) Error

Error returns the error message.

func (UnknownComparisonOperatorErr) Is

Is implements the errors interface, so the errors.Is() function can be used.

type UnsupportedSelector

type UnsupportedSelector struct {
	Selector string
}

UnsupportedSelector is returned when a specific selector type is used in the wrong context.

func (UnsupportedSelector) Error

func (e UnsupportedSelector) Error() string

Error returns the error message.

func (UnsupportedSelector) Is

func (e UnsupportedSelector) Is(err error) bool

Is implements the errors interface, so the errors.Is() function can be used.

type Value

type Value struct {
	reflect.Value
	// contains filtered or unexported fields
}

Value is a wrapper around reflect.Value that adds some handy helper funcs.

func Delete

func Delete(root interface{}, selector string) (Value, error)

Delete resolves the given selector and deletes any found values. The root value may be changed in-place. If this is not desired you should copy the input value before passing it to Delete.

func Put

func Put(root interface{}, selector string, value interface{}) (Value, error)

Put resolves the given selector and writes the given value in their place. The root value may be changed in-place. If this is not desired you should copy the input value before passing it to Put.

func ValueOf

func ValueOf(value interface{}) Value

ValueOf wraps value in a Value.

func (Value) Append

func (v Value) Append() Value

Append appends an empty value to the end of the slice.

func (Value) Delete

func (v Value) Delete()

Delete deletes the current element. Depends on deleteFn since the implementation can differ depending on how the Value was initialised.

func (Value) FieldByName

func (v Value) FieldByName(name string) Value

FieldByName returns the struct field with the given name. It returns the zero Value if no field was found.

func (Value) FirstAddressable

func (v Value) FirstAddressable() reflect.Value

func (Value) Index

func (v Value) Index(i int) Value

Index returns v's i'th element. It panics if v's Kind is not Array, Slice, or String or i is out of range.

func (Value) Interface

func (v Value) Interface() interface{}

Interface returns the interface{} value of v.

func (Value) IsDencodingMap added in v2.3.0

func (v Value) IsDencodingMap() bool

func (Value) IsEmpty

func (v Value) IsEmpty() bool

IsEmpty returns true is v represents an empty reflect.Value.

func (Value) IsNil added in v2.7.0

func (v Value) IsNil() bool

func (Value) Kind

func (v Value) Kind() reflect.Kind

Kind returns the underlying type of v.

func (Value) Len

func (v Value) Len() int

Len returns v's length.

func (Value) MapIndex

func (v Value) MapIndex(key Value) Value

MapIndex returns the value associated with key in the map v. It returns the zero Value if no field was found.

func (Value) MapKeys

func (v Value) MapKeys() []Value

func (Value) Metadata

func (v Value) Metadata(key string) interface{}

Metadata returns the metadata with a key of key for v.

func (Value) NumField

func (v Value) NumField() int

NumField returns the number of fields in the struct v.

func (Value) Set

func (v Value) Set(value Value)

Set sets underlying value of v. Depends on setFn since the implementation can differ depending on how the Value was initialised.

func (Value) String added in v2.3.0

func (v Value) String() string

String returns the string v's underlying value, as a string.

func (Value) Type

func (v Value) Type() reflect.Type

func (Value) Unpack

func (v Value) Unpack(kinds ...reflect.Kind) reflect.Value

Unpack returns the underlying reflect.Value after resolving any pointers or interface types.

func (Value) WithMetadata

func (v Value) WithMetadata(key string, value interface{}) Value

WithMetadata sets the given value into the values metadata.

type ValueNotFound

type ValueNotFound struct {
	Selector      string
	PreviousValue reflect.Value
}

ValueNotFound is returned when a selector string cannot be fully resolved.

func (ValueNotFound) Error

func (e ValueNotFound) Error() string

Error returns the error message.

func (ValueNotFound) Is

func (e ValueNotFound) Is(err error) bool

Is implements the errors interface, so the errors.Is() function can be used.

type Values

type Values []Value

Values represents a list of Value's.

func Select

func Select(root interface{}, selector string) (Values, error)

Select resolves the given selector and returns the resulting values.

func (Values) Interfaces

func (v Values) Interfaces() []interface{}

Interfaces returns the interface values for the underlying values stored in v.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL