mirror of
https://github.com/mikefarah/yq.git
synced 2024-12-19 20:19:04 +00:00
gofmt'ed
This commit is contained in:
parent
ccd95146fe
commit
9869a41122
133
yaml.go
133
yaml.go
@ -1,95 +1,94 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"gopkg.in/yaml.v2"
|
||||
"log"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"github.com/codegangsta/cli"
|
||||
"strings"
|
||||
"strconv"
|
||||
"fmt"
|
||||
"github.com/codegangsta/cli"
|
||||
"gopkg.in/yaml.v2"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
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",
|
||||
Action: read_property,
|
||||
},
|
||||
}
|
||||
app.Action = read_property
|
||||
app.Run(os.Args)
|
||||
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",
|
||||
Action: read_property,
|
||||
},
|
||||
}
|
||||
app.Action = read_property
|
||||
app.Run(os.Args)
|
||||
}
|
||||
|
||||
func read_property(c *cli.Context) {
|
||||
var parsed_data map[interface{}]interface{}
|
||||
read_yaml(c, &parsed_data)
|
||||
var parsed_data map[interface{}]interface{}
|
||||
read_yaml(c, &parsed_data)
|
||||
|
||||
var path = c.Args()[1]
|
||||
var paths = strings.Split(path, ".")
|
||||
var path = c.Args()[1]
|
||||
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{}) {
|
||||
if len(c.Args()) == 0 {
|
||||
log.Fatalf("Must provide filename")
|
||||
}
|
||||
var raw_data = read_file(c.Args()[0])
|
||||
if len(c.Args()) == 0 {
|
||||
log.Fatalf("Must provide filename")
|
||||
}
|
||||
var raw_data = read_file(c.Args()[0])
|
||||
|
||||
if len(c.Args()) == 1 {
|
||||
fmt.Println(string(raw_data[:]))
|
||||
os.Exit(0)
|
||||
}
|
||||
if len(c.Args()) == 1 {
|
||||
fmt.Println(string(raw_data[:]))
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
err := yaml.Unmarshal([]byte(raw_data), &parsed_data)
|
||||
if err != nil {
|
||||
log.Fatalf("error: %v", err)
|
||||
}
|
||||
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
|
||||
var raw_data, read_error = ioutil.ReadFile(filename)
|
||||
if read_error != nil {
|
||||
log.Fatalf("error: %v", read_error)
|
||||
}
|
||||
return raw_data
|
||||
}
|
||||
|
||||
func read_map(context map[interface{}]interface{}, head string, tail []string) interface{} {
|
||||
value := context[head]
|
||||
if (len(tail) > 0) {
|
||||
return recurse(value, tail[0], tail[1:len(tail)])
|
||||
} else {
|
||||
return value
|
||||
}
|
||||
value := context[head]
|
||||
if len(tail) > 0 {
|
||||
return recurse(value, tail[0], tail[1:len(tail)])
|
||||
} else {
|
||||
return value
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
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
|
||||
}
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
|
38
yaml_test.go
38
yaml_test.go
@ -1,13 +1,13 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"gopkg.in/yaml.v2"
|
||||
"fmt"
|
||||
"os"
|
||||
"fmt"
|
||||
"gopkg.in/yaml.v2"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var raw_data = `
|
||||
var raw_data = `
|
||||
a: Easy!
|
||||
b:
|
||||
c: 2
|
||||
@ -17,25 +17,25 @@ b:
|
||||
var parsed_data map[interface{}]interface{}
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
err := yaml.Unmarshal([]byte(raw_data), &parsed_data)
|
||||
if err != nil {
|
||||
fmt.Println("Error parsing yaml: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
err := yaml.Unmarshal([]byte(raw_data), &parsed_data)
|
||||
if err != nil {
|
||||
fmt.Println("Error parsing yaml: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
os.Exit(m.Run())
|
||||
os.Exit(m.Run())
|
||||
}
|
||||
|
||||
func TestRead_map_simple(t *testing.T) {
|
||||
result := read_map(parsed_data, "b", []string{"c"})
|
||||
if( result != 2) {
|
||||
t.Error("Excpted 2 but got ", result)
|
||||
}
|
||||
result := read_map(parsed_data, "b", []string{"c"})
|
||||
if result != 2 {
|
||||
t.Error("Excpted 2 but got ", result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRead_map_array(t *testing.T) {
|
||||
result := read_map(parsed_data, "b", []string{"d", "1"})
|
||||
if( result != 4) {
|
||||
t.Error("Excpted 4 but got ", result)
|
||||
}
|
||||
result := read_map(parsed_data, "b", []string{"d", "1"})
|
||||
if result != 4 {
|
||||
t.Error("Excpted 4 but got ", result)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user