File: config_encoder.go

package info (click to toggle)
hub 2.14.2~ds1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,376 kB
  • sloc: sh: 1,049; ruby: 857; makefile: 89
file content (52 lines) | stat: -rw-r--r-- 838 bytes parent folder | download | duplicates (2)
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
package github

import (
	"io"

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

type configEncoder interface {
	Encode(w io.Writer, c *Config) error
}

type tomlConfigEncoder struct {
}

func (t *tomlConfigEncoder) Encode(w io.Writer, c *Config) error {
	enc := toml.NewEncoder(w)
	return enc.Encode(c)
}

type yamlConfigEncoder struct {
}

func (y *yamlConfigEncoder) Encode(w io.Writer, c *Config) error {
	yc := yaml.MapSlice{}
	for _, h := range c.Hosts {
		yc = append(yc, yaml.MapItem{
			Key: h.Host,
			Value: []yamlHost{
				{
					User:       h.User,
					OAuthToken: h.AccessToken,
					Protocol:   h.Protocol,
					UnixSocket: h.UnixSocket,
				},
			},
		})
	}

	d, err := yaml.Marshal(yc)
	if err != nil {
		return err
	}

	n, err := w.Write(d)
	if err == nil && n < len(d) {
		err = io.ErrShortWrite
	}

	return err
}