Merge branch 'master' into gh_release

* master:
  Gramma issue in README
  Bump homebrew-gh which fixes sha and download path
  Bump homebrew-gh submodule to 1.0.0
  Bump version to v1.0.0
  Make it explicit that multiple credentials are allowed
  Quote path
  Add link to available setting
  Add autoupdate setting to README
  Make sure we load the deprecated configuration and override it with the new one.
  Save the new config after assigning the credentials.
  Save the configuration with the new format when the deprecated one is detected.
  Update versions automatically when autoupdate is set to true.
This commit is contained in:
David Calavera 2013-12-24 14:37:13 -08:00
Родитель e81a80ef02 46ba0fb52c
Коммит 7a0a506e23
5 изменённых файлов: 89 добавлений и 23 удалений

Просмотреть файл

@ -5,7 +5,7 @@
"ResourcesInclude": "INSTALL*,README*,LICENSE*", "ResourcesInclude": "INSTALL*,README*,LICENSE*",
"ResourcesExclude": "*.go", "ResourcesExclude": "*.go",
"MainDirsExclude": "Godeps", "MainDirsExclude": "Godeps",
"PackageVersion": "0.26.0", "PackageVersion": "1.0.0",
"Verbosity": "v", "Verbosity": "v",
"TaskSettings": { "TaskSettings": {
"downloads-page": { "downloads-page": {

Просмотреть файл

@ -74,12 +74,18 @@ func (updater *Updater) PromptForUpdate() (err error) {
releaseName, version := updater.latestReleaseNameAndVersion() releaseName, version := updater.latestReleaseNameAndVersion()
if version != "" && version != updater.CurrentVersion { if version != "" && version != updater.CurrentVersion {
update := github.CurrentConfigs().Autoupdate
if !update {
fmt.Println("There is a newer version of gh available.") fmt.Println("There is a newer version of gh available.")
fmt.Print("Type Y to update: ") fmt.Print("Type Y to update: ")
var confirm string var confirm string
fmt.Scan(&confirm) fmt.Scan(&confirm)
if confirm == "Y" || confirm == "y" { update = confirm == "Y" || confirm == "y"
}
if update {
err = updater.updateTo(releaseName, version) err = updater.updateTo(releaseName, version)
} }
} }

Просмотреть файл

@ -7,7 +7,7 @@ import (
"os" "os"
) )
const Version = "0.26.0" const Version = "1.0.0"
var cmdVersion = &Command{ var cmdVersion = &Command{
Run: runVersion, Run: runVersion,

Просмотреть файл

@ -24,7 +24,8 @@ type Credentials struct {
} }
type Configs struct { type Configs struct {
Credentials []Credentials Autoupdate bool `json:"autoupdate"`
Credentials []Credentials `json:"credentials"`
} }
func (c *Configs) PromptFor(host string) *Credentials { func (c *Configs) PromptFor(host string) *Credentials {
@ -48,7 +49,7 @@ func (c *Configs) PromptFor(host string) *Credentials {
cc = &Credentials{Host: host, User: user, AccessToken: token} cc = &Credentials{Host: host, User: user, AccessToken: token}
c.Credentials = append(c.Credentials, *cc) c.Credentials = append(c.Credentials, *cc)
err = saveTo(configsFile(), c.Credentials) err = saveTo(configsFile(), c)
utils.Check(err) utils.Check(err)
} }
@ -108,7 +109,17 @@ func saveTo(filename string, v interface{}) error {
return enc.Encode(v) return enc.Encode(v)
} }
func loadFrom(filename string, v interface{}) error { func loadFrom(filename string, c *Configs) error {
return loadFromFile(filename, c)
}
// Function to load deprecated configuration.
// It's not intended to be used.
func loadFromDeprecated(filename string, c *[]Credentials) error {
return loadFromFile(filename, c)
}
func loadFromFile(filename string, v interface{}) error {
f, err := os.Open(filename) f, err := os.Open(filename)
if err != nil { if err != nil {
return err return err
@ -137,13 +148,23 @@ func configsFile() string {
} }
func CurrentConfigs() *Configs { func CurrentConfigs() *Configs {
var c []Credentials c := &Configs{}
err := loadFrom(configsFile(), &c)
configFile := configsFile()
err := loadFrom(configFile, c)
if err != nil { if err != nil {
c = make([]Credentials, 0) // Try deprecated configuration
var creds []Credentials
err := loadFromDeprecated(configsFile(), &creds)
if err != nil {
creds = make([]Credentials, 0)
}
c.Credentials = creds
saveTo(configFile, c)
} }
return &Configs{c} return c
} }
func (c *Configs) DefaultCredentials() (credentials *Credentials) { func (c *Configs) DefaultCredentials() (credentials *Credentials) {
@ -184,14 +205,16 @@ func (c *Configs) selectCredentials() *Credentials {
} }
// Public for testing purpose // Public for testing purpose
func CreateTestConfigs(user, token string) []Credentials { func CreateTestConfigs(user, token string) *Configs {
f, _ := ioutil.TempFile("", "test-config") f, _ := ioutil.TempFile("", "test-config")
defaultConfigsFile = f.Name() defaultConfigsFile = f.Name()
c := []Credentials{ creds := []Credentials{
{User: "jingweno", AccessToken: "123", Host: GitHubHost}, {User: "jingweno", AccessToken: "123", Host: GitHubHost},
} }
saveTo(f.Name(), &c)
c := &Configs{Credentials: creds}
saveTo(f.Name(), c)
return c return c
} }

Просмотреть файл

@ -2,23 +2,60 @@ package github
import ( import (
"github.com/bmizerany/assert" "github.com/bmizerany/assert"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"testing" "testing"
) )
func TestSaveCredentials(t *testing.T) { func TestSaveCredentials(t *testing.T) {
c := Credentials{Host: "github.com", User: "jingweno", AccessToken: "123"} file, _ := ioutil.TempFile("", "test-gh-config-")
defer os.RemoveAll(file.Name())
ccreds := Credentials{Host: "github.com", User: "jingweno", AccessToken: "123"}
c := Configs{Credentials: []Credentials{ccreds}}
err := saveTo(file.Name(), &c)
assert.Equal(t, nil, err)
cc := &Configs{}
err = loadFrom(file.Name(), cc)
assert.Equal(t, nil, err)
creds := cc.Credentials[0]
assert.Equal(t, "github.com", creds.Host)
assert.Equal(t, "jingweno", creds.User)
assert.Equal(t, "123", creds.AccessToken)
}
func TestReadAndSaveDeprecatedConfiguration(t *testing.T) {
file, _ := ioutil.TempFile("", "test-gh-config-")
defer os.RemoveAll(file.Name())
defaultConfigsFile = file.Name()
file.WriteString(`[{"host":"github.com","user":"jingweno","access_token":"123"}]`)
file.Close()
CurrentConfigs()
expectedConfig := `{"autoupdate":false,"credentials":[{"host":"github.com","user":"jingweno","access_token":"123"}]}
`
f, _ := os.Open(file.Name())
content, _ := ioutil.ReadAll(f)
assert.Equal(t, expectedConfig, string(content))
}
func TestSaveAutoupdate(t *testing.T) {
file := "./test_support/test" file := "./test_support/test"
defer os.RemoveAll(filepath.Dir(file)) defer os.RemoveAll(filepath.Dir(file))
c := Configs{Autoupdate: true}
err := saveTo(file, &c) err := saveTo(file, &c)
assert.Equal(t, nil, err) assert.Equal(t, nil, err)
var cc Credentials var cc Configs
err = loadFrom(file, &cc) err = loadFrom(file, &cc)
assert.Equal(t, nil, err) assert.T(t, cc.Autoupdate)
assert.Equal(t, "github.com", cc.Host)
assert.Equal(t, "jingweno", cc.User)
assert.Equal(t, "123", cc.AccessToken)
} }