github.com/echohead/hub@v2.2.1+incompatible/github/config_decoder.go (about)

     1  package github
     2  
     3  import (
     4  	"io"
     5  	"io/ioutil"
     6  
     7  	"github.com/github/hub/Godeps/_workspace/src/github.com/BurntSushi/toml"
     8  	"github.com/github/hub/Godeps/_workspace/src/gopkg.in/yaml.v1"
     9  )
    10  
    11  type configDecoder interface {
    12  	Decode(r io.Reader, c *Config) error
    13  }
    14  
    15  type tomlConfigDecoder struct {
    16  }
    17  
    18  func (t *tomlConfigDecoder) Decode(r io.Reader, c *Config) error {
    19  	_, err := toml.DecodeReader(r, c)
    20  	return err
    21  }
    22  
    23  type yamlConfigDecoder struct {
    24  }
    25  
    26  func (y *yamlConfigDecoder) Decode(r io.Reader, c *Config) error {
    27  	d, err := ioutil.ReadAll(r)
    28  	if err != nil {
    29  		return err
    30  	}
    31  
    32  	yc := make(yamlConfig)
    33  	err = yaml.Unmarshal(d, &yc)
    34  
    35  	if err != nil {
    36  		return err
    37  	}
    38  
    39  	for h, v := range yc {
    40  		vv := v[0]
    41  		host := Host{
    42  			Host:        h,
    43  			User:        vv.User,
    44  			AccessToken: vv.OAuthToken,
    45  			Protocol:    vv.Protocol,
    46  		}
    47  		c.Hosts = append(c.Hosts, host)
    48  	}
    49  
    50  	return nil
    51  }