aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAmine <hilalyamine@gmail.com>2019-09-18 21:00:07 +0200
committeramine <hilalyamine@gmail.com>2019-11-09 13:25:07 +0100
commit967e168333a7c3057cf77f9c3f890ea1bbf7a1df (patch)
treec43aa8eedfece818a34c06d3798b353c09f19bcf
parent9370e1292b99bc443fe9128d670f36a94f996ca9 (diff)
downloadgit-bug-967e168333a7c3057cf77f9c3f890ea1bbf7a1df.tar.gz
bridge/core: use token id instead of name
commands: add bridge token rm/add commands: improve bridge token printing
-rw-r--r--bridge/core/token.go65
-rw-r--r--commands/bridge_token.go45
-rw-r--r--commands/bridge_token_add.go44
-rw-r--r--commands/bridge_token_rm.go29
4 files changed, 133 insertions, 50 deletions
diff --git a/bridge/core/token.go b/bridge/core/token.go
index 9e393642..5e629c51 100644
--- a/bridge/core/token.go
+++ b/bridge/core/token.go
@@ -3,7 +3,6 @@ package core
import (
"fmt"
"regexp"
- "strconv"
"strings"
"github.com/MichaelMure/git-bug/repository"
@@ -13,13 +12,12 @@ const (
tokenConfigKeyPrefix = "git-bug.token"
tokenKeyValue = "value"
tokenKeyTarget = "target"
- tokenKeyGlobal = "global"
tokenKeyScopes = "scopes"
)
// Token represent token related informations
type Token struct {
- Name string
+ Id string
Value string
Target string
Global bool
@@ -27,9 +25,9 @@ type Token struct {
}
// NewToken instantiate a new token
-func NewToken(name, value, target string, global bool, scopes []string) *Token {
+func NewToken(id, value, target string, global bool, scopes []string) *Token {
return &Token{
- Name: name,
+ Id: id,
Value: value,
Target: target,
Global: global,
@@ -39,8 +37,8 @@ func NewToken(name, value, target string, global bool, scopes []string) *Token {
// Validate ensure token important fields are valid
func (t *Token) Validate() error {
- if t.Name == "" {
- return fmt.Errorf("missing token name")
+ if t.Id == "" {
+ return fmt.Errorf("missing token id")
}
if t.Value == "" {
return fmt.Errorf("missing token value")
@@ -51,8 +49,8 @@ func (t *Token) Validate() error {
return nil
}
-func loadToken(repo repository.RepoConfig, name string, global bool) (*Token, error) {
- keyPrefix := fmt.Sprintf("git-bug.token.%s", name)
+func loadToken(repo repository.RepoConfig, id string, global bool) (*Token, error) {
+ keyPrefix := fmt.Sprintf("git-bug.token.%s.", id)
var pairs map[string]string
var err error
@@ -62,7 +60,6 @@ func loadToken(repo repository.RepoConfig, name string, global bool) (*Token, er
if err != nil {
return nil, err
}
-
} else {
pairs, err = repo.ReadConfigs(keyPrefix)
if err != nil {
@@ -78,7 +75,7 @@ func loadToken(repo repository.RepoConfig, name string, global bool) (*Token, er
}
var ok bool
- token := &Token{Name: name}
+ token := &Token{Id: id, Global: global}
token.Value, ok = result[tokenKeyValue]
if !ok {
return nil, fmt.Errorf("empty token value")
@@ -89,12 +86,6 @@ func loadToken(repo repository.RepoConfig, name string, global bool) (*Token, er
return nil, fmt.Errorf("empty token key")
}
- if g, ok := result[tokenKeyGlobal]; !ok {
- return nil, fmt.Errorf("empty token global")
- } else if g == "true" {
- token.Global = true
- }
-
scopesString, ok := result[tokenKeyScopes]
if !ok {
return nil, fmt.Errorf("missing scopes config")
@@ -105,25 +96,25 @@ func loadToken(repo repository.RepoConfig, name string, global bool) (*Token, er
}
// GetToken loads a token from repo config
-func GetToken(repo repository.RepoConfig, name string) (*Token, error) {
- return loadToken(repo, name, false)
+func GetToken(repo repository.RepoConfig, id string) (*Token, error) {
+ return loadToken(repo, id, false)
}
// GetGlobalToken loads a token from the global config
-func GetGlobalToken(repo repository.RepoConfig, name string) (*Token, error) {
- return loadToken(repo, name, true)
+func GetGlobalToken(repo repository.RepoConfig, id string) (*Token, error) {
+ return loadToken(repo, id, true)
}
func listTokens(repo repository.RepoConfig, global bool) ([]string, error) {
var configs map[string]string
var err error
if global {
- configs, err = repo.ReadConfigs(tokenConfigKeyPrefix + ".")
+ configs, err = repo.ReadGlobalConfigs(tokenConfigKeyPrefix + ".")
if err != nil {
return nil, err
}
} else {
- configs, err = repo.ReadGlobalConfigs(tokenConfigKeyPrefix + ".")
+ configs, err = repo.ReadConfigs(tokenConfigKeyPrefix + ".")
if err != nil {
return nil, err
}
@@ -175,25 +166,19 @@ func storeToken(repo repository.RepoConfig, token *Token) error {
}
var err error
- storeValueKey := fmt.Sprintf("git-bug.token.%s.%s", token.Name, tokenKeyValue)
+ storeValueKey := fmt.Sprintf("git-bug.token.%s.%s", token.Id, tokenKeyValue)
err = store(storeValueKey, token.Value)
if err != nil {
return err
}
- storeTargetKey := fmt.Sprintf("git-bug.token.%s.%s", token.Name, tokenKeyTarget)
+ storeTargetKey := fmt.Sprintf("git-bug.token.%s.%s", token.Id, tokenKeyTarget)
err = store(storeTargetKey, token.Target)
if err != nil {
return err
}
- storeGlobalKey := fmt.Sprintf("git-bug.token.%s.%s", token.Name, tokenKeyGlobal)
- err = store(storeGlobalKey, strconv.FormatBool(token.Global))
- if err != nil {
- return err
- }
-
- storeScopesKey := fmt.Sprintf("git-bug.token.%s.%s", token.Name, tokenKeyScopes)
+ storeScopesKey := fmt.Sprintf("git-bug.token.%s.%s", token.Id, tokenKeyScopes)
err = store(storeScopesKey, strings.Join(token.Scopes, ","))
if err != nil {
return err
@@ -203,23 +188,23 @@ func storeToken(repo repository.RepoConfig, token *Token) error {
}
// StoreToken stores a token in the repo config
-func StoreToken(repo repository.RepoConfig, name, value, target string, scopes []string) error {
- return storeToken(repo, NewToken(name, value, target, false, scopes))
+func StoreToken(repo repository.RepoConfig, token *Token) error {
+ return storeToken(repo, token)
}
// StoreGlobalToken stores a token in global config
-func StoreGlobalToken(repo repository.RepoConfig, name, value, target string, scopes []string) error {
- return storeToken(repo, NewToken(name, value, target, true, scopes))
+func StoreGlobalToken(repo repository.RepoConfig, token *Token) error {
+ return storeToken(repo, token)
}
// RemoveToken removes a token from the repo config
-func RemoveToken(repo repository.RepoConfig, name string) error {
- keyPrefix := fmt.Sprintf("git-bug.token.%s", name)
+func RemoveToken(repo repository.RepoConfig, id string) error {
+ keyPrefix := fmt.Sprintf("git-bug.token.%s", id)
return repo.RmConfigs(keyPrefix)
}
// RemoveGlobalToken removes a token from the repo config
-func RemoveGlobalToken(repo repository.RepoConfig, name string) error {
- keyPrefix := fmt.Sprintf("git-bug.token.%s", name)
+func RemoveGlobalToken(repo repository.RepoConfig, id string) error {
+ keyPrefix := fmt.Sprintf("git-bug.token.%s", id)
return repo.RmGlobalConfigs(keyPrefix)
}
diff --git a/commands/bridge_token.go b/commands/bridge_token.go
index 6cb6c4b7..52dea9a9 100644
--- a/commands/bridge_token.go
+++ b/commands/bridge_token.go
@@ -2,16 +2,18 @@ package commands
import (
"fmt"
+ "strings"
"github.com/spf13/cobra"
"github.com/MichaelMure/git-bug/bridge/core"
"github.com/MichaelMure/git-bug/cache"
+ "github.com/MichaelMure/git-bug/util/colors"
"github.com/MichaelMure/git-bug/util/interrupt"
+ "github.com/MichaelMure/git-bug/util/text"
)
var (
- bridgeTokenAll bool
bridgeTokenLocalOnly bool
bridgeTokenGlobalOnly bool
)
@@ -24,25 +26,49 @@ func runTokenBridge(cmd *cobra.Command, args []string) error {
defer backend.Close()
interrupt.RegisterCleaner(backend.Close)
- var tokens []string
- if bridgeTokenLocalOnly || bridgeTokenAll {
+ var tokens []*core.Token
+ if !bridgeTokenGlobalOnly {
localTokens, err := core.ListTokens(backend)
if err != nil {
return err
}
- tokens = localTokens
+
+ for _, id := range localTokens {
+ token, err := core.GetToken(repo, id)
+ if err != nil {
+ return err
+ }
+ tokens = append(tokens, token)
+ }
}
- if bridgeTokenGlobalOnly || bridgeTokenAll {
+ if !bridgeTokenLocalOnly {
globalTokens, err := core.ListGlobalTokens(backend)
if err != nil {
return err
}
- tokens = append(tokens, globalTokens...)
+
+ for _, id := range globalTokens {
+ token, err := core.GetGlobalToken(repo, id)
+ if err != nil {
+ return err
+ }
+ tokens = append(tokens, token)
+ }
}
- for _, c := range tokens {
- fmt.Println(c)
+ for _, token := range tokens {
+ valueFmt := text.LeftPadMaxLine(token.Value, 20, 0)
+ targetFmt := text.LeftPadMaxLine(token.Target, 10, 0)
+ scopesFmt := text.LeftPadMaxLine(strings.Join(token.Scopes, ","), 20, 0)
+
+ fmt.Printf("%s %s %s %s %s\n",
+ colors.Cyan(token.Id),
+ colors.Magenta(targetFmt),
+ colors.Yellow(token.Global),
+ valueFmt,
+ scopesFmt,
+ )
}
return nil
}
@@ -57,8 +83,7 @@ var bridgeTokenCmd = &cobra.Command{
func init() {
bridgeCmd.AddCommand(bridgeTokenCmd)
- bridgeTokenCmd.Flags().BoolVarP(&bridgeTokenAll, "all", "a", false, "")
- bridgeTokenCmd.Flags().BoolVarP(&bridgeTokenLocalOnly, "local", "l", true, "")
+ bridgeTokenCmd.Flags().BoolVarP(&bridgeTokenLocalOnly, "local", "l", false, "")
bridgeTokenCmd.Flags().BoolVarP(&bridgeTokenGlobalOnly, "global", "g", false, "")
bridgeTokenCmd.Flags().SortFlags = false
}
diff --git a/commands/bridge_token_add.go b/commands/bridge_token_add.go
new file mode 100644
index 00000000..8b3fc5ce
--- /dev/null
+++ b/commands/bridge_token_add.go
@@ -0,0 +1,44 @@
+package commands
+
+import (
+ "github.com/spf13/cobra"
+
+ "github.com/MichaelMure/git-bug/bridge/core"
+)
+
+var (
+ bridgeToken core.Token
+)
+
+func runBridgeTokenAdd(cmd *cobra.Command, args []string) error {
+ if bridgeToken.Global {
+ return core.StoreToken(
+ repo,
+ &bridgeToken,
+ )
+ }
+
+ return core.StoreGlobalToken(
+ repo,
+ &bridgeToken,
+ )
+}
+
+var bridgeTokenAddCmd = &cobra.Command{
+ Use: "add",
+ Short: "Configure and use bridge tokens.",
+ PreRunE: loadRepo,
+ RunE: runBridgeTokenAdd,
+ Args: cobra.NoArgs,
+}
+
+func init() {
+ bridgeTokenCmd.AddCommand(bridgeTokenAddCmd)
+ bridgeTokenAddCmd.Flags().StringVarP(&bridgeToken.Id, "id", "i", "", "")
+ bridgeTokenAddCmd.Flags().BoolVarP(&bridgeToken.Global, "global", "g", false, "")
+ bridgeTokenAddCmd.Flags().StringVarP(&bridgeToken.Value, "value", "v", "", "")
+ bridgeTokenAddCmd.Flags().StringVarP(&bridgeToken.Target, "target", "t", "", "")
+ bridgeTokenAddCmd.Flags().StringArrayVarP(&bridgeToken.Scopes, "scopes", "s", []string{}, "")
+ bridgeTokenAddCmd.Flags().SortFlags = false
+
+}
diff --git a/commands/bridge_token_rm.go b/commands/bridge_token_rm.go
new file mode 100644
index 00000000..7fd79126
--- /dev/null
+++ b/commands/bridge_token_rm.go
@@ -0,0 +1,29 @@
+package commands
+
+import (
+ "github.com/spf13/cobra"
+
+ "github.com/MichaelMure/git-bug/bridge/core"
+)
+
+func runBridgeTokenRm(cmd *cobra.Command, args []string) error {
+ err := core.RemoveToken(repo, args[0])
+ if err == nil {
+ return nil
+ }
+
+ err = core.RemoveGlobalToken(repo, args[0])
+ return err
+}
+
+var bridgeTokenRmCmd = &cobra.Command{
+ Use: "rm",
+ Short: "Configure and use bridge tokens.",
+ PreRunE: loadRepo,
+ RunE: runBridgeTokenRm,
+ Args: cobra.ExactArgs(1),
+}
+
+func init() {
+ bridgeTokenCmd.AddCommand(bridgeTokenRmCmd)
+}