2015-09-26 22:15:49 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
"log"
|
2015-09-26 22:20:42 +00:00
|
|
|
"io/ioutil"
|
2015-09-26 23:38:44 +00:00
|
|
|
"os"
|
|
|
|
"github.com/codegangsta/cli"
|
2015-09-27 03:11:10 +00:00
|
|
|
"strings"
|
2015-09-27 03:40:09 +00:00
|
|
|
"strconv"
|
2015-09-26 22:15:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2015-09-26 23:38:44 +00:00
|
|
|
app := cli.NewApp()
|
|
|
|
app.Name = "yaml"
|
|
|
|
app.Usage = "command line tool for reading and writing yaml"
|
|
|
|
app.Commands = []cli.Command{
|
|
|
|
{
|
|
|
|
Name: "read",
|
|
|
|
Aliases: []string{"r"},
|
2015-09-27 03:11:10 +00:00
|
|
|
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",
|
2015-09-27 03:18:33 +00:00
|
|
|
Action: read_property,
|
2015-09-26 23:38:44 +00:00
|
|
|
},
|
|
|
|
}
|
2015-09-27 03:18:33 +00:00
|
|
|
app.Action = read_property
|
2015-09-26 23:38:44 +00:00
|
|
|
app.Run(os.Args)
|
|
|
|
}
|
|
|
|
|
2015-09-27 03:18:33 +00:00
|
|
|
func read_property(c *cli.Context) {
|
|
|
|
var parsed_data map[interface{}]interface{}
|
|
|
|
read_yaml(c, &parsed_data)
|
|
|
|
|
|
|
|
var path = c.Args()[1]
|
|
|
|
var paths = strings.Split(path, ".")
|
|
|
|
|
2015-09-27 03:40:09 +00:00
|
|
|
fmt.Println(read_map(parsed_data, paths[0], paths[1:len(paths)]))
|
2015-09-27 03:18:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func read_yaml(c *cli.Context, parsed_data *map[interface{}]interface{}) {
|
2015-09-26 23:38:44 +00:00
|
|
|
if len(c.Args()) == 0 {
|
|
|
|
log.Fatalf("Must provide filename")
|
|
|
|
}
|
2015-09-27 03:18:33 +00:00
|
|
|
var raw_data = read_file(c.Args()[0])
|
2015-09-26 22:20:42 +00:00
|
|
|
|
2015-09-27 03:18:33 +00:00
|
|
|
if len(c.Args()) == 1 {
|
|
|
|
fmt.Println(string(raw_data[:]))
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
2015-09-26 22:15:49 +00:00
|
|
|
|
2015-09-26 22:20:42 +00:00
|
|
|
err := yaml.Unmarshal([]byte(raw_data), &parsed_data)
|
2015-09-26 22:15:49 +00:00
|
|
|
if err != nil {
|
2015-09-26 22:20:42 +00:00
|
|
|
log.Fatalf("error: %v", err)
|
2015-09-26 22:15:49 +00:00
|
|
|
}
|
2015-09-27 03:18:33 +00:00
|
|
|
}
|
2015-09-26 22:15:49 +00:00
|
|
|
|
2015-09-27 03:18:33 +00:00
|
|
|
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
|
2015-09-27 03:11:10 +00:00
|
|
|
}
|
|
|
|
|
2015-09-27 03:40:09 +00:00
|
|
|
func read_map(context map[interface{}]interface{}, head string, tail []string) interface{} {
|
2015-09-27 03:11:10 +00:00
|
|
|
value := context[head]
|
2015-09-27 03:18:33 +00:00
|
|
|
if (len(tail) > 0) {
|
2015-09-27 03:40:09 +00:00
|
|
|
return recurse(value, tail[0], tail[1:len(tail)])
|
2015-09-27 03:18:33 +00:00
|
|
|
} else {
|
|
|
|
return value
|
2015-09-27 03:11:10 +00:00
|
|
|
}
|
2015-09-26 22:15:49 +00:00
|
|
|
}
|
2015-09-27 03:40:09 +00:00
|
|
|
|
|
|
|
func recurse(value interface{}, head string, tail []string) interface{} {
|
|
|
|
switch value.(type) {
|
|
|
|
case []interface {}:
|
|
|
|
index, err := strconv.ParseInt(head, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error accessing array: %v", err)
|
|
|
|
}
|
|
|
|
return read_array(value.([]interface {}), index, tail)
|
|
|
|
default:
|
|
|
|
return read_map(value.(map[interface{}]interface{}), head, tail)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func read_array(array []interface {}, head int64, tail[]string) interface{} {
|
|
|
|
value := array[head]
|
|
|
|
if (len(tail) > 0) {
|
|
|
|
return recurse(value, tail[0], tail[1:len(tail)])
|
|
|
|
} else {
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|