Skip to content

Commit

Permalink
Merge pull request #13 from TomWright/xml
Browse files Browse the repository at this point in the history
Support XML data format
  • Loading branch information
TomWright authored Oct 28, 2020
2 parents bd0f874 + b32aad2 commit a6a0372
Show file tree
Hide file tree
Showing 10 changed files with 436 additions and 249 deletions.
115 changes: 90 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

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

Comparable to [jq](https://github.com/stedolan/jq) / [yq](https://github.com/kislyuk/yq), but supports JSON, YAML and TOML with zero dependencies.
Comparable to [jq](https://github.com/stedolan/jq) / [yq](https://github.com/kislyuk/yq), but supports JSON, YAML, TOML and XML with zero runtime dependencies.

# Table of contents
* [Dasel](#dasel)
Expand All @@ -24,34 +24,35 @@ Comparable to [jq](https://github.com/stedolan/jq) / [yq](https://github.com/kis
* [jq to dasel](#jq-to-dasel)
* [yq to dasel](#yq-to-dasel)
* [Kubernetes](#kubernetes)
* [XML](#xml-examples)

### 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.

#### Command line
You can `go get` the `main` package and go should automatically build and install dasel for you.
```
```bash
go get github.com/tomwright/dasel/cmd/dasel
```

Alternatively you can download a compiled executable from the [latest release](https://github.com/TomWright/dasel/releases/latest).
##### Linux amd64
This one liner should work for you - be sure to change the targeted release executable if needed. It currently targets `dasel_linux_amd64`.
```
```bash
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
mv ./dasel /usr/local/bin/dasel
```

##### Mac OS amd64
You may have to `brew install wget` in order for this to work.
```
```bash
curl -s https://api.github.com/repos/tomwright/dasel/releases/latest | grep browser_download_url | grep macos_amd64 | cut -d '"' -f 4 | wget -qi - && mv dasel_macos_amd64 dasel && chmod +x dasel
mv ./dasel /usr/local/bin/dasel
```

#### Import
As with any other go package, just use `go get`.
```
```bash
go get github.com/tomwright/dasel
```

Expand Down Expand Up @@ -237,9 +238,37 @@ my:
## Supported file 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.
- JSON - `-p json`
- TOML - `-p toml`
- YAML - `-p yaml`
### JSON
```bash
-p json
```
Using [golang.org/pkg/encoding/json](https://golang.org/pkg/encoding/json/).
### TOML
```bash
-p toml
```
Using [github.com/pelletier/go-toml](https://github.com/pelletier/go-toml).
### YAML
```bash
-p yaml
```
Using [gopkg.in/yaml.v2](https://gopkg.in/yaml.v2).
### XML
```bash
-p xml
```
Using [github.com/clbanning/mxj](https://github.com/clbanning/mxj).
#### Arrays/Lists
Due to the way that XML is decoded dasel can only detect something as a list if there are at least 2 items.
If you try to use list selectors (dynamic, index, append) when there are less than 2 items in the list you will get an error.
There are no plans to introduce a workaround for this but if there is enough demand it may be worked on in the future.
## Selectors
Expand Down Expand Up @@ -323,7 +352,7 @@ The follow examples show a set of [jq](https://github.com/stedolan/jq) commands
#### Select a single value
```
```bash
echo '{"name": "Tom"}' | jq '.name'
"Tom"
Expand All @@ -333,7 +362,7 @@ Tom
#### Select a nested value
```
```bash
echo '{"user": {"name": "Tom", "age": 27}}' | jq '.user.age'
27
Expand All @@ -343,7 +372,7 @@ echo '{"user": {"name": "Tom", "age": 27}}' | dasel -p json '.user.age'
#### Select an array index
```
```bash
echo '[1, 2, 3]' | jq '.[1]'
2
Expand All @@ -353,7 +382,7 @@ echo '[1, 2, 3]' | dasel -p json '.[1]'
#### Append to an array of strings
```
```bash
echo '["a", "b", "c"]' | jq '. += ["d"]'
[
"a",
Expand All @@ -373,7 +402,7 @@ echo '["a", "b", "c"]' | dasel put string -p json -s '.[]' d
#### Update a string value
```
```bash
echo '["a", "b", "c"]' | jq '.[1] = "d"'
[
"a",
Expand All @@ -391,7 +420,7 @@ echo '["a", "b", "c"]' | dasel put string -p json -s '.[1]' d
#### Update an int value
```
```bash
echo '[1, 2, 3]' | jq '.[1] = 5'
[
1,
Expand All @@ -409,7 +438,7 @@ echo '[1, 2, 3]' | dasel put int -p json -s '.[1]' 5
#### Overwrite an object
```
```bash
echo '{"user": {"name": "Tom", "age": 27}}' | jq '.user = {"name": "Frank", "age": 25}'
{
"user": {
Expand All @@ -429,7 +458,7 @@ echo '{"user": {"name": "Tom", "age": 27}}' | dasel put object -p json -s '.user
#### Append to an array of objects
```
```bash
echo '{"users": [{"name": "Tom"}]}' | jq '.users += [{"name": "Frank"}]'
{
"users": [
Expand Down Expand Up @@ -461,7 +490,7 @@ The follow examples show a set of [yq](https://github.com/kislyuk/yq) commands a
#### Select a single value
```
```bash
echo 'name: Tom' | yq '.name'
"Tom"
Expand All @@ -471,7 +500,7 @@ Tom
#### Select a nested value
```
```bash
echo 'user:
name: Tom
age: 27' | yq '.user.age'
Expand All @@ -485,7 +514,7 @@ echo 'user:
#### Select an array index
```
```bash
echo '- 1
- 2
- 3' | yq '.[1]'
Expand All @@ -499,7 +528,7 @@ echo '- 1
#### Append to an array of strings
```
```bash
echo '- a
- b
- c' | yq --yaml-output '. += ["d"]'
Expand All @@ -520,7 +549,7 @@ echo '- a
#### Update a string value
```
```bash
echo '- a
- b
- c' | yq --yaml-output '.[1] = "d"'
Expand All @@ -538,7 +567,7 @@ echo '- a
#### Update an int value
```
```bash
echo '- 1
- 2
- 3' | yq --yaml-output '.[1] = 5'
Expand All @@ -556,7 +585,7 @@ echo '- 1
#### Overwrite an object
```
```bash
echo 'user:
name: Tom
age: 27' | yq --yaml-output '.user = {"name": "Frank", "age": 25}'
Expand All @@ -575,7 +604,7 @@ user:
#### Append to an array of objects
```
```bash
echo 'users:
- name: Tom' | yq --yaml-output '.users += [{"name": "Frank"}]'
users:
Expand Down Expand Up @@ -617,4 +646,40 @@ dasel put object -f deployment.yaml -s "spec.template.spec.containers.(name=auth
#### Update an existing env var
```bash
dasel put string -f deployment.yaml -s "spec.template.spec.containers.(name=auth).env.(name=MY_NEW_ENV_VAR).value" NEW_VALUE
```
```
### XML Examples
XML has some slight differences (such as attributes) that should be documented.
#### Query attributes
Decoded attributes are set as properties on the related object with a prefix of `-`.
```bash
echo '<data>
<users primary="true">
<name>Tom</name>
</users>
<users primary="false">
<name>Frank</name>
</users>
</data>' | go run cmd/dasel/main.go -p xml '.data.users[0].-primary'
true
```
#### Filtering on attributes
We can also filter on attributes since they are defined against the related object.
```bash
echo '<data>
<users primary="true">
<name>Tom</name>
</users>
<users primary="false">
<name>Frank</name>
</users>
</data>' | go run cmd/dasel/main.go -p xml '.data.users.(-primary=true).name'
Tom
```
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/tomwright/dasel
go 1.15

require (
github.com/clbanning/mxj/v2 v2.3.2
github.com/pelletier/go-toml v1.8.1
github.com/spf13/cobra v1.0.0
gopkg.in/yaml.v2 v2.2.2
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/clbanning/mxj v1.8.5-0.20201012155914-b957cfd48b51 h1:PK4/gQyzsZnkfJdEaWSybHdjsYORx+u34oqN9Sd63Rs=
github.com/clbanning/mxj v1.8.5-0.20201012155914-b957cfd48b51/go.mod h1:BVjHeAH+rl9rs6f+QIpeRl0tfu10SXn1pUSa5PVGJng=
github.com/clbanning/mxj/v2 v2.3.2 h1:DSkU65zfrBHtrggxd54X9pK1z/Lw2OwSW5D8p+x1toE=
github.com/clbanning/mxj/v2 v2.3.2/go.mod h1:hNiWqW14h+kc+MdF9C6/YoRfjEJoR3ou6tn/Qo+ve2s=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk=
github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
Expand Down
Loading

0 comments on commit a6a0372

Please sign in to comment.