diff options
Diffstat (limited to 'bridge')
-rw-r--r-- | bridge/bridges.go | 12 | ||||
-rw-r--r-- | bridge/core/token.go | 75 | ||||
-rw-r--r-- | bridge/github/github.go | 4 | ||||
-rw-r--r-- | bridge/gitlab/gitlab.go | 4 | ||||
-rw-r--r-- | bridge/launchpad/launchpad.go | 4 |
5 files changed, 40 insertions, 59 deletions
diff --git a/bridge/bridges.go b/bridge/bridges.go index dcb35af1..9bbf3941 100644 --- a/bridge/bridges.go +++ b/bridge/bridges.go @@ -3,13 +3,19 @@ package bridge import ( "github.com/MichaelMure/git-bug/bridge/core" - _ "github.com/MichaelMure/git-bug/bridge/github" - _ "github.com/MichaelMure/git-bug/bridge/gitlab" - _ "github.com/MichaelMure/git-bug/bridge/launchpad" + "github.com/MichaelMure/git-bug/bridge/github" + "github.com/MichaelMure/git-bug/bridge/gitlab" + "github.com/MichaelMure/git-bug/bridge/launchpad" "github.com/MichaelMure/git-bug/cache" "github.com/MichaelMure/git-bug/repository" ) +func init() { + core.Register(&github.Github{}) + core.Register(&gitlab.Gitlab{}) + core.Register(&launchpad.Launchpad{}) +} + // Targets return all known bridge implementation target func Targets() []string { return core.Targets() diff --git a/bridge/core/token.go b/bridge/core/token.go index 0147f747..89c70af2 100644 --- a/bridge/core/token.go +++ b/bridge/core/token.go @@ -14,7 +14,7 @@ const ( tokenKeyScopes = "scopes" ) -// Token represent token related informations +// Token holds an API access token data type Token struct { Value string Target string @@ -35,47 +35,46 @@ func NewToken(value, target string, global bool, scopes []string) *Token { // Validate ensure token important fields are valid func (t *Token) Validate() error { if t.Value == "" { - return fmt.Errorf("missing token value") + return fmt.Errorf("missing value") } if t.Target == "" { - return fmt.Errorf("missing token target") + return fmt.Errorf("missing target") + } + if _, ok := bridgeImpl[t.Target]; !ok { + return fmt.Errorf("unknown target") } return nil } func loadToken(repo repository.RepoConfig, value string, global bool) (*Token, error) { keyPrefix := fmt.Sprintf("git-bug.token.%s.", value) - var pairs map[string]string - var err error - // read token config pairs + readerFn := repo.ReadConfigs if global { - pairs, err = repo.ReadGlobalConfigs(keyPrefix) - if err != nil { - return nil, err - } - } else { - pairs, err = repo.ReadConfigs(keyPrefix) - if err != nil { - return nil, err - } + readerFn = repo.ReadGlobalConfigs + } + + // read token config pairs + configs, err := readerFn(keyPrefix) + if err != nil { + return nil, err } // trim key prefix - result := make(Configuration, len(pairs)) - for key, value := range pairs { - key := strings.TrimPrefix(key, keyPrefix) - result[key] = value + for key, value := range configs { + newKey := strings.TrimPrefix(key, keyPrefix) + configs[newKey] = value + delete(configs, key) } var ok bool token := &Token{Value: value, Global: global} - token.Target, ok = result[tokenKeyTarget] + token.Target, ok = configs[tokenKeyTarget] if !ok { return nil, fmt.Errorf("empty token key") } - scopesString, ok := result[tokenKeyScopes] + scopesString, ok := configs[tokenKeyScopes] if !ok { return nil, fmt.Errorf("missing scopes config") } @@ -95,18 +94,14 @@ func GetGlobalToken(repo repository.RepoConfig, value string) (*Token, error) { } func listTokens(repo repository.RepoConfig, global bool) ([]string, error) { - var configs map[string]string - var err error + readerFn := repo.ReadConfigs if global { - configs, err = repo.ReadGlobalConfigs(tokenConfigKeyPrefix + ".") - if err != nil { - return nil, err - } - } else { - configs, err = repo.ReadConfigs(tokenConfigKeyPrefix + ".") - if err != nil { - return nil, err - } + readerFn = repo.ReadGlobalConfigs + } + + configs, err := readerFn(tokenConfigKeyPrefix + ".") + if err != nil { + return nil, err } re, err := regexp.Compile(tokenConfigKeyPrefix + `.([^.]+)`) @@ -147,27 +142,19 @@ func ListGlobalTokens(repo repository.RepoConfig) ([]string, error) { } func storeToken(repo repository.RepoConfig, token *Token) error { - var store func(key, value string) error + storeFn := repo.StoreConfig if token.Global { - store = repo.StoreGlobalConfig - } else { - store = repo.StoreConfig + storeFn = repo.StoreGlobalConfig } - var err error storeTargetKey := fmt.Sprintf("git-bug.token.%s.%s", token.Value, tokenKeyTarget) - err = store(storeTargetKey, token.Target) + err := storeFn(storeTargetKey, token.Target) if err != nil { return err } storeScopesKey := fmt.Sprintf("git-bug.token.%s.%s", token.Value, tokenKeyScopes) - err = store(storeScopesKey, strings.Join(token.Scopes, ",")) - if err != nil { - return err - } - - return nil + return storeFn(storeScopesKey, strings.Join(token.Scopes, ",")) } // StoreToken stores a token in the repo config diff --git a/bridge/github/github.go b/bridge/github/github.go index 176bdd84..e4fb03dd 100644 --- a/bridge/github/github.go +++ b/bridge/github/github.go @@ -10,10 +10,6 @@ import ( "github.com/MichaelMure/git-bug/bridge/core" ) -func init() { - core.Register(&Github{}) -} - type Github struct{} func (*Github) Target() string { diff --git a/bridge/gitlab/gitlab.go b/bridge/gitlab/gitlab.go index f4b980ac..05721bfe 100644 --- a/bridge/gitlab/gitlab.go +++ b/bridge/gitlab/gitlab.go @@ -23,10 +23,6 @@ const ( defaultTimeout = 60 * time.Second ) -func init() { - core.Register(&Gitlab{}) -} - type Gitlab struct{} func (*Gitlab) Target() string { diff --git a/bridge/launchpad/launchpad.go b/bridge/launchpad/launchpad.go index 1fd9edc2..030d9169 100644 --- a/bridge/launchpad/launchpad.go +++ b/bridge/launchpad/launchpad.go @@ -5,10 +5,6 @@ import ( "github.com/MichaelMure/git-bug/bridge/core" ) -func init() { - core.Register(&Launchpad{}) -} - type Launchpad struct{} func (*Launchpad) Target() string { |