forked from drone/jkl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
95 lines (81 loc) · 2.11 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
package main
import (
"strings"
"io/ioutil"
"launchpad.net/goyaml"
)
// Config represents the key-value pairs in a _config.yaml file.
// The file is freeform, and thus requires the flexibility of a map.
type Config map[string]interface{}
// Sets a parameter value.
func (c Config) Set(key string, val interface{}) {
c[key] = val
}
// Gets a parameter value.
func (c Config) Get(key string) interface{} {
return c[key]
}
// Gets a parameter value as a string. If none exists return an empty string.
func (c Config) GetString(key string) (str string) {
if v, ok := c[key]; ok {
str = v.(string)
}
return
}
// Gets a parameter value as a string array.
func (c Config) GetStrings(key string) (strs []string) {
if v, ok := c[key]; ok {
switch v.(type) {
case []interface{}:
for _, s := range v.([]interface{}) {
strs = append(strs, s.(string))
}
case string:
for _, s := range strings.Split(v.(string), ",") {
if x := strings.TrimSpace(s); len(x) > 0 {
strs = append(strs, x)
}
}
}
}
return
}
// ParseConfig will parse a YAML file at the given path and return
// a key-value Config structure.
//
// ParseConfig always returns a non-nil map containing all the
// valid YAML parameters found; err describes the first unmarshalling
// error encountered, if any.
func ParseConfig(path string) (Config, error) {
data, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
conf := map[string]interface{}{}
err = goyaml.Unmarshal(data, &conf)
if err != nil {
return nil, err
}
return conf, nil
}
// DeployConfig represents the key-value data in the _jekyll_s3.yaml file
// used for deploying a website to Amazon's S3.
type DeployConfig struct {
Key string `s3_id`
Secret string `s3_secret`
Bucket string `s3_bucket`
}
// ParseDeployConfig will parse a YAML file at the given path and return
// a key-value DeployConfig structure.
func ParseDeployConfig(path string) (*DeployConfig, error) {
data, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
conf := DeployConfig{}
err = goyaml.Unmarshal(data, &conf)
if err != nil {
return nil, err
}
return &conf, nil
}