@@ -2,6 +2,7 @@ package utility
2
2
3
3
import (
4
4
"encoding/json"
5
+ "fmt"
5
6
"io"
6
7
"io/ioutil"
7
8
"os"
@@ -10,24 +11,30 @@ import (
10
11
yaml "gopkg.in/yaml.v2"
11
12
)
12
13
14
+ // ReadYAML provides an alternate interface to yaml.Unmarshal that
15
+ // reads data from an io.ReadCloser.
13
16
func ReadYAML (r io.ReadCloser , target interface {}) error {
14
17
defer r .Close ()
15
- bytes , err := ioutil .ReadAll (r )
18
+ data , err := ioutil .ReadAll (r )
16
19
if err != nil {
17
20
return errors .WithStack (err )
18
21
}
19
- return errors .WithStack (yaml .Unmarshal (bytes , data ))
22
+ return errors .WithStack (yaml .Unmarshal (data , target ))
20
23
}
21
24
25
+ // ReadJSON provides an alternate interface to json.Unmarshal that
26
+ // reads data from an io.ReadCloser.
22
27
func ReadJSON (r io.ReadCloser , target interface {}) error {
23
28
defer r .Close ()
24
- bytes , err := ioutil .ReadAll (r )
29
+ data , err := ioutil .ReadAll (r )
25
30
if err != nil {
26
31
return errors .WithStack (err )
27
32
}
28
- return errors .WithStack (json .Unmarshal (bytes , data ))
33
+ return errors .WithStack (json .Unmarshal (data , target ))
29
34
}
30
35
36
+ // ReadYAMLFile parses yaml into the target argument from the file
37
+ // located at the specifed path.
31
38
func ReadYAMLFile (path string , target interface {}) error {
32
39
if ! FileExists (path ) {
33
40
return errors .Errorf ("file '%s' does not exist" , path )
@@ -38,9 +45,11 @@ func ReadYAMLFile(path string, target interface{}) error {
38
45
return errors .Wrapf (err , "invalid file: %s" , path )
39
46
}
40
47
41
- return errors .Wrap (ReadYAML (file , target ), "problem reading yaml from '%s'" , path )
48
+ return errors .Wrapf (ReadYAML (file , target ), "problem reading yaml from '%s'" , path )
42
49
}
43
50
51
+ // ReadJSONFile parses json into the target argument from the file
52
+ // located at the specifed path.
44
53
func ReadJSONFile (path string , target interface {}) error {
45
54
if ! FileExists (path ) {
46
55
return errors .Errorf ("file '%s' does not exist" , path )
@@ -51,27 +60,39 @@ func ReadJSONFile(path string, target interface{}) error {
51
60
return errors .Wrapf (err , "invalid file: %s" , path )
52
61
}
53
62
54
- return errors .Wrap (ReadYAML (file , target ), "problem reading json from '%s'" , path )
63
+ return errors .Wrapf (ReadYAML (file , target ), "problem reading json from '%s'" , path )
55
64
}
56
65
57
- func WriteJSON (out io.Writer , data interface {}) error {
58
- out , err := json .Marshal (data )
66
+ // PrintJSON marshals the data to a pretty-printed (indented) string
67
+ // and then prints it to standard output.
68
+ func PrintJSON (data interface {}) error {
69
+ out , err := json .MarshalIndent (data , "" , " " )
59
70
if err != nil {
60
- return errors .Wrap (err , "problem writing JSON " )
71
+ return errors .Wrap (err , "problem writing data " )
61
72
}
62
73
74
+ fmt .Println (string (out ))
63
75
return nil
64
76
}
65
77
78
+ // WriteJSONFile marshals the data into json and writes it into a file
79
+ // at the specified path.
66
80
func WriteJSONFile (fn string , data interface {}) error {
67
- file , err := os . Create ( fn )
81
+ payload , err := json . Marshal ( data )
68
82
if err != nil {
69
- return errors .Wrapf (err , "problem creating file '%s'" , fn )
83
+ return errors .Wrap (err , "problem constructing JSON" )
70
84
}
71
85
72
- err = WriteJSON ( file , data )
73
- if err != nil {
86
+ return errors . WithStack ( WriteRawFile ( fn , payload ) )
87
+ }
74
88
89
+ // WriteYAMLFile marshals the data into json and writes it into a file
90
+ // at the specified path.
91
+ func WriteYAMLFile (fn string , data interface {}) error {
92
+ payload , err := yaml .Marshal (data )
93
+ if err != nil {
94
+ return errors .Wrap (err , "problem constructing YAML" )
75
95
}
76
96
97
+ return errors .WithStack (WriteRawFile (fn , payload ))
77
98
}
0 commit comments