File: config_decoder.go

package info (click to toggle)
hub 2.7.0~ds1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,176 kB
  • sloc: sh: 1,072; ruby: 825; makefile: 74
file content (55 lines) | stat: -rw-r--r-- 855 bytes parent folder | download
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
package github

import (
	"io"
	"io/ioutil"

	"github.com/BurntSushi/toml"
	"gopkg.in/yaml.v2"
)

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 {
		if len(v) < 1 {
			continue
		}
		vv := v[0]
		host := &Host{
			Host:        h,
			User:        vv.User,
			AccessToken: vv.OAuthToken,
			Protocol:    vv.Protocol,
			UnixSocket:  vv.UnixSocket,
		}
		c.Hosts = append(c.Hosts, host)
	}

	return nil
}