diff --git a/cmd/commands_test.go b/cmd/commands_test.go index 4253fa50..77fe3a59 100644 --- a/cmd/commands_test.go +++ b/cmd/commands_test.go @@ -1201,6 +1201,29 @@ func TestWriteCmd(t *testing.T) { test.AssertResult(t, expectedOutput, result.Output) } +func TestWriteFromFileCmd(t *testing.T) { + content := `b: + c: 3 +` + filename := test.WriteTempYamlFile(content) + defer test.RemoveTempYamlFile(filename) + + source := `kittens: are cute # sure are!` + fromFilename := test.WriteTempYamlFile(source) + defer test.RemoveTempYamlFile(fromFilename) + + cmd := getRootCommand() + result := test.RunCmd(cmd, fmt.Sprintf("write %s b.c -f %s", filename, fromFilename)) + if result.Error != nil { + t.Error(result.Error) + } + expectedOutput := `b: + c: + kittens: are cute # sure are! +` + test.AssertResult(t, expectedOutput, result.Output) +} + func TestWriteEmptyCmd(t *testing.T) { content := `` filename := test.WriteTempYamlFile(content) diff --git a/cmd/constant.go b/cmd/constant.go index b18a9f94..0df9397c 100644 --- a/cmd/constant.go +++ b/cmd/constant.go @@ -9,6 +9,7 @@ var customTag = "" var printMode = "v" var writeInplace = false var writeScript = "" +var sourceYamlFile = "" var outputToJSON = false var prettyPrint = false var explodeAnchors = false diff --git a/cmd/utils.go b/cmd/utils.go index 1d491f28..2283e576 100644 --- a/cmd/utils.go +++ b/cmd/utils.go @@ -367,6 +367,17 @@ func readUpdateCommands(args []string, expectedArgs int, badArgsMessage string) } log.Debugf("Read write commands file '%v'", updateCommands) + } else if sourceYamlFile != "" && len(args) == expectedArgs-1 { + log.Debugf("Reading value from %v", sourceYamlFile) + var value yaml.Node + err := readData(sourceYamlFile, 0, &value) + if err != nil && err != io.EOF { + return nil, err + } + log.Debug("args %v", args[expectedArgs-2]) + updateCommands = make([]yqlib.UpdateCommand, 1) + updateCommands[0] = yqlib.UpdateCommand{Command: "update", Path: args[expectedArgs-2], Value: value.Content[0], Overwrite: true} + } else if len(args) < expectedArgs { return nil, errors.New(badArgsMessage) } else { diff --git a/cmd/write.go b/cmd/write.go index 0528d4fd..8aa97592 100644 --- a/cmd/write.go +++ b/cmd/write.go @@ -43,6 +43,7 @@ format is list of update commands (update or delete) like so: } cmdWrite.PersistentFlags().BoolVarP(&writeInplace, "inplace", "i", false, "update the yaml file inplace") cmdWrite.PersistentFlags().StringVarP(&writeScript, "script", "s", "", "yaml script for updating yaml") + cmdWrite.PersistentFlags().StringVarP(&sourceYamlFile, "from", "f", "", "yaml file for updating yaml (as-is)") cmdWrite.PersistentFlags().StringVarP(&customTag, "tag", "t", "", "set yaml tag (e.g. !!int)") cmdWrite.PersistentFlags().StringVarP(&docIndex, "doc", "d", "0", "process document index number (0 based, * for all documents)") return cmdWrite