2020-01-13 09:11:56 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2020-02-27 23:09:49 +00:00
|
|
|
"github.com/mikefarah/yq/v3/pkg/yqlib"
|
2020-01-13 09:11:56 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
func createNewCmd() *cobra.Command {
|
|
|
|
var cmdNew = &cobra.Command{
|
|
|
|
Use: "new [path] [value]",
|
|
|
|
Aliases: []string{"n"},
|
|
|
|
Short: "yq n [--script/-s script_file] a.b.c newValue",
|
|
|
|
Example: `
|
|
|
|
yq new 'a.b.c' cat
|
|
|
|
yq n 'a.b.c' --tag '!!str' true # force 'true' to be interpreted as a string instead of bool
|
|
|
|
yq n 'a.b[+]' cat
|
|
|
|
yq n -- '--key-starting-with-dash' cat # need to use '--' to stop processing arguments as flags
|
|
|
|
yq 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.
|
|
|
|
`,
|
|
|
|
RunE: newProperty,
|
|
|
|
}
|
|
|
|
cmdNew.PersistentFlags().StringVarP(&writeScript, "script", "s", "", "yaml script for creating yaml")
|
|
|
|
cmdNew.PersistentFlags().StringVarP(&customTag, "tag", "t", "", "set yaml tag (e.g. !!int)")
|
2020-04-17 01:03:43 +00:00
|
|
|
cmdNew.PersistentFlags().StringVarP(&customStyle, "style", "", "", "formatting style of the value: singleQuoted, doubleQuoted, folded, flow, literal, tagged")
|
2020-01-13 09:11:56 +00:00
|
|
|
return cmdNew
|
|
|
|
}
|
|
|
|
|
|
|
|
func newProperty(cmd *cobra.Command, args []string) error {
|
|
|
|
var updateCommands, updateCommandsError = readUpdateCommands(args, 2, "Must provide <path_to_update> <value>")
|
|
|
|
if updateCommandsError != nil {
|
|
|
|
return updateCommandsError
|
|
|
|
}
|
|
|
|
newNode := lib.New(updateCommands[0].Path)
|
|
|
|
|
|
|
|
for _, updateCommand := range updateCommands {
|
|
|
|
|
|
|
|
errorUpdating := lib.Update(&newNode, updateCommand, true)
|
|
|
|
|
|
|
|
if errorUpdating != nil {
|
|
|
|
return errorUpdating
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-27 23:09:49 +00:00
|
|
|
var encoder = yqlib.NewYamlEncoder(cmd.OutOrStdout(), indent, colorsEnabled)
|
|
|
|
return encoder.Encode(&newNode)
|
2020-01-13 09:11:56 +00:00
|
|
|
}
|