forked from mislav/hub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_decoder.go
51 lines (40 loc) · 878 Bytes
/
config_decoder.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
package github
import (
"io"
"io/ioutil"
"github.com/github/hub/Godeps/_workspace/src/github.com/BurntSushi/toml"
"github.com/github/hub/Godeps/_workspace/src/gopkg.in/yaml.v1"
)
type configDecoder interface {
Decode(r io.Reader, c *Config) error
}
type tomlConfigDecoder struct {
}
func (t *tomlConfigDecoder) Decode(r io.Reader, c *Config) error {
_, err := toml.DecodeReader(r, c)
return err
}
type yamlConfigDecoder struct {
}
func (y *yamlConfigDecoder) Decode(r io.Reader, c *Config) error {
d, err := ioutil.ReadAll(r)
if err != nil {
return err
}
yc := make(yamlConfig)
err = yaml.Unmarshal(d, &yc)
if err != nil {
return err
}
for h, v := range yc {
vv := v[0]
host := &Host{
Host: h,
User: vv.User,
AccessToken: vv.OAuthToken,
Protocol: vv.Protocol,
}
c.Hosts = append(c.Hosts, host)
}
return nil
}