Skip to content

Commit 82f1230

Browse files
author
Mike Farah
committed
Added "new" command for creating new yaml
1 parent a3190f1 commit 82f1230

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

yaml.go

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,14 @@ func main() {
3030

3131
var cmdRead = createReadCmd()
3232
var cmdWrite = createWriteCmd()
33+
var cmdNew = createNewCmd()
3334

3435
var rootCmd = &cobra.Command{Use: "yaml"}
3536
rootCmd.PersistentFlags().BoolVarP(&trimOutput, "trim", "t", true, "trim yaml output")
3637
rootCmd.PersistentFlags().BoolVarP(&outputToJSON, "tojson", "j", false, "output as json")
3738
rootCmd.PersistentFlags().BoolVarP(&inputJSON, "fromjson", "J", false, "input as json")
3839
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose mode")
39-
rootCmd.AddCommand(cmdRead, cmdWrite)
40+
rootCmd.AddCommand(cmdRead, cmdWrite, cmdNew)
4041
rootCmd.Execute()
4142
}
4243

@@ -87,6 +88,28 @@ a.b.e:
8788
return cmdWrite
8889
}
8990

91+
func createNewCmd() *cobra.Command {
92+
var cmdNew = &cobra.Command{
93+
Use: "new [path] [value]",
94+
Aliases: []string{"n"},
95+
Short: "yaml n [--script/-s script_file] a.b.c newValueForC",
96+
Example: `
97+
yaml new a.b.c cat
98+
yaml n a.b.c cat
99+
yaml n --script create_script.yaml
100+
`,
101+
Long: `Creates a new yaml w.r.t the given path and value.
102+
Outputs to STDOUT
103+
104+
Create Scripts:
105+
Note that you can give a create script to perform more sophisticated yaml. This follows the same format as the update script.
106+
`,
107+
Run: newProperty,
108+
}
109+
cmdNew.PersistentFlags().StringVarP(&writeScript, "script", "s", "", "yaml script for updating yaml")
110+
return cmdNew
111+
}
112+
90113
func readProperty(cmd *cobra.Command, args []string) {
91114
if verbose {
92115
backend.SetLevel(logging.DEBUG, "")
@@ -108,6 +131,35 @@ func read(args []string) interface{} {
108131
return readMap(parsedData, paths[0], paths[1:len(paths)])
109132
}
110133

134+
func newProperty(cmd *cobra.Command, args []string) {
135+
if verbose {
136+
backend.SetLevel(logging.DEBUG, "")
137+
}
138+
updatedData := newYaml(args)
139+
print(updatedData)
140+
}
141+
142+
func newYaml(args []string) interface{} {
143+
var writeCommands map[string]interface{}
144+
if writeScript != "" {
145+
readData(writeScript, &writeCommands, false)
146+
} else if len(args) < 2 {
147+
die("Must provide <path_to_update> <value>")
148+
} else {
149+
writeCommands = make(map[string]interface{})
150+
writeCommands[args[0]] = parseValue(args[1])
151+
}
152+
153+
parsedData := make(yaml.MapSlice, 0)
154+
155+
for path, value := range writeCommands {
156+
var paths = parsePath(path)
157+
parsedData = writeMap(parsedData, paths, value)
158+
}
159+
160+
return parsedData
161+
}
162+
111163
func writeProperty(cmd *cobra.Command, args []string) {
112164
if verbose {
113165
backend.SetLevel(logging.DEBUG, "")

yaml_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ application: MyApp`,
3636
formattedResult)
3737
}
3838

39+
func TestNewYaml(t *testing.T) {
40+
result := newYaml([]string{"b.c", "3"})
41+
formattedResult := fmt.Sprintf("%v", result)
42+
assertResult(t,
43+
"[{b [{c 3}]}]",
44+
formattedResult)
45+
}
46+
3947
func TestUpdateYaml(t *testing.T) {
4048
result := updateYaml([]string{"sample.yaml", "b.c", "3"})
4149
formattedResult := fmt.Sprintf("%v", result)
@@ -48,3 +56,12 @@ func TestUpdateYaml_WithScript(t *testing.T) {
4856
writeScript = "instruction_sample.yaml"
4957
updateYaml([]string{"sample.yaml"})
5058
}
59+
60+
func TestNewYaml_WithScript(t *testing.T) {
61+
writeScript = "instruction_sample.yaml"
62+
result := newYaml([]string{""})
63+
formattedResult := fmt.Sprintf("%v", result)
64+
assertResult(t,
65+
"[{b [{c cat} {e [[{name Mike Farah}]]}]}]",
66+
formattedResult)
67+
}

0 commit comments

Comments
 (0)