Skip to content

Commit

Permalink
Merge pull request #39 from kenjones-cisco/feature/append-array
Browse files Browse the repository at this point in the history
Feature: Add append to array
  • Loading branch information
kenjones-cisco authored Sep 24, 2017
2 parents c17f8df + c5f80a1 commit 9d1a5b1
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 9 deletions.
19 changes: 19 additions & 0 deletions commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,25 @@ func TestWriteCmd_Inplace(t *testing.T) {
assertResult(t, expectedOutput, gotOutput)
}

func TestWriteCmd_Append(t *testing.T) {
content := `b:
- foo
`
filename := writeTempYamlFile(content)
defer removeTempYamlFile(filename)

cmd := getRootCommand()
result := runCmd(cmd, fmt.Sprintf("write %s b[+] 7", filename))
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `b:
- foo
- 7
`
assertResult(t, expectedOutput, result.Output)
}

func TestMergeCmd(t *testing.T) {
cmd := getRootCommand()
result := runCmd(cmd, "merge examples/data1.yaml examples/data2.yaml")
Expand Down
10 changes: 8 additions & 2 deletions data_navigator.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func updatedChildValue(child interface{}, remainingPaths []string, value interfa
}

_, nextIndexErr := strconv.ParseInt(remainingPaths[0], 10, 64)
if nextIndexErr != nil {
if nextIndexErr != nil && remainingPaths[0] != "+" {
// must be a map
return writeMap(child, remainingPaths, value)
}
Expand All @@ -81,7 +81,13 @@ func writeArray(context interface{}, paths []string, value interface{}) []interf
log.Debugf("\tarray %v\n", array)

rawIndex := paths[0]
index, _ := strconv.ParseInt(rawIndex, 10, 64)
var index int64
// the append array indicator
if rawIndex == "+" {
index = int64(len(array))
} else {
index, _ = strconv.ParseInt(rawIndex, 10, 64)
}
// writeArray is only called by updatedChildValue which handles parsing the
// index, as such this renders this dead code.
// if err != nil {
Expand Down
7 changes: 6 additions & 1 deletion docs/mkdocs/search_index.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
},
{
"location": "/write/",
"text": "yaml w \nyaml_file|json_file\n \npath\n \nnew value\n\n\n\n\n\nThis command can take a json file as input too, and will output yaml unless specified to export as json (-j)\n\n\nTo Stdout\n\n\nGiven a sample.yaml file of:\n\n\nb:\n c: 2\n\n\n\n\nthen\n\n\nyaml w sample.yaml b.c cat\n\n\n\n\nwill output:\n\n\nb:\n c: cat\n\n\n\n\nFrom STDIN\n\n\ncat sample.yaml | yaml w - b.c blah\n\n\n\n\nAdding new fields\n\n\nAny missing fields in the path will be created on the fly.\n\n\nGiven a sample.yaml file of:\n\n\nb:\n c: 2\n\n\n\n\nthen\n\n\nyaml w sample.yaml b.d[0] \nnew thing\n\n\n\n\n\nwill output:\n\n\nb:\n c: cat\n d:\n - new thing\n\n\n\n\nUpdating files in-place\n\n\nGiven a sample.yaml file of:\n\n\nb:\n c: 2\n\n\n\n\nthen\n\n\nyaml w -i sample.yaml b.c cat\n\n\n\n\nwill update the sample.yaml file so that the value of 'c' is cat.\n\n\nUpdating multiple values with a script\n\n\nGiven a sample.yaml file of:\n\n\nb:\n c: 2\n e:\n - name: Billy Bob\n\n\n\n\nand a script update_instructions.yaml of:\n\n\nb.c: 3\nb.e[0].name: Howdy Partner\n\n\n\n\nthen\n\n\nyaml w -s update_instructions.yaml sample.yaml\n\n\n\n\nwill output:\n\n\nb:\n c: 3\n e:\n - name: Howdy Partner\n\n\n\n\nAnd, of course, you can pipe the instructions in using '-':\n\n\ncat update_instructions.yaml | yaml w -s - sample.yaml\n\n\n\n\nValues starting with a hyphen (or dash)\n\n\nThe flag terminator needs to be used to stop the app from attempting to parse the subsequent arguments as flags:\n\n\nyaml w -- my.path -3\n\n\n\n\nwill output\n\n\nmy:\n path: -3",
"text": "yaml w \nyaml_file|json_file\n \npath\n \nnew value\n\n\n\n\n\nThis command can take a json file as input too, and will output yaml unless specified to export as json (-j)\n\n\nTo Stdout\n\n\nGiven a sample.yaml file of:\n\n\nb:\n c: 2\n\n\n\n\nthen\n\n\nyaml w sample.yaml b.c cat\n\n\n\n\nwill output:\n\n\nb:\n c: cat\n\n\n\n\nFrom STDIN\n\n\ncat sample.yaml | yaml w - b.c blah\n\n\n\n\nAdding new fields\n\n\nAny missing fields in the path will be created on the fly.\n\n\nGiven a sample.yaml file of:\n\n\nb:\n c: 2\n\n\n\n\nthen\n\n\nyaml w sample.yaml b.d[0] \nnew thing\n\n\n\n\n\nwill output:\n\n\nb:\n c: cat\n d:\n - new thing\n\n\n\n\nAppending value to an array field\n\n\nGiven a sample.yaml file of:\n\n\nb:\n c: 2\n d:\n - new thing\n - foo thing\n\n\n\n\nthen\n\n\nyaml w sample.yaml b.d[+] \nbar thing\n\n\n\n\n\nwill output:\n\n\nb:\n c: cat\n d:\n - new thing\n - foo thing\n - bar thing\n\n\n\n\nUpdating files in-place\n\n\nGiven a sample.yaml file of:\n\n\nb:\n c: 2\n\n\n\n\nthen\n\n\nyaml w -i sample.yaml b.c cat\n\n\n\n\nwill update the sample.yaml file so that the value of 'c' is cat.\n\n\nUpdating multiple values with a script\n\n\nGiven a sample.yaml file of:\n\n\nb:\n c: 2\n e:\n - name: Billy Bob\n\n\n\n\nand a script update_instructions.yaml of:\n\n\nb.c: 3\nb.e[0].name: Howdy Partner\n\n\n\n\nthen\n\n\nyaml w -s update_instructions.yaml sample.yaml\n\n\n\n\nwill output:\n\n\nb:\n c: 3\n e:\n - name: Howdy Partner\n\n\n\n\nAnd, of course, you can pipe the instructions in using '-':\n\n\ncat update_instructions.yaml | yaml w -s - sample.yaml\n\n\n\n\nValues starting with a hyphen (or dash)\n\n\nThe flag terminator needs to be used to stop the app from attempting to parse the subsequent arguments as flags:\n\n\nyaml w -- my.path -3\n\n\n\n\nwill output\n\n\nmy:\n path: -3",
"title": "Write/Update"
},
{
Expand All @@ -75,6 +75,11 @@
"text": "Any missing fields in the path will be created on the fly. Given a sample.yaml file of: b:\n c: 2 then yaml w sample.yaml b.d[0] new thing will output: b:\n c: cat\n d:\n - new thing",
"title": "Adding new fields"
},
{
"location": "/write/#appending-value-to-an-array-field",
"text": "Given a sample.yaml file of: b:\n c: 2\n d:\n - new thing\n - foo thing then yaml w sample.yaml b.d[+] bar thing will output: b:\n c: cat\n d:\n - new thing\n - foo thing\n - bar thing",
"title": "Appending value to an array field"
},
{
"location": "/write/#updating-files-in-place",
"text": "Given a sample.yaml file of: b:\n c: 2 then yaml w -i sample.yaml b.c cat will update the sample.yaml file so that the value of 'c' is cat.",
Expand Down
12 changes: 6 additions & 6 deletions docs/sitemap.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,47 @@

<url>
<loc>/</loc>
<lastmod>2017-09-23</lastmod>
<lastmod>2017-09-24</lastmod>
<changefreq>daily</changefreq>
</url>



<url>
<loc>/read/</loc>
<lastmod>2017-09-23</lastmod>
<lastmod>2017-09-24</lastmod>
<changefreq>daily</changefreq>
</url>



<url>
<loc>/write/</loc>
<lastmod>2017-09-23</lastmod>
<lastmod>2017-09-24</lastmod>
<changefreq>daily</changefreq>
</url>



<url>
<loc>/create/</loc>
<lastmod>2017-09-23</lastmod>
<lastmod>2017-09-24</lastmod>
<changefreq>daily</changefreq>
</url>



<url>
<loc>/convert/</loc>
<lastmod>2017-09-23</lastmod>
<lastmod>2017-09-24</lastmod>
<changefreq>daily</changefreq>
</url>



<url>
<loc>/merge/</loc>
<lastmod>2017-09-23</lastmod>
<lastmod>2017-09-24</lastmod>
<changefreq>daily</changefreq>
</url>

Expand Down
36 changes: 36 additions & 0 deletions docs/write/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,13 @@
Adding new fields
</a>

</li>

<li class="md-nav__item">
<a href="#appending-value-to-an-array-field" title="Appending value to an array field" class="md-nav__link">
Appending value to an array field
</a>

</li>

<li class="md-nav__item">
Expand Down Expand Up @@ -355,6 +362,13 @@
Adding new fields
</a>

</li>

<li class="md-nav__item">
<a href="#appending-value-to-an-array-field" title="Appending value to an array field" class="md-nav__link">
Appending value to an array field
</a>

</li>

<li class="md-nav__item">
Expand Down Expand Up @@ -436,6 +450,28 @@ <h3 id="adding-new-fields">Adding new fields<a class="headerlink" href="#adding-
- new thing
</code></pre>

<h3 id="appending-value-to-an-array-field">Appending value to an array field<a class="headerlink" href="#appending-value-to-an-array-field" title="Permanent link">&para;</a></h3>
<p>Given a sample.yaml file of:</p>
<pre><code class="yaml">b:
c: 2
d:
- new thing
- foo thing
</code></pre>

<p>then</p>
<pre><code class="bash">yaml w sample.yaml b.d[+] &quot;bar thing&quot;
</code></pre>

<p>will output:</p>
<pre><code class="yaml">b:
c: cat
d:
- new thing
- foo thing
- bar thing
</code></pre>

<h3 id="updating-files-in-place">Updating files in-place<a class="headerlink" href="#updating-files-in-place" title="Permanent link">&para;</a></h3>
<p>Given a sample.yaml file of:</p>
<pre><code class="yaml">b:
Expand Down
23 changes: 23 additions & 0 deletions mkdocs/write.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,29 @@ b:
- new thing
```
### Appending value to an array field
Given a sample.yaml file of:
```yaml
b:
c: 2
d:
- new thing
- foo thing
```
then
```bash
yaml w sample.yaml b.d[+] "bar thing"
```
will output:
```yaml
b:
c: cat
d:
- new thing
- foo thing
- bar thing
```
### Updating files in-place
Given a sample.yaml file of:
```yaml
Expand Down
1 change: 1 addition & 0 deletions path_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var parsePathsTests = []struct {
}{
{"a.b", []string{"a", "b"}},
{"a.b[0]", []string{"a", "b", "0"}},
{"a.b.d[+]", []string{"a", "b", "d", "+"}},
}

func TestParsePath(t *testing.T) {
Expand Down
4 changes: 4 additions & 0 deletions yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,14 @@ yaml write --inplace things.yaml a.b.c cat
yaml w -i things.yaml a.b.c cat
yaml w --script update_script.yaml things.yaml
yaml w -i -s update_script.yaml things.yaml
yaml w things.yaml a.b.d[+] foo
yaml w things.yaml a.b.d[+] foo
`,
Long: `Updates the yaml file w.r.t the given path and value.
Outputs to STDOUT unless the inplace flag is used, in which case the file is updated instead.
Append value to array adds the value to the end of array.
Update Scripts:
Note that you can give an update script to perform more sophisticated updated. Update script
format is a yaml map where the key is the path and the value is..well the value. e.g.:
Expand Down

2 comments on commit 9d1a5b1

@mikefarah
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @kenjones-cisco - looks like you've been very busy and have added a bunch of new features 🎉

I think it's high time to make a new release of yaml, did you want to do that? I'm happy to do it if you don't have the time.

Cheers,

Mike

@kenjones-cisco
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mikefarah Sure, I can do that.

Please sign in to comment.