зеркало из https://github.com/mislav/hub.git
Load yaml config
This commit is contained in:
Родитель
0a7ee90c51
Коммит
0f60c59615
|
@ -14,7 +14,7 @@ func (c *TestConfigs) TearDown() {
|
|||
os.RemoveAll(c.Path)
|
||||
}
|
||||
|
||||
func SetupTestConfigs() *TestConfigs {
|
||||
func SetupTomlTestConfig() *TestConfigs {
|
||||
file, _ := ioutil.TempFile("", "test-gh-config-")
|
||||
|
||||
content := `[[hosts]]
|
||||
|
@ -27,3 +27,17 @@ func SetupTestConfigs() *TestConfigs {
|
|||
|
||||
return &TestConfigs{file.Name()}
|
||||
}
|
||||
|
||||
func SetupTestConfigs() *TestConfigs {
|
||||
file, _ := ioutil.TempFile("", "test-gh-config-")
|
||||
|
||||
content := `---
|
||||
github.com:
|
||||
user: jingweno
|
||||
oauth_token: 123
|
||||
protocol: http`
|
||||
ioutil.WriteFile(file.Name(), []byte(content), os.ModePerm)
|
||||
os.Setenv("GH_CONFIG", file.Name())
|
||||
|
||||
return &TestConfigs{file.Name()}
|
||||
}
|
||||
|
|
|
@ -16,6 +16,14 @@ var (
|
|||
defaultConfigsFile = filepath.Join(os.Getenv("HOME"), ".config", "hub")
|
||||
)
|
||||
|
||||
type yamlHost struct {
|
||||
User string `yaml:"user"`
|
||||
OAuthToken string `yaml:"oauth_token"`
|
||||
Protocol string `yaml:"protocol"`
|
||||
}
|
||||
|
||||
type yamlConfig map[string]yamlHost
|
||||
|
||||
type Host struct {
|
||||
Host string `toml:"host"`
|
||||
User string `toml:"user"`
|
||||
|
@ -160,7 +168,7 @@ func CurrentConfig() *Config {
|
|||
|
||||
err := newConfigService().Load(configsFile(), c)
|
||||
if err != nil {
|
||||
// load from YAML
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return c
|
||||
|
|
|
@ -9,25 +9,42 @@ import (
|
|||
)
|
||||
|
||||
type configDecoder interface {
|
||||
Decode(r io.Reader, v interface{}) error
|
||||
Decode(r io.Reader, c *Config) error
|
||||
}
|
||||
|
||||
type tomlConfigDecoder struct {
|
||||
}
|
||||
|
||||
func (t *tomlConfigDecoder) Decode(r io.Reader, v interface{}) error {
|
||||
_, err := toml.DecodeReader(r, v)
|
||||
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, v interface{}) error {
|
||||
func (y *yamlConfigDecoder) Decode(r io.Reader, c *Config) error {
|
||||
d, err := ioutil.ReadAll(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return yaml.Unmarshal(d, v)
|
||||
yc := make(yamlConfig)
|
||||
err = yaml.Unmarshal(d, &yc)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for h, v := range yc {
|
||||
host := Host{
|
||||
Host: h,
|
||||
User: v.User,
|
||||
AccessToken: v.OAuthToken,
|
||||
Protocol: v.Protocol,
|
||||
}
|
||||
c.Hosts = append(c.Hosts, host)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -8,22 +8,31 @@ import (
|
|||
)
|
||||
|
||||
type configEncoder interface {
|
||||
Encode(w io.Writer, v interface{}) error
|
||||
Encode(w io.Writer, c *Config) error
|
||||
}
|
||||
|
||||
type tomlConfigEncoder struct {
|
||||
}
|
||||
|
||||
func (t *tomlConfigEncoder) Encode(w io.Writer, v interface{}) error {
|
||||
func (t *tomlConfigEncoder) Encode(w io.Writer, c *Config) error {
|
||||
enc := toml.NewEncoder(w)
|
||||
return enc.Encode(v)
|
||||
return enc.Encode(c)
|
||||
}
|
||||
|
||||
type yamlConfigEncoder struct {
|
||||
}
|
||||
|
||||
func (y *yamlConfigEncoder) Encode(w io.Writer, v interface{}) error {
|
||||
d, err := yaml.Marshal(v)
|
||||
func (y *yamlConfigEncoder) Encode(w io.Writer, c *Config) error {
|
||||
yc := make(yamlConfig)
|
||||
for _, h := range c.Hosts {
|
||||
yc[h.Host] = yamlHost{
|
||||
User: h.User,
|
||||
OAuthToken: h.AccessToken,
|
||||
Protocol: h.Protocol,
|
||||
}
|
||||
}
|
||||
|
||||
d, err := yaml.Marshal(yc)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -10,12 +10,16 @@ import (
|
|||
"github.com/github/hub/fixtures"
|
||||
)
|
||||
|
||||
func TestConfigService_Load(t *testing.T) {
|
||||
testConfig := fixtures.SetupTestConfigs()
|
||||
func TestConfigService_TomlLoad(t *testing.T) {
|
||||
testConfig := fixtures.SetupTomlTestConfig()
|
||||
defer testConfig.TearDown()
|
||||
|
||||
cc := &Config{}
|
||||
err := newConfigService().Load(testConfig.Path, cc)
|
||||
cs := &configService{
|
||||
Encoder: &tomlConfigEncoder{},
|
||||
Decoder: &tomlConfigDecoder{},
|
||||
}
|
||||
err := cs.Load(testConfig.Path, cc)
|
||||
assert.Equal(t, nil, err)
|
||||
|
||||
assert.Equal(t, 1, len(cc.Hosts))
|
||||
|
@ -26,7 +30,27 @@ func TestConfigService_Load(t *testing.T) {
|
|||
assert.Equal(t, "http", host.Protocol)
|
||||
}
|
||||
|
||||
func TestConfigService_Save(t *testing.T) {
|
||||
func TestConfigService_YamlLoad(t *testing.T) {
|
||||
testConfig := fixtures.SetupTestConfigs()
|
||||
defer testConfig.TearDown()
|
||||
|
||||
cc := &Config{}
|
||||
cs := &configService{
|
||||
Encoder: &yamlConfigEncoder{},
|
||||
Decoder: &yamlConfigDecoder{},
|
||||
}
|
||||
err := cs.Load(testConfig.Path, cc)
|
||||
assert.Equal(t, nil, err)
|
||||
|
||||
assert.Equal(t, 1, len(cc.Hosts))
|
||||
host := cc.Hosts[0]
|
||||
assert.Equal(t, "github.com", host.Host)
|
||||
assert.Equal(t, "jingweno", host.User)
|
||||
assert.Equal(t, "123", host.AccessToken)
|
||||
assert.Equal(t, "http", host.Protocol)
|
||||
}
|
||||
|
||||
func TestConfigService_TomlSave(t *testing.T) {
|
||||
file, _ := ioutil.TempFile("", "test-gh-config-")
|
||||
defer os.RemoveAll(file.Name())
|
||||
|
||||
|
@ -38,7 +62,11 @@ func TestConfigService_Save(t *testing.T) {
|
|||
}
|
||||
c := Config{Hosts: []Host{host}}
|
||||
|
||||
err := newConfigService().Save(file.Name(), &c)
|
||||
cs := &configService{
|
||||
Encoder: &tomlConfigEncoder{},
|
||||
Decoder: &tomlConfigDecoder{},
|
||||
}
|
||||
err := cs.Save(file.Name(), &c)
|
||||
assert.Equal(t, nil, err)
|
||||
|
||||
b, _ := ioutil.ReadFile(file.Name())
|
||||
|
@ -49,3 +77,30 @@ func TestConfigService_Save(t *testing.T) {
|
|||
protocol = "https"`
|
||||
assert.Equal(t, content, strings.TrimSpace(string(b)))
|
||||
}
|
||||
|
||||
func TestConfigService_YamlSave(t *testing.T) {
|
||||
file, _ := ioutil.TempFile("", "test-gh-config-")
|
||||
defer os.RemoveAll(file.Name())
|
||||
|
||||
host := Host{
|
||||
Host: "github.com",
|
||||
User: "jingweno",
|
||||
AccessToken: "123",
|
||||
Protocol: "https",
|
||||
}
|
||||
c := Config{Hosts: []Host{host}}
|
||||
|
||||
cs := &configService{
|
||||
Encoder: &yamlConfigEncoder{},
|
||||
Decoder: &yamlConfigDecoder{},
|
||||
}
|
||||
err := cs.Save(file.Name(), &c)
|
||||
assert.Equal(t, nil, err)
|
||||
|
||||
b, _ := ioutil.ReadFile(file.Name())
|
||||
content := `github.com:
|
||||
user: jingweno
|
||||
oauth_token: "123"
|
||||
protocol: https`
|
||||
assert.Equal(t, content, strings.TrimSpace(string(b)))
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче