mirror of
https://github.com/mikefarah/yq.git
synced 2024-11-12 13:48:06 +00:00
Handle arrays and strings when reading
This commit is contained in:
parent
cdd78af4bc
commit
5bb0934710
1
sample_array.yaml
Normal file
1
sample_array.yaml
Normal file
@ -0,0 +1 @@
|
|||||||
|
[1,2,3]
|
1
sample_text.yaml
Normal file
1
sample_text.yaml
Normal file
@ -0,0 +1 @@
|
|||||||
|
hi
|
6
vendor/vendor.json
vendored
Normal file
6
vendor/vendor.json
vendored
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"comment": "",
|
||||||
|
"ignore": "test",
|
||||||
|
"package": [],
|
||||||
|
"rootPath": "github.com/mikefarah/yaml"
|
||||||
|
}
|
39
yaml.go
39
yaml.go
@ -117,15 +117,26 @@ func readProperty(cmd *cobra.Command, args []string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func read(args []string) interface{} {
|
func read(args []string) interface{} {
|
||||||
|
|
||||||
var parsedData yaml.MapSlice
|
var parsedData yaml.MapSlice
|
||||||
|
var path = ""
|
||||||
|
if len(args) > 1 {
|
||||||
|
path = args[1]
|
||||||
|
}
|
||||||
|
err := readData(args[0], &parsedData, inputJSON)
|
||||||
|
if err != nil {
|
||||||
|
var generalData interface{}
|
||||||
|
readDataOrDie(args[0], &generalData, inputJSON)
|
||||||
|
item := yaml.MapItem{Key: "thing", Value: generalData}
|
||||||
|
parsedData = yaml.MapSlice{item}
|
||||||
|
path = "thing." + path
|
||||||
|
}
|
||||||
|
|
||||||
readData(args[0], &parsedData, inputJSON)
|
if path == "" {
|
||||||
|
|
||||||
if len(args) == 1 {
|
|
||||||
return parsedData
|
return parsedData
|
||||||
}
|
}
|
||||||
|
|
||||||
var paths = parsePath(args[1])
|
var paths = parsePath(path)
|
||||||
|
|
||||||
return readMap(parsedData, paths[0], paths[1:len(paths)])
|
return readMap(parsedData, paths[0], paths[1:len(paths)])
|
||||||
}
|
}
|
||||||
@ -141,7 +152,7 @@ func newProperty(cmd *cobra.Command, args []string) {
|
|||||||
func newYaml(args []string) interface{} {
|
func newYaml(args []string) interface{} {
|
||||||
var writeCommands yaml.MapSlice
|
var writeCommands yaml.MapSlice
|
||||||
if writeScript != "" {
|
if writeScript != "" {
|
||||||
readData(writeScript, &writeCommands, false)
|
readDataOrDie(writeScript, &writeCommands, false)
|
||||||
} else if len(args) < 2 {
|
} else if len(args) < 2 {
|
||||||
die("Must provide <path_to_update> <value>")
|
die("Must provide <path_to_update> <value>")
|
||||||
} else {
|
} else {
|
||||||
@ -179,7 +190,7 @@ func updateParsedData(parsedData yaml.MapSlice, writeCommands yaml.MapSlice) yam
|
|||||||
func updateYaml(args []string) interface{} {
|
func updateYaml(args []string) interface{} {
|
||||||
var writeCommands yaml.MapSlice
|
var writeCommands yaml.MapSlice
|
||||||
if writeScript != "" {
|
if writeScript != "" {
|
||||||
readData(writeScript, &writeCommands, false)
|
readDataOrDie(writeScript, &writeCommands, false)
|
||||||
} else if len(args) < 3 {
|
} else if len(args) < 3 {
|
||||||
die("Must provide <filename> <path_to_update> <value>")
|
die("Must provide <filename> <path_to_update> <value>")
|
||||||
} else {
|
} else {
|
||||||
@ -188,7 +199,7 @@ func updateYaml(args []string) interface{} {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var parsedData yaml.MapSlice
|
var parsedData yaml.MapSlice
|
||||||
readData(args[0], &parsedData, inputJSON)
|
readDataOrDie(args[0], &parsedData, inputJSON)
|
||||||
|
|
||||||
return updateParsedData(parsedData, writeCommands)
|
return updateParsedData(parsedData, writeCommands)
|
||||||
}
|
}
|
||||||
@ -234,7 +245,14 @@ func yamlToString(context interface{}) string {
|
|||||||
return outStr
|
return outStr
|
||||||
}
|
}
|
||||||
|
|
||||||
func readData(filename string, parsedData interface{}, readAsJSON bool) {
|
func readDataOrDie(filename string, parsedData interface{}, readAsJSON bool) {
|
||||||
|
err := readData(filename, parsedData, readAsJSON)
|
||||||
|
if err != nil {
|
||||||
|
die("error parsing data: ", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func readData(filename string, parsedData interface{}, readAsJSON bool) error {
|
||||||
if filename == "" {
|
if filename == "" {
|
||||||
die("Must provide filename")
|
die("Must provide filename")
|
||||||
}
|
}
|
||||||
@ -246,10 +264,7 @@ func readData(filename string, parsedData interface{}, readAsJSON bool) {
|
|||||||
rawData = readFile(filename)
|
rawData = readFile(filename)
|
||||||
}
|
}
|
||||||
|
|
||||||
err := yaml.Unmarshal([]byte(rawData), parsedData)
|
return yaml.Unmarshal([]byte(rawData), parsedData)
|
||||||
if err != nil {
|
|
||||||
die("error parsing data: ", err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func readStdin() []byte {
|
func readStdin() []byte {
|
||||||
|
10
yaml_test.go
10
yaml_test.go
@ -28,6 +28,16 @@ func TestRead(t *testing.T) {
|
|||||||
assertResult(t, 2, result)
|
assertResult(t, 2, result)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestReadArray(t *testing.T) {
|
||||||
|
result := read([]string{"sample_array.yaml", "[1]"})
|
||||||
|
assertResult(t, 2, result)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestReadString(t *testing.T) {
|
||||||
|
result := read([]string{"sample_text.yaml"})
|
||||||
|
assertResult(t, "hi", result)
|
||||||
|
}
|
||||||
|
|
||||||
func TestOrder(t *testing.T) {
|
func TestOrder(t *testing.T) {
|
||||||
result := read([]string{"order.yaml"})
|
result := read([]string{"order.yaml"})
|
||||||
formattedResult := yamlToString(result)
|
formattedResult := yamlToString(result)
|
||||||
|
Loading…
Reference in New Issue
Block a user