From 47a42322c714cb4fa29def330c22c77ca4ec7a2e Mon Sep 17 00:00:00 2001 From: Jingwen Owen Ou Date: Wed, 29 May 2013 14:44:28 -0700 Subject: [PATCH] Allow multiple configs --- commands/pull_request.go | 2 +- config/config.go | 53 +++++++++++++++++++++++++++++++++------- config/config_test.go | 21 +++++++++++++--- github/github.go | 10 +++++--- github/github_test.go | 18 -------------- 5 files changed, 68 insertions(+), 36 deletions(-) delete mode 100644 github/github_test.go diff --git a/commands/pull_request.go b/commands/pull_request.go index 5a9be028..e8dcb0b5 100644 --- a/commands/pull_request.go +++ b/commands/pull_request.go @@ -63,7 +63,7 @@ func pullRequest(cmd *Command, args []string) { } params := github.PullRequestParams{title, body, repo.Base, repo.Head} - gh := github.NewGitHub() + gh := github.New() pullRequestResponse, err := gh.CreatePullRequest(repo.Project, params) utils.Check(err) diff --git a/config/config.go b/config/config.go index 0201409e..b820b74b 100644 --- a/config/config.go +++ b/config/config.go @@ -3,6 +3,7 @@ package config import ( "bufio" "encoding/json" + "errors" "os" "path/filepath" ) @@ -18,27 +19,43 @@ func init() { DefaultFile = filepath.Join(os.Getenv("HOME"), ".config", "gh") } -func Load() (*Config, error) { - return loadFrom(DefaultFile) +func Load(user string) (*Config, error) { + configs, err := loadFrom(DefaultFile) + if err != nil { + return nil, err + } + + for _, c := range configs { + if c.User == user { + return &c, nil + } + } + + return nil, errors.New("There's no matching config for user: " + user) } -func loadFrom(filename string) (*Config, error) { +func loadFrom(filename string) ([]Config, error) { f, err := os.Open(filename) if err != nil { return nil, err } + + return doLoadFrom(f) +} + +func doLoadFrom(f *os.File) ([]Config, error) { defer f.Close() reader := bufio.NewReader(f) dec := json.NewDecoder(reader) - var c Config - err = dec.Decode(&c) + var c []Config + err := dec.Decode(&c) if err != nil { return nil, err } - return &c, nil + return c, nil } func Save(config Config) error { @@ -46,6 +63,8 @@ func Save(config Config) error { } func saveTo(filename string, config Config) error { + configs, _ := loadFrom(filename) + err := os.MkdirAll(filepath.Dir(filename), 0771) if err != nil { return err @@ -55,10 +74,26 @@ func saveTo(filename string, config Config) error { if err != nil { return err } + + var foundConfig *Config + for _, c := range configs { + if c.User == config.User { + foundConfig = &c + break + } + } + if foundConfig == nil { + configs = append(configs, config) + } else { + foundConfig.Token = config.Token + } + + return doSaveTo(f, configs) +} + +func doSaveTo(f *os.File, configs []Config) error { defer f.Close() enc := json.NewEncoder(f) - enc.Encode(config) - - return nil + return enc.Encode(configs) } diff --git a/config/config_test.go b/config/config_test.go index ecad20d8..cd938d19 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -10,13 +10,26 @@ import ( func TestSave(t *testing.T) { config := Config{"jingweno", "123"} file := "./test_support/test" + defer os.RemoveAll(filepath.Dir(file)) + err := saveTo(file, config) + assert.Equal(t, nil, err) + configs, err := loadFrom(file) assert.Equal(t, nil, err) + assert.Equal(t, 1, len(configs)) + assert.Equal(t, "jingweno", configs[0].User) + assert.Equal(t, "123", configs[0].Token) - newConfig, _ := loadFrom(file) - assert.Equal(t, "jingweno", newConfig.User) - assert.Equal(t, "123", newConfig.Token) + newConfig := Config{"foo", "456"} + err = saveTo(file, newConfig) + assert.Equal(t, nil, err) - os.RemoveAll(filepath.Dir(file)) + configs, err = loadFrom(file) + assert.Equal(t, nil, err) + assert.Equal(t, 2, len(configs)) + assert.Equal(t, "jingweno", configs[0].User) + assert.Equal(t, "123", configs[0].Token) + assert.Equal(t, "foo", configs[1].User) + assert.Equal(t, "456", configs[1].Token) } diff --git a/github/github.go b/github/github.go index 06a6ee06..a758b2de 100644 --- a/github/github.go +++ b/github/github.go @@ -47,11 +47,12 @@ type Authorization struct { NoteUrl string `josn:"note_url"` } -func NewGitHub() *GitHub { - config, _ := config.Load() +func New() *GitHub { + project := CurrentProject() + config, err := config.Load(project.Owner) var user, auth string - if config != nil { + if err == nil { user = config.User auth = config.Token } @@ -60,7 +61,7 @@ func NewGitHub() *GitHub { auth = fmt.Sprintf("token %s", auth) } - return &GitHub{&http.Client{}, user, "", auth} + return &GitHub{&http.Client{}, user, "", auth, project} } func hashAuth(u, p string) string { @@ -73,6 +74,7 @@ type GitHub struct { User string Password string Authorization string + Project *Project } func (gh *GitHub) performBasicAuth() error { diff --git a/github/github_test.go b/github/github_test.go deleted file mode 100644 index f7aba481..00000000 --- a/github/github_test.go +++ /dev/null @@ -1,18 +0,0 @@ -package github - -import ( - "github.com/bmizerany/assert" - "github.com/jingweno/gh/config" - "net/http" - "testing" -) - -func _TestCreatePullRequest(t *testing.T) { - config, _ := config.Load() - - client := &http.Client{} - gh := GitHub{client, "jingweno", "123", config.Token} - params := PullRequestParams{"title", "body", "jingweno:master", "jingweno:pull_request"} - _, err := gh.CreatePullRequest(CurrentProject(), params) - assert.Equal(t, nil, err) -} -- GitLab