aboutsummaryrefslogtreecommitdiffstats
path: root/commands
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2019-12-08 21:15:06 +0100
committerMichael Muré <batolettre@gmail.com>2019-12-08 21:28:27 +0100
commitb92adfcb5f79f2b32c3dafb0fc3e7f1b753b6197 (patch)
tree69202c4021b10f3ab7b7f5ebf229d501e95c4786 /commands
parent981a4a848b1329da1a73270e27633911f9298bb1 (diff)
downloadgit-bug-b92adfcb5f79f2b32c3dafb0fc3e7f1b753b6197.tar.gz
bridge: huge refactor to accept multiple kind of credentials
Diffstat (limited to 'commands')
-rw-r--r--commands/add.go2
-rw-r--r--commands/bridge_auth.go52
-rw-r--r--commands/bridge_auth_addtoken.go (renamed from commands/bridge_auth_add.go)15
-rw-r--r--commands/bridge_auth_rm.go8
-rw-r--r--commands/bridge_auth_show.go18
-rw-r--r--commands/bridge_configure.go50
-rw-r--r--commands/bridge_pull.go2
-rw-r--r--commands/bridge_push.go2
-rw-r--r--commands/comment_add.go2
-rw-r--r--commands/label_add.go2
-rw-r--r--commands/root.go8
-rw-r--r--commands/status_close.go2
-rw-r--r--commands/status_open.go2
-rw-r--r--commands/title_edit.go2
-rw-r--r--commands/user.go5
-rw-r--r--commands/user_create.go4
-rw-r--r--commands/webui.go2
17 files changed, 111 insertions, 67 deletions
diff --git a/commands/add.go b/commands/add.go
index ff4f9529..e656a262 100644
--- a/commands/add.go
+++ b/commands/add.go
@@ -57,7 +57,7 @@ func runAddBug(cmd *cobra.Command, args []string) error {
var addCmd = &cobra.Command{
Use: "add",
Short: "Create a new bug.",
- PreRunE: loadRepo,
+ PreRunE: loadRepoEnsureUser,
RunE: runAddBug,
}
diff --git a/commands/bridge_auth.go b/commands/bridge_auth.go
index e7fce1bd..4e8b50c4 100644
--- a/commands/bridge_auth.go
+++ b/commands/bridge_auth.go
@@ -7,36 +7,56 @@ import (
text "github.com/MichaelMure/go-term-text"
- "github.com/MichaelMure/git-bug/bridge/core"
+ "github.com/MichaelMure/git-bug/bridge/core/auth"
+ "github.com/MichaelMure/git-bug/cache"
"github.com/MichaelMure/git-bug/util/colors"
+ "github.com/MichaelMure/git-bug/util/interrupt"
)
func runBridgeAuth(cmd *cobra.Command, args []string) error {
- tokens, err := core.ListTokens(repo)
+ backend, err := cache.NewRepoCache(repo)
if err != nil {
return err
}
+ defer backend.Close()
+ interrupt.RegisterCleaner(backend.Close)
- for _, token := range tokens {
- token, err := core.LoadToken(repo, token)
+ creds, err := auth.List(backend)
+ if err != nil {
+ return err
+ }
+
+ defaultUser, _ := backend.GetUserIdentity()
+
+ for _, cred := range creds {
+ targetFmt := text.LeftPadMaxLine(cred.Target(), 10, 0)
+
+ var value string
+ switch cred := cred.(type) {
+ case *auth.Token:
+ value = cred.Value
+ }
+
+ user, err := backend.ResolveIdentity(cred.UserId())
if err != nil {
return err
}
- printToken(token)
- }
+ userFmt := user.DisplayName()
- return nil
-}
+ if cred.UserId() == defaultUser.Id() {
+ userFmt = colors.Red(userFmt)
+ }
-func printToken(token *core.Token) {
- targetFmt := text.LeftPadMaxLine(token.Target, 10, 0)
+ fmt.Printf("%s %s %s %s %s\n",
+ colors.Cyan(cred.ID().Human()),
+ colors.Yellow(targetFmt),
+ colors.Magenta(cred.Kind()),
+ userFmt,
+ value,
+ )
+ }
- fmt.Printf("%s %s %s %s\n",
- colors.Cyan(token.ID().Human()),
- colors.Yellow(targetFmt),
- colors.Magenta("token"),
- token.Value,
- )
+ return nil
}
var bridgeAuthCmd = &cobra.Command{
diff --git a/commands/bridge_auth_add.go b/commands/bridge_auth_addtoken.go
index ae2c4dbc..018015e4 100644
--- a/commands/bridge_auth_add.go
+++ b/commands/bridge_auth_addtoken.go
@@ -12,6 +12,8 @@ import (
"github.com/MichaelMure/git-bug/bridge"
"github.com/MichaelMure/git-bug/bridge/core"
+ "github.com/MichaelMure/git-bug/bridge/core/auth"
+ "github.com/MichaelMure/git-bug/identity"
)
var (
@@ -22,7 +24,7 @@ func runBridgeTokenAdd(cmd *cobra.Command, args []string) error {
var value string
if bridgeAuthAddTokenTarget == "" {
- return fmt.Errorf("auth target is required")
+ return fmt.Errorf("flag --target is required")
}
if !core.TargetExist(bridgeAuthAddTokenTarget) {
@@ -44,12 +46,17 @@ func runBridgeTokenAdd(cmd *cobra.Command, args []string) error {
value = strings.TrimSuffix(raw, "\n")
}
- token := core.NewToken(value, bridgeAuthAddTokenTarget)
+ user, err := identity.GetUserIdentity(repo)
+ if err != nil {
+ return err
+ }
+
+ token := auth.NewToken(user.Id(), value, bridgeAuthAddTokenTarget)
if err := token.Validate(); err != nil {
return errors.Wrap(err, "invalid token")
}
- err := core.StoreToken(repo, token)
+ err = auth.Store(repo, token)
if err != nil {
return err
}
@@ -61,7 +68,7 @@ func runBridgeTokenAdd(cmd *cobra.Command, args []string) error {
var bridgeAuthAddTokenCmd = &cobra.Command{
Use: "add-token [<token>]",
Short: "Store a new token",
- PreRunE: loadRepo,
+ PreRunE: loadRepoEnsureUser,
RunE: runBridgeTokenAdd,
Args: cobra.MaximumNArgs(1),
}
diff --git a/commands/bridge_auth_rm.go b/commands/bridge_auth_rm.go
index b0b4d437..17e70625 100644
--- a/commands/bridge_auth_rm.go
+++ b/commands/bridge_auth_rm.go
@@ -5,21 +5,21 @@ import (
"github.com/spf13/cobra"
- "github.com/MichaelMure/git-bug/bridge/core"
+ "github.com/MichaelMure/git-bug/bridge/core/auth"
)
func runBridgeAuthRm(cmd *cobra.Command, args []string) error {
- token, err := core.LoadTokenPrefix(repo, args[0])
+ cred, err := auth.LoadWithPrefix(repo, args[0])
if err != nil {
return err
}
- err = core.RemoveToken(repo, token.ID())
+ err = auth.Remove(repo, cred.ID())
if err != nil {
return err
}
- fmt.Printf("token %s removed\n", token.ID())
+ fmt.Printf("credential %s removed\n", cred.ID())
return nil
}
diff --git a/commands/bridge_auth_show.go b/commands/bridge_auth_show.go
index 94141b93..5352957d 100644
--- a/commands/bridge_auth_show.go
+++ b/commands/bridge_auth_show.go
@@ -6,20 +6,24 @@ import (
"github.com/spf13/cobra"
- "github.com/MichaelMure/git-bug/bridge/core"
+ "github.com/MichaelMure/git-bug/bridge/core/auth"
)
func runBridgeAuthShow(cmd *cobra.Command, args []string) error {
- token, err := core.LoadTokenPrefix(repo, args[0])
+ cred, err := auth.LoadWithPrefix(repo, args[0])
if err != nil {
return err
}
- fmt.Printf("Id: %s\n", token.ID())
- fmt.Printf("Target: %s\n", token.Target)
- fmt.Printf("Type: token\n")
- fmt.Printf("Value: %s\n", token.Value)
- fmt.Printf("Creation: %s\n", token.CreateTime.Format(time.RFC822))
+ fmt.Printf("Id: %s\n", cred.ID())
+ fmt.Printf("Target: %s\n", cred.Target())
+ fmt.Printf("Kind: %s\n", cred.Kind())
+ fmt.Printf("Creation: %s\n", cred.CreateTime().Format(time.RFC822))
+
+ switch cred := cred.(type) {
+ case *auth.Token:
+ fmt.Printf("Value: %s\n", cred.Value)
+ }
return nil
}
diff --git a/commands/bridge_configure.go b/commands/bridge_configure.go
index 6c24568c..00634b28 100644
--- a/commands/bridge_configure.go
+++ b/commands/bridge_configure.go
@@ -11,6 +11,7 @@ import (
"github.com/MichaelMure/git-bug/bridge"
"github.com/MichaelMure/git-bug/bridge/core"
+ "github.com/MichaelMure/git-bug/bridge/core/auth"
"github.com/MichaelMure/git-bug/cache"
"github.com/MichaelMure/git-bug/repository"
"github.com/MichaelMure/git-bug/util/interrupt"
@@ -21,9 +22,11 @@ const (
)
var (
- bridgeConfigureName string
- bridgeConfigureTarget string
- bridgeParams core.BridgeParams
+ bridgeConfigureName string
+ bridgeConfigureTarget string
+ bridgeConfigureParams core.BridgeParams
+ bridgeConfigureToken string
+ bridgeConfigureTokenStdin bool
)
func runBridgeConfigure(cmd *cobra.Command, args []string) error {
@@ -34,9 +37,28 @@ func runBridgeConfigure(cmd *cobra.Command, args []string) error {
defer backend.Close()
interrupt.RegisterCleaner(backend.Close)
- if (bridgeParams.TokenStdin || bridgeParams.Token != "" || bridgeParams.TokenId != "") &&
+ if (bridgeConfigureTokenStdin || bridgeConfigureToken != "" || bridgeConfigureParams.CredPrefix != "") &&
(bridgeConfigureName == "" || bridgeConfigureTarget == "") {
- return fmt.Errorf("you must provide a bridge name and target to configure a bridge with a token")
+ return fmt.Errorf("you must provide a bridge name and target to configure a bridge with a credential")
+ }
+
+ // early fail
+ if bridgeConfigureParams.CredPrefix != "" {
+ if _, err := auth.LoadWithPrefix(repo, bridgeConfigureParams.CredPrefix); err != nil {
+ return err
+ }
+ }
+
+ switch {
+ case bridgeConfigureTokenStdin:
+ reader := bufio.NewReader(os.Stdin)
+ token, err := reader.ReadString('\n')
+ if err != nil {
+ return fmt.Errorf("reading from stdin: %v", err)
+ }
+ bridgeConfigureParams.TokenRaw = strings.TrimSpace(token)
+ case bridgeConfigureToken != "":
+ bridgeConfigureParams.TokenRaw = bridgeConfigureToken
}
if bridgeConfigureTarget == "" {
@@ -58,7 +80,7 @@ func runBridgeConfigure(cmd *cobra.Command, args []string) error {
return err
}
- err = b.Configure(bridgeParams)
+ err = b.Configure(bridgeConfigureParams)
if err != nil {
return err
}
@@ -94,7 +116,7 @@ func promptTarget() (string, error) {
}
}
-func promptName(repo repository.RepoCommon) (string, error) {
+func promptName(repo repository.RepoConfig) (string, error) {
defaultExist := core.BridgeExist(repo, defaultName)
for {
@@ -184,7 +206,7 @@ git bug bridge configure \
--target=github \
--url=https://github.com/michaelmure/git-bug \
--token=$(TOKEN)`,
- PreRunE: loadRepo,
+ PreRunE: loadRepoEnsureUser,
RunE: runBridgeConfigure,
}
@@ -193,11 +215,11 @@ func init() {
bridgeConfigureCmd.Flags().StringVarP(&bridgeConfigureName, "name", "n", "", "A distinctive name to identify the bridge")
bridgeConfigureCmd.Flags().StringVarP(&bridgeConfigureTarget, "target", "t", "",
fmt.Sprintf("The target of the bridge. Valid values are [%s]", strings.Join(bridge.Targets(), ",")))
- bridgeConfigureCmd.Flags().StringVarP(&bridgeParams.URL, "url", "u", "", "The URL of the target repository")
- bridgeConfigureCmd.Flags().StringVarP(&bridgeParams.Owner, "owner", "o", "", "The owner of the target repository")
- bridgeConfigureCmd.Flags().StringVarP(&bridgeParams.Token, "token", "T", "", "The authentication token for the API")
- bridgeConfigureCmd.Flags().StringVarP(&bridgeParams.TokenId, "token-id", "i", "", "The authentication token identifier for the API")
- bridgeConfigureCmd.Flags().BoolVar(&bridgeParams.TokenStdin, "token-stdin", false, "Will read the token from stdin and ignore --token")
- bridgeConfigureCmd.Flags().StringVarP(&bridgeParams.Project, "project", "p", "", "The name of the target repository")
+ bridgeConfigureCmd.Flags().StringVarP(&bridgeConfigureParams.URL, "url", "u", "", "The URL of the target repository")
+ bridgeConfigureCmd.Flags().StringVarP(&bridgeConfigureParams.Owner, "owner", "o", "", "The owner of the target repository")
+ bridgeConfigureCmd.Flags().StringVarP(&bridgeConfigureParams.CredPrefix, "credential", "c", "", "The identifier or prefix of an already known credential for the API (see \"git-bug bridge auth\")")
+ bridgeConfigureCmd.Flags().StringVar(&bridgeConfigureToken, "token", "", "A raw authentication token for the API")
+ bridgeConfigureCmd.Flags().BoolVar(&bridgeConfigureTokenStdin, "token-stdin", false, "Will read the token from stdin and ignore --token")
+ bridgeConfigureCmd.Flags().StringVarP(&bridgeConfigureParams.Project, "project", "p", "", "The name of the target repository")
bridgeConfigureCmd.Flags().SortFlags = false
}
diff --git a/commands/bridge_pull.go b/commands/bridge_pull.go
index 67f19024..bde434cd 100644
--- a/commands/bridge_pull.go
+++ b/commands/bridge_pull.go
@@ -128,7 +128,7 @@ func parseSince(since string) (time.Time, error) {
var bridgePullCmd = &cobra.Command{
Use: "pull [<name>]",
Short: "Pull updates.",
- PreRunE: loadRepo,
+ PreRunE: loadRepoEnsureUser,
RunE: runBridgePull,
Args: cobra.MaximumNArgs(1),
}
diff --git a/commands/bridge_push.go b/commands/bridge_push.go
index 95ad5f5e..52d23a97 100644
--- a/commands/bridge_push.go
+++ b/commands/bridge_push.go
@@ -90,7 +90,7 @@ func runBridgePush(cmd *cobra.Command, args []string) error {
var bridgePushCmd = &cobra.Command{
Use: "push [<name>]",
Short: "Push updates.",
- PreRunE: loadRepo,
+ PreRunE: loadRepoEnsureUser,
RunE: runBridgePush,
Args: cobra.MaximumNArgs(1),
}
diff --git a/commands/comment_add.go b/commands/comment_add.go
index 3e153009..dfd63e38 100644
--- a/commands/comment_add.go
+++ b/commands/comment_add.go
@@ -57,7 +57,7 @@ func runCommentAdd(cmd *cobra.Command, args []string) error {
var commentAddCmd = &cobra.Command{
Use: "add [<id>]",
Short: "Add a new comment to a bug.",
- PreRunE: loadRepo,
+ PreRunE: loadRepoEnsureUser,
RunE: runCommentAdd,
}
diff --git a/commands/label_add.go b/commands/label_add.go
index 6e2679d9..39dfb085 100644
--- a/commands/label_add.go
+++ b/commands/label_add.go
@@ -38,7 +38,7 @@ func runLabelAdd(cmd *cobra.Command, args []string) error {
var labelAddCmd = &cobra.Command{
Use: "add [<id>] <label>[...]",
Short: "Add a label to a bug.",
- PreRunE: loadRepo,
+ PreRunE: loadRepoEnsureUser,
RunE: runLabelAdd,
}
diff --git a/commands/root.go b/commands/root.go
index 387c6342..1a424d32 100644
--- a/commands/root.go
+++ b/commands/root.go
@@ -84,16 +84,10 @@ func loadRepoEnsureUser(cmd *cobra.Command, args []string) error {
return err
}
- set, err := identity.IsUserIdentitySet(repo)
+ _, err = identity.GetUserIdentity(repo)
if err != nil {
return err
}
- if !set {
- // Print the error directly to not confuse a user
- _, _ = fmt.Fprintln(os.Stderr, identity.ErrNoIdentitySet.Error())
- os.Exit(-1)
- }
-
return nil
}
diff --git a/commands/status_close.go b/commands/status_close.go
index 94d05ddf..08c67e87 100644
--- a/commands/status_close.go
+++ b/commands/status_close.go
@@ -31,7 +31,7 @@ func runStatusClose(cmd *cobra.Command, args []string) error {
var closeCmd = &cobra.Command{
Use: "close [<id>]",
Short: "Mark a bug as closed.",
- PreRunE: loadRepo,
+ PreRunE: loadRepoEnsureUser,
RunE: runStatusClose,
}
diff --git a/commands/status_open.go b/commands/status_open.go
index 9a2b76ab..1b1c426e 100644
--- a/commands/status_open.go
+++ b/commands/status_open.go
@@ -31,7 +31,7 @@ func runStatusOpen(cmd *cobra.Command, args []string) error {
var openCmd = &cobra.Command{
Use: "open [<id>]",
Short: "Mark a bug as open.",
- PreRunE: loadRepo,
+ PreRunE: loadRepoEnsureUser,
RunE: runStatusOpen,
}
diff --git a/commands/title_edit.go b/commands/title_edit.go
index 385dbdc9..3e40bd9e 100644
--- a/commands/title_edit.go
+++ b/commands/title_edit.go
@@ -55,7 +55,7 @@ func runTitleEdit(cmd *cobra.Command, args []string) error {
var titleEditCmd = &cobra.Command{
Use: "edit [<id>]",
Short: "Edit a title of a bug.",
- PreRunE: loadRepo,
+ PreRunE: loadRepoEnsureUser,
RunE: runTitleEdit,
}
diff --git a/commands/user.go b/commands/user.go
index 254abf2f..f669c73f 100644
--- a/commands/user.go
+++ b/commands/user.go
@@ -4,9 +4,10 @@ import (
"errors"
"fmt"
+ "github.com/spf13/cobra"
+
"github.com/MichaelMure/git-bug/cache"
"github.com/MichaelMure/git-bug/util/interrupt"
- "github.com/spf13/cobra"
)
var (
@@ -84,7 +85,7 @@ func runUser(cmd *cobra.Command, args []string) error {
var userCmd = &cobra.Command{
Use: "user [<user-id>]",
Short: "Display or change the user identity.",
- PreRunE: loadRepo,
+ PreRunE: loadRepoEnsureUser,
RunE: runUser,
}
diff --git a/commands/user_create.go b/commands/user_create.go
index 037d79b2..88cc94de 100644
--- a/commands/user_create.go
+++ b/commands/user_create.go
@@ -18,10 +18,6 @@ func runUserCreate(cmd *cobra.Command, args []string) error {
defer backend.Close()
interrupt.RegisterCleaner(backend.Close)
- _, _ = fmt.Fprintf(os.Stderr, "Before creating a new identity, please be aware that "+
- "you can also use an already existing one using \"git bug user adopt\". As an example, "+
- "you can do that if your identity has already been created by an importer.\n\n")
-
preName, err := backend.GetUserName()
if err != nil {
return err
diff --git a/commands/webui.go b/commands/webui.go
index 8e735e55..2e1a1bc0 100644
--- a/commands/webui.go
+++ b/commands/webui.go
@@ -249,7 +249,7 @@ var webUICmd = &cobra.Command{
Available git config:
git-bug.webui.open [bool]: control the automatic opening of the web UI in the default browser
`,
- PreRunE: loadRepo,
+ PreRunE: loadRepoEnsureUser,
RunE: runWebUI,
}