-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathparsing.go
122 lines (102 loc) · 3.2 KB
/
parsing.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package utility
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"github.com/pkg/errors"
yaml "gopkg.in/yaml.v2"
)
// ReadYAML provides an alternate interface to yaml.Unmarshal that
// reads data from an io.ReadCloser.
func ReadYAML(r io.ReadCloser, target interface{}) error {
defer r.Close()
data, err := ioutil.ReadAll(r)
if err != nil {
return errors.WithStack(err)
}
return errors.WithStack(yaml.Unmarshal(data, target))
}
// ReadYAMLStrict is the same as ReadYAML but uses strict unmarshalling.
func ReadYAMLStrict(r io.ReadCloser, target interface{}) error {
defer r.Close()
data, err := ioutil.ReadAll(r)
if err != nil {
return errors.WithStack(err)
}
return errors.WithStack(yaml.UnmarshalStrict(data, target))
}
// ReadJSON provides an alternate interface to json.Unmarshal that
// reads data from an io.ReadCloser.
func ReadJSON(r io.ReadCloser, target interface{}) error {
defer r.Close()
data, err := ioutil.ReadAll(r)
if err != nil {
return errors.WithStack(err)
}
return errors.WithStack(json.Unmarshal(data, target))
}
// ReadYAMLFile parses yaml into the target argument from the file
// located at the specified path.
func ReadYAMLFile(path string, target interface{}) error {
if !FileExists(path) {
return errors.Errorf("file '%s' does not exist", path)
}
file, err := os.Open(path)
if err != nil {
return errors.Wrapf(err, "invalid file: %s", path)
}
return errors.Wrapf(ReadYAML(file, target), "reading YAML from '%s'", path)
}
// ReadYAMLFileStrict is the same as ReadYAMLFile but uses strict unmarshalling.
func ReadYAMLFileStrict(path string, target interface{}) error {
if !FileExists(path) {
return errors.Errorf("file '%s' does not exist", path)
}
file, err := os.Open(path)
if err != nil {
return errors.Wrapf(err, "invalid file: %s", path)
}
return errors.Wrapf(ReadYAMLStrict(file, target), "reading YAML from '%s'", path)
}
// ReadJSONFile parses json into the target argument from the file
// located at the specified path.
func ReadJSONFile(path string, target interface{}) error {
if !FileExists(path) {
return errors.Errorf("file '%s' does not exist", path)
}
file, err := os.Open(path)
if err != nil {
return errors.Wrapf(err, "invalid file: %s", path)
}
return errors.Wrapf(ReadJSON(file, target), "reading JSON from '%s'", path)
}
// PrintJSON marshals the data to a pretty-printed (indented) string
// and then prints it to standard output.
func PrintJSON(data interface{}) error {
out, err := json.MarshalIndent(data, "", " ")
if err != nil {
return errors.Wrap(err, "writing data")
}
fmt.Println(string(out))
return nil
}
// WriteJSONFile marshals the data into json and writes it into a file
// at the specified path.
func WriteJSONFile(fn string, data interface{}) error {
payload, err := json.Marshal(data)
if err != nil {
return errors.Wrap(err, "constructing JSON")
}
return errors.WithStack(WriteRawFile(fn, payload))
}
// WriteYAMLFile marshals the data into json and writes it into a file
// at the specified path.
func WriteYAMLFile(fn string, data interface{}) error {
payload, err := yaml.Marshal(data)
if err != nil {
return errors.Wrap(err, "constructing YAML")
}
return errors.WithStack(WriteRawFile(fn, payload))
}