dasel

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2020 License: MIT Imports: 6 Imported by: 22

README

dasel

Go Report Card PkgGoDev Test codecov Build

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

Installation

You can import dasel as a package and use it in your applications, or you can use a pre-built binary to modify files from the command line.

Import

As with any other go package, just use go get.

go get github.com/tomwright/dasel
Command line

You can go get the main package and go should automatically build and install dasel for you.

go get github.com/tomwright/dasel/cmd/dasel

Alternatively you can download a compiled executable from the latest release: This one liner should work for you - be sure to change the targeted release executable if needed. It currently targets dasel_linux_amd64.

curl -s https://api.github.com/repos/tomwright/dasel/releases/latest | grep browser_download_url | grep linux_amd64 | cut -d '"' -f 4 | wget -qi - && mv dasel_linux_amd64 dasel && chmod +x dasel

Usage

Select
$ dasel select -h

The following should select the image within a kubernetes deployment manifest.

$ dasel select -f deployment.yaml -s "spec.template.spec.containers.(name=auth).image"
tomwright/auth:v1.0.0

You can also pipe data into dasel, although you then need to tell dasel which parser to use with the -p flag.

$ cat deployment.yaml | dasel select -p yaml -s "spec.template.spec.containers.(name=auth).image"
tomwright/auth:v1.0.0
Put
$ dasel put -h

Basic usage is:

dasel put <string|int|bool|object> -f <file> -s "<selector>" <value>
Piping Data

You can pipe data both in and out of dasel.

$ echo "name: Tom" | ./dasel put string -p yaml -s "name" Jim
name: Jim

It's important to remember than if you are piping data you must provide a parser using the -p flag.

Input

Input is taken from stdin if you do not pass a file using the -f flag.

Output

The select commands will always output to stdout.

The default functionality for put commands is to edit the file in place, unless input is from stdin in which case it will be written to stdout.

You can choose a new output file by passing -o <filepath>. Alternatively passing -o stdout will result in the results being written to stdout.

Putting Objects

If putting an object, you can pass multiple arguments in the format of KEY=VALUE, each of which needs a related -t <string|int|bool> flag passed in the same order as the arguments. This tells dasel which data types to parse the values as.

$ dasel put object -f preferences.yaml -s "my.favourites" -t string -t int colour=red number=3

Results in the following:

my:
  favourites:
    colour: red
    number: 3
Kubernetes

The following should work on a kubernetes deployment manifest. While kubernetes isn't for everyone, it does give me some good example use-cases.

Change the image for a container named auth
$ dasel put string -f deployment.yaml -s "spec.template.spec.containers.(name=auth).image" "tomwright/x:v2.0.0"
Update replicas to 3
$ dasel put int -f deployment.yaml -s "spec.replicas" 3
Add a new env var
$ dasel put object -f deployment.yaml -s "spec.template.spec.containers.(name=auth).env.[]" -t string -t string name=MY_NEW_ENV_VAR value=MY_NEW_VALUE
Update an existing env var
$ dasel put string -f deployment.yaml -s "spec.template.spec.containers.(name=auth).env.(name=MY_NEW_ENV_VAR).value" NEW_VALUE

Supported data types

Dasel attempts to find the correct parser for the given file type, but if that fails you can choose which parser to use with the -p or --parser flag.

  • YAML - -p yaml
  • JSON - -p json

Selectors

Selectors are used to define a path through a set of data. This path is usually defined as a chain of nodes.

A selector is made up of different parts separated by a dot ., each part being used to identify the next node in the chain.

The following YAML data structure will be used as a reference in the following examples.

name: Tom
preferences:
  favouriteColour: red
colours:
- red
- green
- blue
colourCodes:
- name: red
  rgb: ff0000
- name: green
  rgb: 00ff00
- name: blue
  rgb: 0000ff
Property

Property selectors are used to reference a single property of an object.

Just use the property name as a string.

$ dasel select -f ./tests/assets/example.yaml -s "name"
Tom
  • name == Tom
Child Elements

Just separate the child element from the parent element using a .:

$ dasel select -f ./tests/assets/example.yaml -s "preferences.favouriteColour"
red
  • preferences.favouriteColour == red
Index

When you have a list, you can use square brackets to access a specific item in the list by its index.

$ dasel select -f ./tests/assets/example.yaml -s "colours.[1]"
green
  • colours.[0] == red
  • colours.[1] == green
  • colours.[2] == blue
