|
1 | 1 | package main
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "fmt" |
5 |
| - "gopkg.in/yaml.v2" |
6 |
| - "log" |
7 |
| - "io/ioutil" |
8 |
| - "os" |
9 |
| - "github.com/codegangsta/cli" |
10 |
| - "strings" |
11 |
| - "strconv" |
| 4 | + "fmt" |
| 5 | + "github.com/codegangsta/cli" |
| 6 | + "gopkg.in/yaml.v2" |
| 7 | + "io/ioutil" |
| 8 | + "log" |
| 9 | + "os" |
| 10 | + "strconv" |
| 11 | + "strings" |
12 | 12 | )
|
13 | 13 |
|
14 | 14 | func main() {
|
15 |
| - app := cli.NewApp() |
16 |
| - app.Name = "yaml" |
17 |
| - app.Usage = "command line tool for reading and writing yaml" |
18 |
| - app.Commands = []cli.Command{ |
19 |
| - { |
20 |
| - Name: "read", |
21 |
| - Aliases: []string{"r"}, |
22 |
| - 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", |
23 |
| - Action: read_property, |
24 |
| - }, |
25 |
| - } |
26 |
| - app.Action = read_property |
27 |
| - app.Run(os.Args) |
| 15 | + app := cli.NewApp() |
| 16 | + app.Name = "yaml" |
| 17 | + app.Usage = "command line tool for reading and writing yaml" |
| 18 | + app.Commands = []cli.Command{ |
| 19 | + { |
| 20 | + Name: "read", |
| 21 | + Aliases: []string{"r"}, |
| 22 | + 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", |
| 23 | + Action: read_property, |
| 24 | + }, |
| 25 | + } |
| 26 | + app.Action = read_property |
| 27 | + app.Run(os.Args) |
28 | 28 | }
|
29 | 29 |
|
30 | 30 | func read_property(c *cli.Context) {
|
31 |
| - var parsed_data map[interface{}]interface{} |
32 |
| - read_yaml(c, &parsed_data) |
| 31 | + var parsed_data map[interface{}]interface{} |
| 32 | + read_yaml(c, &parsed_data) |
33 | 33 |
|
34 |
| - var path = c.Args()[1] |
35 |
| - var paths = strings.Split(path, ".") |
| 34 | + var path = c.Args()[1] |
| 35 | + var paths = strings.Split(path, ".") |
36 | 36 |
|
37 |
| - fmt.Println(read_map(parsed_data, paths[0], paths[1:len(paths)])) |
| 37 | + fmt.Println(read_map(parsed_data, paths[0], paths[1:len(paths)])) |
38 | 38 | }
|
39 | 39 |
|
40 | 40 | func read_yaml(c *cli.Context, parsed_data *map[interface{}]interface{}) {
|
41 |
| - if len(c.Args()) == 0 { |
42 |
| - log.Fatalf("Must provide filename") |
43 |
| - } |
44 |
| - var raw_data = read_file(c.Args()[0]) |
| 41 | + if len(c.Args()) == 0 { |
| 42 | + log.Fatalf("Must provide filename") |
| 43 | + } |
| 44 | + var raw_data = read_file(c.Args()[0]) |
45 | 45 |
|
46 |
| - if len(c.Args()) == 1 { |
47 |
| - fmt.Println(string(raw_data[:])) |
48 |
| - os.Exit(0) |
49 |
| - } |
| 46 | + if len(c.Args()) == 1 { |
| 47 | + fmt.Println(string(raw_data[:])) |
| 48 | + os.Exit(0) |
| 49 | + } |
50 | 50 |
|
51 |
| - err := yaml.Unmarshal([]byte(raw_data), &parsed_data) |
52 |
| - if err != nil { |
53 |
| - log.Fatalf("error: %v", err) |
54 |
| - } |
| 51 | + err := yaml.Unmarshal([]byte(raw_data), &parsed_data) |
| 52 | + if err != nil { |
| 53 | + log.Fatalf("error: %v", err) |
| 54 | + } |
55 | 55 | }
|
56 | 56 |
|
57 | 57 | func read_file(filename string) []byte {
|
58 |
| - var raw_data, read_error = ioutil.ReadFile(filename) |
59 |
| - if read_error != nil { |
60 |
| - log.Fatalf("error: %v", read_error) |
61 |
| - } |
62 |
| - return raw_data |
| 58 | + var raw_data, read_error = ioutil.ReadFile(filename) |
| 59 | + if read_error != nil { |
| 60 | + log.Fatalf("error: %v", read_error) |
| 61 | + } |
| 62 | + return raw_data |
63 | 63 | }
|
64 | 64 |
|
65 | 65 | func read_map(context map[interface{}]interface{}, head string, tail []string) interface{} {
|
66 |
| - value := context[head] |
67 |
| - if (len(tail) > 0) { |
68 |
| - return recurse(value, tail[0], tail[1:len(tail)]) |
69 |
| - } else { |
70 |
| - return value |
71 |
| - } |
| 66 | + value := context[head] |
| 67 | + if len(tail) > 0 { |
| 68 | + return recurse(value, tail[0], tail[1:len(tail)]) |
| 69 | + } else { |
| 70 | + return value |
| 71 | + } |
72 | 72 | }
|
73 | 73 |
|
74 | 74 | func recurse(value interface{}, head string, tail []string) interface{} {
|
75 |
| - switch value.(type) { |
76 |
| - case []interface {}: |
77 |
| - index, err := strconv.ParseInt(head, 10, 64) |
78 |
| - if err != nil { |
79 |
| - log.Fatalf("Error accessing array: %v", err) |
80 |
| - } |
81 |
| - return read_array(value.([]interface {}), index, tail) |
82 |
| - default: |
83 |
| - return read_map(value.(map[interface{}]interface{}), head, tail) |
84 |
| - } |
| 75 | + switch value.(type) { |
| 76 | + case []interface{}: |
| 77 | + index, err := strconv.ParseInt(head, 10, 64) |
| 78 | + if err != nil { |
| 79 | + log.Fatalf("Error accessing array: %v", err) |
| 80 | + } |
| 81 | + return read_array(value.([]interface{}), index, tail) |
| 82 | + default: |
| 83 | + return read_map(value.(map[interface{}]interface{}), head, tail) |
| 84 | + } |
85 | 85 | }
|
86 | 86 |
|
87 |
| -func read_array(array []interface {}, head int64, tail[]string) interface{} { |
88 |
| - value := array[head] |
89 |
| - if (len(tail) > 0) { |
90 |
| - return recurse(value, tail[0], tail[1:len(tail)]) |
91 |
| - } else { |
92 |
| - return value |
93 |
| - } |
| 87 | +func read_array(array []interface{}, head int64, tail []string) interface{} { |
| 88 | + value := array[head] |
| 89 | + if len(tail) > 0 { |
| 90 | + return recurse(value, tail[0], tail[1:len(tail)]) |
| 91 | + } else { |
| 92 | + return value |
| 93 | + } |
94 | 94 | }
|
95 |
| - |
0 commit comments