diff --git a/yaml.go b/yaml.go index 2aa190bd..5ea8312e 100644 --- a/yaml.go +++ b/yaml.go @@ -30,13 +30,14 @@ func main() { var cmdRead = createReadCmd() var cmdWrite = createWriteCmd() + var cmdNew = createNewCmd() var rootCmd = &cobra.Command{Use: "yaml"} rootCmd.PersistentFlags().BoolVarP(&trimOutput, "trim", "t", true, "trim yaml output") rootCmd.PersistentFlags().BoolVarP(&outputToJSON, "tojson", "j", false, "output as json") rootCmd.PersistentFlags().BoolVarP(&inputJSON, "fromjson", "J", false, "input as json") rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose mode") - rootCmd.AddCommand(cmdRead, cmdWrite) + rootCmd.AddCommand(cmdRead, cmdWrite, cmdNew) rootCmd.Execute() } @@ -87,6 +88,28 @@ a.b.e: 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) { if verbose { backend.SetLevel(logging.DEBUG, "") @@ -108,6 +131,35 @@ func read(args []string) interface{} { 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 ") + } 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) { if verbose { backend.SetLevel(logging.DEBUG, "") diff --git a/yaml_test.go b/yaml_test.go index 0cd22e35..b2af972a 100644 --- a/yaml_test.go +++ b/yaml_test.go @@ -36,6 +36,14 @@ application: MyApp`, 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) { result := updateYaml([]string{"sample.yaml", "b.c", "3"}) formattedResult := fmt.Sprintf("%v", result) @@ -48,3 +56,12 @@ func TestUpdateYaml_WithScript(t *testing.T) { writeScript = "instruction_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) +}