зеркало из https://github.com/mislav/hub.git
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:
Коммит
7a0a506e23
|
@ -5,7 +5,7 @@
|
|||
"ResourcesInclude": "INSTALL*,README*,LICENSE*",
|
||||
"ResourcesExclude": "*.go",
|
||||
"MainDirsExclude": "Godeps",
|
||||
"PackageVersion": "0.26.0",
|
||||
"PackageVersion": "1.0.0",
|
||||
"Verbosity": "v",
|
||||
"TaskSettings": {
|
||||
"downloads-page": {
|
||||
|
|
|
@ -74,12 +74,18 @@ func (updater *Updater) PromptForUpdate() (err error) {
|
|||
|
||||
releaseName, version := updater.latestReleaseNameAndVersion()
|
||||
if version != "" && version != updater.CurrentVersion {
|
||||
fmt.Println("There is a newer version of gh available.")
|
||||
fmt.Print("Type Y to update: ")
|
||||
var confirm string
|
||||
fmt.Scan(&confirm)
|
||||
update := github.CurrentConfigs().Autoupdate
|
||||
|
||||
if confirm == "Y" || confirm == "y" {
|
||||
if !update {
|
||||
fmt.Println("There is a newer version of gh available.")
|
||||
fmt.Print("Type Y to update: ")
|
||||
var confirm string
|
||||
fmt.Scan(&confirm)
|
||||
|
||||
update = confirm == "Y" || confirm == "y"
|
||||
}
|
||||
|
||||
if update {
|
||||
err = updater.updateTo(releaseName, version)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@ import (
|
|||
"os"
|
||||
)
|
||||
|
||||
const Version = "0.26.0"
|
||||
const Version = "1.0.0"
|
||||
|
||||
var cmdVersion = &Command{
|
||||
Run: runVersion,
|
||||
|
|
|
@ -24,7 +24,8 @@ type Credentials struct {
|
|||
}
|
||||
|
||||
type Configs struct {
|
||||
Credentials []Credentials
|
||||
Autoupdate bool `json:"autoupdate"`
|
||||
Credentials []Credentials `json:"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}
|
||||
c.Credentials = append(c.Credentials, *cc)
|
||||
err = saveTo(configsFile(), c.Credentials)
|
||||
err = saveTo(configsFile(), c)
|
||||
utils.Check(err)
|
||||
}
|
||||
|
||||
|
@ -108,7 +109,17 @@ func saveTo(filename string, v interface{}) error {
|
|||
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)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -137,13 +148,23 @@ func configsFile() string {
|
|||
}
|
||||
|
||||
func CurrentConfigs() *Configs {
|
||||
var c []Credentials
|
||||
err := loadFrom(configsFile(), &c)
|
||||
c := &Configs{}
|
||||
|
||||
configFile := configsFile()
|
||||
err := loadFrom(configFile, c)
|
||||
|
||||
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) {
|
||||
|
@ -184,14 +205,16 @@ func (c *Configs) selectCredentials() *Credentials {
|
|||
}
|
||||
|
||||
// Public for testing purpose
|
||||
func CreateTestConfigs(user, token string) []Credentials {
|
||||
func CreateTestConfigs(user, token string) *Configs {
|
||||
f, _ := ioutil.TempFile("", "test-config")
|
||||
defaultConfigsFile = f.Name()
|
||||
|
||||
c := []Credentials{
|
||||
creds := []Credentials{
|
||||
{User: "jingweno", AccessToken: "123", Host: GitHubHost},
|
||||
}
|
||||
saveTo(f.Name(), &c)
|
||||
|
||||
c := &Configs{Credentials: creds}
|
||||
saveTo(f.Name(), c)
|
||||
|
||||
return c
|
||||
}
|
||||
|
|
|
@ -2,23 +2,60 @@ package github
|
|||
|
||||
import (
|
||||
"github.com/bmizerany/assert"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
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"
|
||||
defer os.RemoveAll(filepath.Dir(file))
|
||||
|
||||
c := Configs{Autoupdate: true}
|
||||
|
||||
err := saveTo(file, &c)
|
||||
assert.Equal(t, nil, err)
|
||||
|
||||
var cc Credentials
|
||||
var cc Configs
|
||||
err = loadFrom(file, &cc)
|
||||
assert.Equal(t, nil, err)
|
||||
assert.Equal(t, "github.com", cc.Host)
|
||||
assert.Equal(t, "jingweno", cc.User)
|
||||
assert.Equal(t, "123", cc.AccessToken)
|
||||
assert.T(t, cc.Autoupdate)
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче