You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: yaml.go
+61-53Lines changed: 61 additions & 53 deletions
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@ package main
2
2
3
3
import (
4
4
"fmt"
5
-
"github.com/codegangsta/cli"
5
+
"github.com/spf13/cobra"
6
6
"gopkg.in/yaml.v2"
7
7
"io/ioutil"
8
8
"log"
@@ -11,74 +11,79 @@ import (
11
11
"strings"
12
12
)
13
13
14
+
vartrimOutput=true
15
+
varwriteInplace=false
16
+
14
17
funcmain() {
15
-
app:=cli.NewApp()
16
-
app.Name="yaml"
17
-
app.Usage="command line tool for reading and writing yaml"
18
-
app.Commands= []cli.Command{
19
-
{
20
-
Name: "read",
21
-
Aliases: []string{"r"},
22
-
Usage: "read <filename> <path>\n\te.g.: yaml read sample.yaml a.b.c\n\t(default) reads a property from a given yaml file\n",
23
-
Action: readProperty,
24
-
},
25
-
{
26
-
Name: "write",
27
-
Aliases: []string{"w"},
28
-
Usage: "write <filename> <path> <value>\n\te.g.: yaml write sample.yaml a.b.c 5\n\tupdates a property from a given yaml file, outputs to stdout\n",
29
-
Action: writeProperty,
30
-
},
31
-
{
32
-
Name: "write-inplace",
33
-
Aliases: []string{"wi"},
34
-
Usage: "wi <filename> <path> <value>\n\te.g.: yaml wi sample.yaml a.b.c 5\n\tupdates a property from a given yaml file and saves it to the given filename (sample.yaml)\n",
35
-
Action: writePropertyInPlace,
36
-
},
18
+
varcmdRead=&cobra.Command{
19
+
Use: "read [yaml_file] [path]",
20
+
Aliases: []string{"r"},
21
+
Short: "yaml r sample.yaml a.b.c",
22
+
Example: `
23
+
yaml read things.yaml a.b.c
24
+
yaml r - a.b.c (reads from stdin)
25
+
yaml r things.yaml a.*.c
26
+
yaml r things.yaml a.array[0].blah
27
+
yaml r things.yaml a.array[*].blah
28
+
`,
29
+
Long: "Outputs the value of the given path in the yaml file to STDOUT",
30
+
Run: readProperty,
37
31
}
38
-
app.Action=readProperty
39
-
app.Run(os.Args)
40
-
}
41
32
42
-
funcreadProperty(c*cli.Context) {
33
+
varcmdWrite=&cobra.Command{
34
+
Use: "write [yaml_file] [path] [value]",
35
+
Aliases: []string{"w"},
36
+
Short: "yaml w [--inplace/-i] sample.yaml a.b.c newValueForC",
37
+
Example: `
38
+
yaml write things.yaml a.b.c cat
39
+
yaml write --inplace things.yaml a.b.c cat
40
+
yaml w -i things.yaml a.b.c cat
41
+
`,
42
+
Long: `Updates the yaml file w.r.t the given path and value.
43
+
Outputs to STDOUT unless the inplace flag is used, in which case the file is updated instead.`,
44
+
Run: writeProperty,
45
+
}
46
+
cmdWrite.PersistentFlags().BoolVarP(&writeInplace, "inplace", "i", false, "update the yaml file inplace")
0 commit comments