mirror of
https://github.com/mikefarah/yq.git
synced 2025-01-13 11:55:38 +00:00
Now attempts to parse the write value type
This commit is contained in:
parent
364c1a8af3
commit
c1b803364a
25
yaml.go
25
yaml.go
@ -61,18 +61,39 @@ func writeProperty(c *cli.Context) {
|
|||||||
var parsedData map[interface{}]interface{}
|
var parsedData map[interface{}]interface{}
|
||||||
readYaml(c, &parsedData)
|
readYaml(c, &parsedData)
|
||||||
|
|
||||||
if len(c.Args()) != 3 {
|
if len(c.Args()) < 3 {
|
||||||
log.Fatalf("Must provide <filename> <path_to_update> <value>")
|
log.Fatalf("Must provide <filename> <path_to_update> <value>")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var forceString bool
|
||||||
|
if len(c.Args()) == 4 {
|
||||||
|
forceString = true
|
||||||
|
}
|
||||||
|
|
||||||
var path = c.Args()[1]
|
var path = c.Args()[1]
|
||||||
var paths = strings.Split(path, ".")
|
var paths = strings.Split(path, ".")
|
||||||
|
|
||||||
write(parsedData, paths[0], paths[1:len(paths)], c.Args()[2])
|
write(parsedData, paths[0], paths[1:len(paths)], getValue(c.Args()[2], forceString))
|
||||||
|
|
||||||
printYaml(parsedData, c.Bool("trim"))
|
printYaml(parsedData, c.Bool("trim"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getValue(argument string, forceString bool) interface{} {
|
||||||
|
var value, err interface{}
|
||||||
|
|
||||||
|
if !forceString {
|
||||||
|
value, err = strconv.ParseFloat(argument, 64)
|
||||||
|
if err == nil {
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
value, err = strconv.ParseBool(argument)
|
||||||
|
if err == nil {
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return argument
|
||||||
|
}
|
||||||
|
|
||||||
func printYaml(context interface{}, trim bool) {
|
func printYaml(context interface{}, trim bool) {
|
||||||
out, err := yaml.Marshal(context)
|
out, err := yaml.Marshal(context)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user