aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authoramine <hilalyamine@gmail.com>2019-11-24 14:38:44 +0100
committeramine <hilalyamine@gmail.com>2019-11-24 14:38:44 +0100
commitda2d7970cd46c52bb8630c5540ea013bd3b9e81a (patch)
tree0c09acddbf5a84451c801f1b118b378b42c3194f
parent09db1cda71c87d87e700eabbecf89e10a0923d43 (diff)
downloadgit-bug-da2d7970cd46c52bb8630c5540ea013bd3b9e81a.tar.gz
bridge/core: add LoadTokensWithTarget and LoadOrCreateToken functions
bridge/github: global fixes
-rw-r--r--bridge/core/token.go42
-rw-r--r--bridge/github/config.go54
2 files changed, 58 insertions, 38 deletions
diff --git a/bridge/core/token.go b/bridge/core/token.go
index 0eac1a24..186b9ff4 100644
--- a/bridge/core/token.go
+++ b/bridge/core/token.go
@@ -195,6 +195,26 @@ func LoadTokens(repo repository.RepoCommon) ([]*Token, error) {
return tokens, nil
}
+// LoadTokensWithTarget load all existing tokens for a given target
+func LoadTokensWithTarget(repo repository.RepoCommon, target string) ([]*Token, error) {
+ tokensIds, err := ListTokens(repo)
+ if err != nil {
+ return nil, err
+ }
+
+ var tokens []*Token
+ for _, id := range tokensIds {
+ token, err := LoadToken(repo, id)
+ if err != nil {
+ return nil, err
+ }
+ if token.Target == target {
+ tokens = append(tokens, token)
+ }
+ }
+ return tokens, nil
+}
+
// TokenIdExist return wether token id exist or not
func TokenIdExist(repo repository.RepoCommon, id entity.Id) bool {
_, err := LoadToken(repo, id)
@@ -252,3 +272,25 @@ func RemoveToken(repo repository.RepoCommon, id entity.Id) error {
keyPrefix := fmt.Sprintf("git-bug.token.%s", id)
return repo.GlobalConfig().RemoveAll(keyPrefix)
}
+
+// LoadOrCreateToken will try to load a token matching the same value or create it
+func LoadOrCreateToken(repo repository.RepoCommon, target, tokenValue string) (*Token, error) {
+ tokens, err := LoadTokensWithTarget(repo, target)
+ if err != nil {
+ return nil, err
+ }
+
+ for _, token := range tokens {
+ if token.Value == tokenValue {
+ return token, nil
+ }
+ }
+
+ token := NewToken(tokenValue, target)
+ err = StoreToken(repo, token)
+ if err != nil {
+ return nil, err
+ }
+
+ return token, nil
+}
diff --git a/bridge/github/config.go b/bridge/github/config.go
index 45b2bd44..0fbbd5aa 100644
--- a/bridge/github/config.go
+++ b/bridge/github/config.go
@@ -113,7 +113,7 @@ func (g *Github) Configure(repo repository.RepoCommon, params core.BridgeParams)
// at this point, we check if the token already exist or we create a new one
if token != "" {
- tokenObj, err = loadOrCreateToken(repo, token)
+ tokenObj, err = core.LoadOrCreateToken(repo, target, token)
if err != nil {
return nil, err
}
@@ -149,27 +149,6 @@ func (g *Github) Configure(repo repository.RepoCommon, params core.BridgeParams)
return conf, nil
}
-func loadOrCreateToken(repo repository.RepoCommon, tokenValue string) (*core.Token, error) {
- tokens, err := core.LoadTokens(repo)
- if err != nil {
- return nil, err
- }
-
- for _, token := range tokens {
- if token.Target == target && token.Value == tokenValue {
- return token, nil
- }
- }
-
- token := core.NewToken(tokenValue, target)
- err = core.StoreToken(repo, token)
- if err != nil {
- return nil, err
- }
-
- return token, nil
-}
-
func (*Github) ValidateConfig(conf core.Configuration) error {
if v, ok := conf[core.ConfigKeyTarget]; !ok {
return fmt.Errorf("missing %s key", core.ConfigKeyTarget)
@@ -264,7 +243,7 @@ func randomFingerprint() string {
func promptTokenOptions(repo repository.RepoCommon, owner, project string) (*core.Token, error) {
for {
- tokens, err := core.LoadTokens(repo)
+ tokens, err := core.LoadTokensWithTarget(repo, target)
if err != nil {
return nil, err
}
@@ -272,15 +251,13 @@ func promptTokenOptions(repo repository.RepoCommon, owner, project string) (*cor
fmt.Println()
fmt.Println("[1]: user provided token")
fmt.Println("[2]: interactive token creation")
- fmt.Println("known tokens for Github:")
-
- var githubTokens []*core.Token
- i := 0
- for _, token := range tokens {
- if token.Target == target {
- fmt.Printf("[%d]: %s\n", i+3, token.ID())
- githubTokens = append(githubTokens, token)
- i++
+
+ if len(tokens) > 0 {
+ fmt.Println("known tokens for Github:")
+ for i, token := range tokens {
+ if token.Target == target {
+ fmt.Printf("[%d]: %s\n", i+3, token.ID())
+ }
}
}
fmt.Print("Select option: ")
@@ -294,27 +271,28 @@ func promptTokenOptions(repo repository.RepoCommon, owner, project string) (*cor
line = strings.TrimRight(line, "\n")
index, err := strconv.Atoi(line)
- if err != nil || index < 1 || index > len(githubTokens)+2 {
+ if err != nil || index < 1 || index > len(tokens)+2 {
fmt.Println("invalid input")
continue
}
var token string
- if index == 1 {
+ switch index {
+ case 1:
token, err = promptToken()
if err != nil {
return nil, err
}
- } else if index == 2 {
+ case 2:
token, err = loginAndRequestToken(owner, project)
if err != nil {
return nil, err
}
- } else {
- return githubTokens[index-2], nil
+ default:
+ return tokens[index-3], nil
}
- return loadOrCreateToken(repo, token)
+ return core.LoadOrCreateToken(repo, target, token)
}
}