mirror of
https://github.com/mikefarah/yq.git
synced 2025-01-24 23:20:05 +00:00
Simplified code
This commit is contained in:
parent
b1c670e121
commit
6a35a5f382
59
yaml.go
59
yaml.go
@ -19,29 +19,16 @@ func main() {
|
|||||||
Name: "read",
|
Name: "read",
|
||||||
Aliases: []string{"r"},
|
Aliases: []string{"r"},
|
||||||
Usage: "read <filename> <path>\n\te.g.: yaml read sample.json a.b.c\n\t(default) reads a property from a given yaml file",
|
Usage: "read <filename> <path>\n\te.g.: yaml read sample.json a.b.c\n\t(default) reads a property from a given yaml file",
|
||||||
Action: read_file,
|
Action: read_property,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
app.Action = read_file
|
app.Action = read_property
|
||||||
app.Run(os.Args)
|
app.Run(os.Args)
|
||||||
}
|
}
|
||||||
|
|
||||||
func read_file(c *cli.Context) {
|
func read_property(c *cli.Context) {
|
||||||
if len(c.Args()) == 0 {
|
|
||||||
log.Fatalf("Must provide filename")
|
|
||||||
}
|
|
||||||
var filename = c.Args()[0]
|
|
||||||
var raw_data, read_error = ioutil.ReadFile(filename)
|
|
||||||
if read_error != nil {
|
|
||||||
log.Fatalf("error: %v", read_error)
|
|
||||||
}
|
|
||||||
|
|
||||||
var parsed_data map[interface{}]interface{}
|
var parsed_data map[interface{}]interface{}
|
||||||
|
read_yaml(c, &parsed_data)
|
||||||
err := yaml.Unmarshal([]byte(raw_data), &parsed_data)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("error: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
var path = c.Args()[1]
|
var path = c.Args()[1]
|
||||||
var paths = strings.Split(path, ".")
|
var paths = strings.Split(path, ".")
|
||||||
@ -49,14 +36,36 @@ func read_file(c *cli.Context) {
|
|||||||
fmt.Println(read(parsed_data, paths[0], paths[1:len(paths)]))
|
fmt.Println(read(parsed_data, paths[0], paths[1:len(paths)]))
|
||||||
}
|
}
|
||||||
|
|
||||||
func read(context map[interface{}]interface{}, head string, tail []string) interface{} {
|
func read_yaml(c *cli.Context, parsed_data *map[interface{}]interface{}) {
|
||||||
value := context[head]
|
if len(c.Args()) == 0 {
|
||||||
// fmt.Println("read called")
|
log.Fatalf("Must provide filename")
|
||||||
|
}
|
||||||
|
var raw_data = read_file(c.Args()[0])
|
||||||
|
|
||||||
switch value.(type) {
|
if len(c.Args()) == 1 {
|
||||||
case bool, int, string, []interface{}:
|
fmt.Println(string(raw_data[:]))
|
||||||
return value
|
os.Exit(0)
|
||||||
default: // recurse into map
|
}
|
||||||
return read(value.(map[interface{}]interface{}), tail[0], tail[1:len(tail)])
|
|
||||||
|
err := yaml.Unmarshal([]byte(raw_data), &parsed_data)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("error: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func read_file(filename string) []byte {
|
||||||
|
var raw_data, read_error = ioutil.ReadFile(filename)
|
||||||
|
if read_error != nil {
|
||||||
|
log.Fatalf("error: %v", read_error)
|
||||||
|
}
|
||||||
|
return raw_data
|
||||||
|
}
|
||||||
|
|
||||||
|
func read(context map[interface{}]interface{}, head string, tail []string) interface{} {
|
||||||
|
value := context[head]
|
||||||
|
if (len(tail) > 0) {
|
||||||
|
return read(value.(map[interface{}]interface{}), tail[0], tail[1:len(tail)])
|
||||||
|
} else {
|
||||||
|
return value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user