Added "new" command for creating new yaml

This commit is contained in:
Mike Farah 2017-04-12 21:10:00 +10:00
parent a3190f1504
commit 82f1230db8
2 changed files with 70 additions and 1 deletions

54
yaml.go
View File

@ -30,13 +30,14 @@ func main() {
var cmdRead = createReadCmd() var cmdRead = createReadCmd()
var cmdWrite = createWriteCmd() var cmdWrite = createWriteCmd()
var cmdNew = createNewCmd()
var rootCmd = &cobra.Command{Use: "yaml"} var rootCmd = &cobra.Command{Use: "yaml"}
rootCmd.PersistentFlags().BoolVarP(&trimOutput, "trim", "t", true, "trim yaml output") rootCmd.PersistentFlags().BoolVarP(&trimOutput, "trim", "t", true, "trim yaml output")
rootCmd.PersistentFlags().BoolVarP(&outputToJSON, "tojson", "j", false, "output as json") rootCmd.PersistentFlags().BoolVarP(&outputToJSON, "tojson", "j", false, "output as json")
rootCmd.PersistentFlags().BoolVarP(&inputJSON, "fromjson", "J", false, "input as json") rootCmd.PersistentFlags().BoolVarP(&inputJSON, "fromjson", "J", false, "input as json")
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose mode") rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose mode")
rootCmd.AddCommand(cmdRead, cmdWrite) rootCmd.AddCommand(cmdRead, cmdWrite, cmdNew)
rootCmd.Execute() rootCmd.Execute()
} }
@ -87,6 +88,28 @@ a.b.e:
return cmdWrite return cmdWrite
} }
func createNewCmd() *cobra.Command {
var cmdNew = &cobra.Command{
Use: "new [path] [value]",
Aliases: []string{"n"},
Short: "yaml n [--script/-s script_file] a.b.c newValueForC",
Example: `
yaml new a.b.c cat
yaml n a.b.c cat
yaml n --script create_script.yaml
`,
Long: `Creates a new yaml w.r.t the given path and value.
Outputs to STDOUT
Create Scripts:
Note that you can give a create script to perform more sophisticated yaml. This follows the same format as the update script.
`,
Run: newProperty,
}
cmdNew.PersistentFlags().StringVarP(&writeScript, "script", "s", "", "yaml script for updating yaml")
return cmdNew
}
func readProperty(cmd *cobra.Command, args []string) { func readProperty(cmd *cobra.Command, args []string) {
if verbose { if verbose {
backend.SetLevel(logging.DEBUG, "") backend.SetLevel(logging.DEBUG, "")
@ -108,6 +131,35 @@ func read(args []string) interface{} {
return readMap(parsedData, paths[0], paths[1:len(paths)]) return readMap(parsedData, paths[0], paths[1:len(paths)])
} }
func newProperty(cmd *cobra.Command, args []string) {
if verbose {
backend.SetLevel(logging.DEBUG, "")
}
updatedData := newYaml(args)
print(updatedData)
}
func newYaml(args []string) interface{} {
var writeCommands map[string]interface{}
if writeScript != "" {
readData(writeScript, &writeCommands, false)
} else if len(args) < 2 {
die("Must provide <path_to_update> <value>")
} else {
writeCommands = make(map[string]interface{})
writeCommands[args[0]] = parseValue(args[1])
}
parsedData := make(yaml.MapSlice, 0)
for path, value := range writeCommands {
var paths = parsePath(path)
parsedData = writeMap(parsedData, paths, value)
}
return parsedData
}
func writeProperty(cmd *cobra.Command, args []string) { func writeProperty(cmd *cobra.Command, args []string) {
if verbose { if verbose {
backend.SetLevel(logging.DEBUG, "") backend.SetLevel(logging.DEBUG, "")

View File

@ -36,6 +36,14 @@ application: MyApp`,
formattedResult) formattedResult)
} }
func TestNewYaml(t *testing.T) {
result := newYaml([]string{"b.c", "3"})
formattedResult := fmt.Sprintf("%v", result)
assertResult(t,
"[{b [{c 3}]}]",
formattedResult)
}
func TestUpdateYaml(t *testing.T) { func TestUpdateYaml(t *testing.T) {
result := updateYaml([]string{"sample.yaml", "b.c", "3"}) result := updateYaml([]string{"sample.yaml", "b.c", "3"})
formattedResult := fmt.Sprintf("%v", result) formattedResult := fmt.Sprintf("%v", result)
@ -48,3 +56,12 @@ func TestUpdateYaml_WithScript(t *testing.T) {
writeScript = "instruction_sample.yaml" writeScript = "instruction_sample.yaml"
updateYaml([]string{"sample.yaml"}) updateYaml([]string{"sample.yaml"})
} }
func TestNewYaml_WithScript(t *testing.T) {
writeScript = "instruction_sample.yaml"
result := newYaml([]string{""})
formattedResult := fmt.Sprintf("%v", result)
assertResult(t,
"[{b [{c cat} {e [[{name Mike Farah}]]}]}]",
formattedResult)
}