Next Available Index

Next available index selector is used when adding to a list of items. It allows you to append to a list.

  • colours.[]
Dynamic

Dynamic selectors are used with lists when you don't know the index of the item, but instead know the value of a property of an object within the list.

Look ups are defined in brackets. You can use multiple dynamic selectors within the same part to perform multiple checks.

$ dasel select -f ./tests/assets/example.yaml -s "colourCodes.(name=red).rgb"
ff0000

$ dasel select -f ./tests/assets/example.yaml -s "colourCodes.(name=blue)(rgb=0000ff)"
map[name:blue rgb:0000ff]
  • colourCodes.(name=red).rgb == ff0000
  • colourCodes.(name=green).rgb == 00ff00
  • colourCodes.(name=blue).rgb == 0000ff
  • colourCodes.(name=blue)(rgb=0000ff).rgb == 0000ff

If you want to dynamically target a value in a list when it isn't a list of objects, just define the dynamic selector with (value=<some_value>) instead.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrMissingPreviousNode = errors.New("missing previous node")

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

Functions

This section is empty.

Types

type Condition

type Condition interface {
	Check(other reflect.Value) (bool, error)
}

Condition defines a Check we can use within dynamic selectors.

type EqualCondition

type EqualCondition struct {
	// Key is the key of the value to check against.
	Key string
	// Value is the value we are looking for.
	Value string
}

EqualCondition lets you check for an exact match.

func (EqualCondition) Check

func (c EqualCondition) Check(other reflect.Value) (bool, error)

Check checks to see if other contains the required key value pair.

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.

type Node

type Node struct {
	// Previous is the previous node in the chain.
	Previous *Node `json:"-"`
	// Next is the next node in the chain.
	Next *Node `json:"next,omitempty"`

	// Value is the value of the current node.
	Value reflect.Value `json:"value"`
	// Selector is the selector for the current node.
	Selector Selector `json:"selector"`
	// contains filtered or unexported fields
}

Node represents a single node in the chain of nodes for a selector.

func New

func New(value interface{}) *Node

New returns a new root note with the given value.

func (*Node) InterfaceValue added in v0.0.3

func (n *Node) InterfaceValue() interface{}

InterfaceValue returns the value stored within the node as an interface{}.

func (*Node) Put

func (n *Node) Put(selector string, newValue interface{}) error

Put finds the node using the given selector and updates it's value. It then attempts to propagate the value back up the chain to the root element.

func (*Node) Query

func (n *Node) Query(selector string) (*Node, error)

Query uses the given selector to query the current node and return the result.

type Selector

type Selector struct {
	// Raw is the full selector.
	Raw string `json:"raw"`
	// Current is the selector to be used with the current node.
	Current string `json:"current"`
	// Remaining is the remaining parts of the Raw selector.
	Remaining string `json:"remaining"`
	// Type is the type of the selector.
	Type string `json:"type"`
	// Property is the name of the property this selector targets, if applicable.
	Property string `json:"property,omitempty"`
	// Index is the index to use if applicable.
	Index int `json:"index,omitempty"`
	// Conditions contains a set of conditions to optionally match a target.
	Conditions []Condition `json:"conditions,omitempty"`
}

Selector represents the selector for a node.

func ParseSelector

func ParseSelector(selector string) (Selector, error)

ParseSelector parses the given selector string and returns a 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.

type UnhandledCheckType added in v0.0.3

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 added in v0.0.3

func (e UnhandledCheckType) Error() string

Error returns the error message.

type UnknownComparisonOperatorErr

type UnknownComparisonOperatorErr struct {
	Operator string
}

UnknownComparisonOperatorErr is returned when

func (UnknownComparisonOperatorErr) Error

Error returns the error message.

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.

type UnsupportedTypeForSelector

type UnsupportedTypeForSelector struct {
	Selector Selector
	Value    interface{}
}

UnsupportedTypeForSelector is returned when a selector attempts to handle a data type it can't handle.

func (UnsupportedTypeForSelector) Error

Error returns the error message.

type ValueNotFound

type ValueNotFound struct {
	Selector string
	Node     *Node
}

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

func (ValueNotFound) Error

func (e ValueNotFound) Error() string

Error returns the error message.

Directories

Path Synopsis
cmd
dasel command

Jump to

Keyboard shortcuts

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