mirror of
https://github.com/mikefarah/yq.git
synced 2024-12-19 20:19:04 +00:00
Can supply value for write from file
This commit is contained in:
parent
9de2573009
commit
5cc01e43bc
@ -1201,6 +1201,29 @@ func TestWriteCmd(t *testing.T) {
|
|||||||
test.AssertResult(t, expectedOutput, result.Output)
|
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) {
|
func TestWriteEmptyCmd(t *testing.T) {
|
||||||
content := ``
|
content := ``
|
||||||
filename := test.WriteTempYamlFile(content)
|
filename := test.WriteTempYamlFile(content)
|
||||||
|
@ -9,6 +9,7 @@ var customTag = ""
|
|||||||
var printMode = "v"
|
var printMode = "v"
|
||||||
var writeInplace = false
|
var writeInplace = false
|
||||||
var writeScript = ""
|
var writeScript = ""
|
||||||
|
var sourceYamlFile = ""
|
||||||
var outputToJSON = false
|
var outputToJSON = false
|
||||||
var prettyPrint = false
|
var prettyPrint = false
|
||||||
var explodeAnchors = false
|
var explodeAnchors = false
|
||||||
|
11
cmd/utils.go
11
cmd/utils.go
@ -367,6 +367,17 @@ func readUpdateCommands(args []string, expectedArgs int, badArgsMessage string)
|
|||||||
}
|
}
|
||||||
|
|
||||||
log.Debugf("Read write commands file '%v'", updateCommands)
|
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 {
|
} else if len(args) < expectedArgs {
|
||||||
return nil, errors.New(badArgsMessage)
|
return nil, errors.New(badArgsMessage)
|
||||||
} else {
|
} else {
|
||||||
|
@ -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().BoolVarP(&writeInplace, "inplace", "i", false, "update the yaml file inplace")
|
||||||
cmdWrite.PersistentFlags().StringVarP(&writeScript, "script", "s", "", "yaml script for updating yaml")
|
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(&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)")
|
cmdWrite.PersistentFlags().StringVarP(&docIndex, "doc", "d", "0", "process document index number (0 based, * for all documents)")
|
||||||
return cmdWrite
|
return cmdWrite
|
||||||
|
Loading…
Reference in New Issue
Block a user