This repository has been archived by the owner on Apr 24, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
config.go
80 lines (68 loc) · 1.85 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
package main
import (
"io/ioutil"
"launchpad.net/goyaml"
)
// Config represents the key-value pairs in a _config.yml 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
}
// 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) {
b, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
return parseConfig(b)
}
func parseConfig(data []byte) (Config, error) {
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.yml 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) {
b, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
return parseDeployConfig(b)
}
func parseDeployConfig(data []byte) (*DeployConfig, error) {
conf := DeployConfig{}
err := goyaml.Unmarshal(data, &conf)
if err != nil {
return nil, err
}
return &conf, nil
}