@@ -30,37 +30,59 @@ func main() {
30
30
},
31
31
}
32
32
app .Action = readProperty
33
+ app .Flags = []cli.Flag {
34
+ cli.StringFlag {
35
+ Name : "trim, t" ,
36
+ Value : "true" ,
37
+ Usage : "trim output" ,
38
+ },
39
+ }
33
40
app .Run (os .Args )
34
41
}
35
42
36
43
func readProperty (c * cli.Context ) {
44
+
37
45
var parsedData map [interface {}]interface {}
46
+
38
47
readYaml (c , & parsedData )
39
48
49
+ if len (c .Args ()) == 1 {
50
+ printYaml (parsedData , c .Bool ("trim" ))
51
+ os .Exit (0 )
52
+ }
53
+
40
54
var path = c .Args ()[1 ]
41
55
var paths = strings .Split (path , "." )
42
56
43
- printYaml (readMap (parsedData , paths [0 ], paths [1 :len (paths )]))
57
+ printYaml (readMap (parsedData , paths [0 ], paths [1 :len (paths )]), c . Bool ( "trim" ) )
44
58
}
45
59
46
60
func writeProperty (c * cli.Context ) {
47
61
var parsedData map [interface {}]interface {}
48
62
readYaml (c , & parsedData )
49
63
64
+ if len (c .Args ()) != 3 {
65
+ log .Fatalf ("Must provide <filename> <path_to_update> <value>" )
66
+ }
67
+
50
68
var path = c .Args ()[1 ]
51
69
var paths = strings .Split (path , "." )
52
70
53
71
write (parsedData , paths [0 ], paths [1 :len (paths )], c .Args ()[2 ])
54
72
55
- printYaml (parsedData )
73
+ printYaml (parsedData , c . Bool ( "trim" ) )
56
74
}
57
75
58
- func printYaml (context interface {}) {
76
+ func printYaml (context interface {}, trim bool ) {
59
77
out , err := yaml .Marshal (context )
60
78
if err != nil {
61
79
log .Fatalf ("error printing yaml: %v" , err )
62
80
}
63
- fmt .Println (string (out ))
81
+ outStr := string (out )
82
+ if trim {
83
+ outStr = strings .Trim (outStr , "\n " )
84
+ }
85
+ fmt .Println (outStr )
64
86
}
65
87
66
88
func readYaml (c * cli.Context , parsedData * map [interface {}]interface {}) {
@@ -69,11 +91,6 @@ func readYaml(c *cli.Context, parsedData *map[interface{}]interface{}) {
69
91
}
70
92
var rawData = readFile (c .Args ()[0 ])
71
93
72
- if len (c .Args ()) == 1 {
73
- fmt .Println (string (rawData [:]))
74
- os .Exit (0 )
75
- }
76
-
77
94
err := yaml .Unmarshal ([]byte (rawData ), & parsedData )
78
95
if err != nil {
79
96
log .Fatalf ("error: %v" , err )
0 commit comments