2015-09-26 22:15:49 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-09-28 02:00:38 +00:00
|
|
|
"fmt"
|
|
|
|
"github.com/codegangsta/cli"
|
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2015-09-26 22:15:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2015-09-28 02:00:38 +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"},
|
|
|
|
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-28 01:58:56 +00:00
|
|
|
Action: readProperty,
|
2015-09-28 02:00:38 +00:00
|
|
|
},
|
|
|
|
}
|
2015-09-28 01:58:56 +00:00
|
|
|
app.Action = readProperty
|
2015-09-28 02:00:38 +00:00
|
|
|
app.Run(os.Args)
|
2015-09-26 23:38:44 +00:00
|
|
|
}
|
|
|
|
|
2015-09-28 01:58:56 +00:00
|
|
|
func readProperty(c *cli.Context) {
|
|
|
|
var parsedData map[interface{}]interface{}
|
|
|
|
readYaml(c, &parsedData)
|
2015-09-27 03:18:33 +00:00
|
|
|
|
2015-09-28 02:00:38 +00:00
|
|
|
var path = c.Args()[1]
|
|
|
|
var paths = strings.Split(path, ".")
|
2015-09-27 03:18:33 +00:00
|
|
|
|
2015-09-29 00:56:12 +00:00
|
|
|
printYaml(readMap(parsedData, paths[0], paths[1:len(paths)]))
|
|
|
|
}
|
|
|
|
|
|
|
|
func printYaml(context interface{}) {
|
|
|
|
out, err := yaml.Marshal(context)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("error printing yaml: %v", err)
|
|
|
|
}
|
|
|
|
fmt.Println(string(out))
|
2015-09-27 03:18:33 +00:00
|
|
|
}
|
|
|
|
|
2015-09-28 01:58:56 +00:00
|
|
|
func readYaml(c *cli.Context, parsedData *map[interface{}]interface{}) {
|
2015-09-28 02:00:38 +00:00
|
|
|
if len(c.Args()) == 0 {
|
|
|
|
log.Fatalf("Must provide filename")
|
|
|
|
}
|
2015-09-28 01:58:56 +00:00
|
|
|
var rawData = readFile(c.Args()[0])
|
2015-09-26 22:20:42 +00:00
|
|
|
|
2015-09-28 02:00:38 +00:00
|
|
|
if len(c.Args()) == 1 {
|
2015-09-28 01:58:56 +00:00
|
|
|
fmt.Println(string(rawData[:]))
|
2015-09-28 02:00:38 +00:00
|
|
|
os.Exit(0)
|
|
|
|
}
|
2015-09-26 22:15:49 +00:00
|
|
|
|
2015-09-28 01:58:56 +00:00
|
|
|
err := yaml.Unmarshal([]byte(rawData), &parsedData)
|
2015-09-28 02:00:38 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("error: %v", err)
|
|
|
|
}
|
2015-09-27 03:18:33 +00:00
|
|
|
}
|
2015-09-26 22:15:49 +00:00
|
|
|
|
2015-09-28 01:58:56 +00:00
|
|
|
func readFile(filename string) []byte {
|
|
|
|
var rawData, readError = ioutil.ReadFile(filename)
|
|
|
|
if readError != nil {
|
|
|
|
log.Fatalf("error: %v", readError)
|
2015-09-28 02:00:38 +00:00
|
|
|
}
|
2015-09-28 01:58:56 +00:00
|
|
|
return rawData
|
2015-09-27 03:11:10 +00:00
|
|
|
}
|
|
|
|
|
2015-09-28 01:58:56 +00:00
|
|
|
func readMap(context map[interface{}]interface{}, head string, tail []string) interface{} {
|
2015-09-28 02:00:38 +00:00
|
|
|
value := context[head]
|
|
|
|
if len(tail) > 0 {
|
|
|
|
return recurse(value, tail[0], tail[1:len(tail)])
|
|
|
|
}
|
2015-09-28 01:58:56 +00:00
|
|
|
return value
|
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{} {
|
2015-09-28 02:00:38 +00:00
|
|
|
switch value.(type) {
|
|
|
|
case []interface{}:
|
|
|
|
index, err := strconv.ParseInt(head, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error accessing array: %v", err)
|
|
|
|
}
|
2015-09-28 01:58:56 +00:00
|
|
|
return readArray(value.([]interface{}), index, tail)
|
2015-09-28 02:00:38 +00:00
|
|
|
default:
|
2015-09-28 01:58:56 +00:00
|
|
|
return readMap(value.(map[interface{}]interface{}), head, tail)
|
2015-09-28 02:00:38 +00:00
|
|
|
}
|
2015-09-27 03:40:09 +00:00
|
|
|
}
|
|
|
|
|
2015-09-28 01:58:56 +00:00
|
|
|
func readArray(array []interface{}, head int64, tail []string) interface{} {
|
2015-09-28 02:00:38 +00:00
|
|
|
value := array[head]
|
|
|
|
if len(tail) > 0 {
|
|
|
|
return recurse(value, tail[0], tail[1:len(tail)])
|
|
|
|
}
|
2015-09-28 01:58:56 +00:00
|
|
|
return value
|
2015-09-27 03:40:09 +00:00
|
|
|
}
|