This commit is contained in:
mfarah 2015-09-28 12:00:38 +10:00
parent ccd95146fe
commit 9869a41122
2 changed files with 85 additions and 86 deletions

133
yaml.go
View File

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

View File

@ -1,13 +1,13 @@
package main package main
import ( import (
"testing" "fmt"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
"fmt" "os"
"os" "testing"
) )
var raw_data = ` var raw_data = `
a: Easy! a: Easy!
b: b:
c: 2 c: 2
@ -17,25 +17,25 @@ b:
var parsed_data map[interface{}]interface{} var parsed_data map[interface{}]interface{}
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
err := yaml.Unmarshal([]byte(raw_data), &parsed_data) err := yaml.Unmarshal([]byte(raw_data), &parsed_data)
if err != nil { if err != nil {
fmt.Println("Error parsing yaml: %v", err) fmt.Println("Error parsing yaml: %v", err)
os.Exit(1) os.Exit(1)
} }
os.Exit(m.Run()) os.Exit(m.Run())
} }
func TestRead_map_simple(t *testing.T) { func TestRead_map_simple(t *testing.T) {
result := read_map(parsed_data, "b", []string{"c"}) result := read_map(parsed_data, "b", []string{"c"})
if( result != 2) { if result != 2 {
t.Error("Excpted 2 but got ", result) t.Error("Excpted 2 but got ", result)
} }
} }
func TestRead_map_array(t *testing.T) { func TestRead_map_array(t *testing.T) {
result := read_map(parsed_data, "b", []string{"d", "1"}) result := read_map(parsed_data, "b", []string{"d", "1"})
if( result != 4) { if result != 4 {
t.Error("Excpted 4 but got ", result) t.Error("Excpted 4 but got ", result)
} }
} }