-
Notifications
You must be signed in to change notification settings - Fork 28
/
config.go
137 lines (111 loc) · 2.58 KB
/
config.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
package draft
import (
"bytes"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"
"gopkg.in/yaml.v2"
)
// Opt is a function to define a configuration property.
type Opt func(cfg *Config)
// Verbose sets verbose output.
func Verbose(enable bool) func(*Config) {
return func(cfg *Config) {
cfg.verbose = enable
}
}
// BottomTop sets the graph layout.
func BottomTop(enable bool) func(*Config) {
return func(cfg *Config) {
cfg.bottomTop = enable
}
}
// URI sets the input YAML definition file.
// Can be also an HTTP URL.
func URI(s string) func(*Config) {
return func(cfg *Config) {
if !strings.HasPrefix(s, "http") {
cfg.uri, _ = filepath.Abs(s)
} else {
cfg.uri = s
}
}
}
// IconsPath sets the custom icons path.
func IconsPath(s string) func(*Config) {
return func(cfg *Config) {
cfg.iconsPath, _ = filepath.Abs(strings.TrimSpace(s))
os.Mkdir(filepath.Join(cfg.iconsPath, "default"), os.ModePerm)
}
}
// ShowImpl show the cloud provider implementation.
func ShowImpl(show bool) func(*Config) {
return func(cfg *Config) {
cfg.showImpl = show
}
}
// Config defines the 'draft' configuration.
type Config struct {
bottomTop bool
verbose bool
showImpl bool
iconsPath string
uri string
}
// NewConfig create a configuration
// with the specified attributes.
func NewConfig(opts ...Opt) Config {
res := Config{}
for _, op := range opts {
op(&res)
}
return res
}
// Load a YAML from the config info.
func Load(cfg Config) (Design, error) {
const bytesLimit = 500 * 1024
if strings.HasPrefix(cfg.uri, "http") {
body, err := getURI(cfg.uri, bytesLimit)
if err != nil {
return Design{}, err
}
return decodeYAML(body)
}
body, err := getFILE(cfg.uri, bytesLimit)
if err != nil {
return Design{}, err
}
return decodeYAML(body)
}
// getURI fetch data (with limit) from an HTTP URL
func getURI(uri string, limit int64) ([]byte, error) {
response, err := http.Get(uri)
if err != nil {
return nil, err
}
defer response.Body.Close()
return ioutil.ReadAll(io.LimitReader(response.Body, limit))
}
// getFILE fetch data (with limit) from an file
func getFILE(fin string, limit int64) ([]byte, error) {
file, err := os.Open(fin)
if err != nil {
return nil, err
}
defer file.Close()
return ioutil.ReadAll(io.LimitReader(file, limit))
}
// decodeYAML decode a YAML to return a Design struct.
func decodeYAML(dat []byte) (Design, error) {
res := Design{}
// Init new YAML decode
d := yaml.NewDecoder(bytes.NewReader(dat))
// Start YAML decoding from file
if err := d.Decode(&res); err != nil {
return res, err
}
return res, nil
}