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

     1  package github
     2  
     3  import (
     4  	"io"
     5  
     6  	"github.com/github/hub/Godeps/_workspace/src/github.com/BurntSushi/toml"
     7  	"github.com/github/hub/Godeps/_workspace/src/gopkg.in/yaml.v1"
     8  )
     9  
    10  type configEncoder interface {
    11  	Encode(w io.Writer, c *Config) error
    12  }
    13  
    14  type tomlConfigEncoder struct {
    15  }
    16  
    17  func (t *tomlConfigEncoder) Encode(w io.Writer, c *Config) error {
    18  	enc := toml.NewEncoder(w)
    19  	return enc.Encode(c)
    20  }
    21  
    22  type yamlConfigEncoder struct {
    23  }
    24  
    25  func (y *yamlConfigEncoder) Encode(w io.Writer, c *Config) error {
    26  	yc := make(yamlConfig)
    27  	for _, h := range c.Hosts {
    28  		yc[h.Host] = []yamlHost{
    29  			{
    30  				User:       h.User,
    31  				OAuthToken: h.AccessToken,
    32  				Protocol:   h.Protocol,
    33  			},
    34  		}
    35  	}
    36  
    37  	d, err := yaml.Marshal(yc)
    38  	if err != nil {
    39  		return err
    40  	}
    41  
    42  	n, err := w.Write(d)
    43  	if err == nil && n < len(d) {
    44  		err = io.ErrShortWrite
    45  	}
    46  
    47  	return err
    48  }