This repository has been archived by the owner on Mar 18, 2019. It is now read-only.
/
parse.go
154 lines (138 loc) · 2.97 KB
/
parse.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package config
import (
"bufio"
"bytes"
"io"
"io/ioutil"
"os"
"strings"
"gopkg.in/yaml.v2"
)
// Parse parses the configuration from bytes b.
func Parse(r io.Reader) (*Config, error) {
out, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
return ParseBytes(out)
}
// ParseBytes parses the configuration from bytes b.
func ParseBytes(b []byte) (*Config, error) {
out := new(Config)
err := yaml.Unmarshal(b, out)
if err != nil {
return nil, err
}
return out, nil
}
// ParseString parses the configuration from string s.
func ParseString(s string) (*Config, error) {
return ParseBytes(
[]byte(s),
)
}
// ParseFile parses the configuration from path p.
func ParseFile(p string) (*Config, error) {
f, err := os.Open(p)
if err != nil {
return nil, err
}
defer f.Close()
return Parse(f)
}
//
// parse multiple yaml documents delimited by ---
//
// ParseMulti parses the configurations from bytes b.
func ParseMulti(r io.Reader) ([]*Config, error) {
var list []*Config
scanner := bufio.NewScanner(r)
row := 0
buf := new(bytes.Buffer)
for scanner.Scan() {
row++
txt := scanner.Text()
if strings.HasPrefix(txt, "---") && row != 1 {
out, err := Parse(buf)
if err != nil {
return nil, err
}
list = append(list, out)
buf.Reset()
} else {
buf.WriteString(txt)
buf.WriteByte('\n')
}
}
out, err := Parse(buf)
if err != nil {
return nil, err
}
list = append(list, out)
return list, nil
}
// ParseMultiBytes parses the configurations from bytes b.
func ParseMultiBytes(b []byte) ([]*Config, error) {
return ParseMulti(
bytes.NewBuffer(b),
)
}
// ParseMultiString parses the configurations from string s.
func ParseMultiString(s string) ([]*Config, error) {
return ParseMultiBytes(
[]byte(s),
)
}
// ParseMultiFile parses the configurations from path p.
func ParseMultiFile(p string) ([]*Config, error) {
f, err := os.Open(p)
if err != nil {
return nil, err
}
defer f.Close()
return ParseMulti(f)
}
//
// extract yaml documents delimited by ---
//
// ParseDocuments returns the yaml documents.
func ParseDocuments(r io.Reader) ([]string, error) {
var list []string
scanner := bufio.NewScanner(r)
row := 0
buf := new(bytes.Buffer)
for scanner.Scan() {
row++
txt := scanner.Text()
if strings.HasPrefix(txt, "---") && row != 1 {
list = append(list, buf.String())
buf.Reset()
} else {
buf.WriteString(txt)
buf.WriteByte('\n')
}
}
list = append(list, buf.String())
return list, nil
}
// ParseDocumentsBytes returns the yaml documents.
func ParseDocumentsBytes(b []byte) ([]string, error) {
return ParseDocuments(
bytes.NewBuffer(b),
)
}
// ParseDocumentsString returns the yaml documents.
func ParseDocumentsString(s string) ([]string, error) {
return ParseDocumentsBytes(
[]byte(s),
)
}
// ParseDocumentsFile returns the yaml documents from file p.
func ParseDocumentsFile(p string) ([]string, error) {
f, err := os.Open(p)
if err != nil {
return nil, err
}
defer f.Close()
return ParseDocuments(f)
}