From b92adfcb5f79f2b32c3dafb0fc3e7f1b753b6197 Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Sun, 8 Dec 2019 21:15:06 +0100 Subject: bridge: huge refactor to accept multiple kind of credentials --- commands/add.go | 2 +- commands/bridge_auth.go | 52 ++++++++++++++++++-------- commands/bridge_auth_add.go | 74 ------------------------------------ commands/bridge_auth_addtoken.go | 81 ++++++++++++++++++++++++++++++++++++++++ commands/bridge_auth_rm.go | 8 ++-- commands/bridge_auth_show.go | 18 +++++---- commands/bridge_configure.go | 50 ++++++++++++++++++------- commands/bridge_pull.go | 2 +- commands/bridge_push.go | 2 +- commands/comment_add.go | 2 +- commands/label_add.go | 2 +- commands/root.go | 8 +--- commands/status_close.go | 2 +- commands/status_open.go | 2 +- commands/title_edit.go | 2 +- commands/user.go | 5 ++- commands/user_create.go | 4 -- commands/webui.go | 2 +- 18 files changed, 181 insertions(+), 137 deletions(-) delete mode 100644 commands/bridge_auth_add.go create mode 100644 commands/bridge_auth_addtoken.go (limited to 'commands') 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_add.go deleted file mode 100644 index ae2c4dbc..00000000 --- a/commands/bridge_auth_add.go +++ /dev/null @@ -1,74 +0,0 @@ -package commands - -import ( - "bufio" - "fmt" - "os" - "strings" - - "github.com/mattn/go-isatty" - "github.com/pkg/errors" - "github.com/spf13/cobra" - - "github.com/MichaelMure/git-bug/bridge" - "github.com/MichaelMure/git-bug/bridge/core" -) - -var ( - bridgeAuthAddTokenTarget string -) - -func runBridgeTokenAdd(cmd *cobra.Command, args []string) error { - var value string - - if bridgeAuthAddTokenTarget == "" { - return fmt.Errorf("auth target is required") - } - - if !core.TargetExist(bridgeAuthAddTokenTarget) { - return fmt.Errorf("unknown target") - } - - if len(args) == 1 { - value = args[0] - } else { - // Read from Stdin - if isatty.IsTerminal(os.Stdin.Fd()) { - fmt.Println("Enter the token:") - } - reader := bufio.NewReader(os.Stdin) - raw, err := reader.ReadString('\n') - if err != nil { - return fmt.Errorf("reading from stdin: %v", err) - } - value = strings.TrimSuffix(raw, "\n") - } - - token := core.NewToken(value, bridgeAuthAddTokenTarget) - if err := token.Validate(); err != nil { - return errors.Wrap(err, "invalid token") - } - - err := core.StoreToken(repo, token) - if err != nil { - return err - } - - fmt.Printf("token %s added\n", token.ID()) - return nil -} - -var bridgeAuthAddTokenCmd = &cobra.Command{ - Use: "add-token []", - Short: "Store a new token", - PreRunE: loadRepo, - RunE: runBridgeTokenAdd, - Args: cobra.MaximumNArgs(1), -} - -func init() { - bridgeAuthCmd.AddCommand(bridgeAuthAddTokenCmd) - bridgeAuthAddTokenCmd.Flags().StringVarP(&bridgeAuthAddTokenTarget, "target", "t", "", - fmt.Sprintf("The target of the bridge. Valid values are [%s]", strings.Join(bridge.Targets(), ","))) - bridgeAuthAddTokenCmd.Flags().SortFlags = false -} diff --git a/commands/bridge_auth_addtoken.go b/commands/bridge_auth_addtoken.go new file mode 100644 index 00000000..018015e4 --- /dev/null +++ b/commands/bridge_auth_addtoken.go @@ -0,0 +1,81 @@ +package commands + +import ( + "bufio" + "fmt" + "os" + "strings" + + "github.com/mattn/go-isatty" + "github.com/pkg/errors" + "github.com/spf13/cobra" + + "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 ( + bridgeAuthAddTokenTarget string +) + +func runBridgeTokenAdd(cmd *cobra.Command, args []string) error { + var value string + + if bridgeAuthAddTokenTarget == "" { + return fmt.Errorf("flag --target is required") + } + + if !core.TargetExist(bridgeAuthAddTokenTarget) { + return fmt.Errorf("unknown target") + } + + if len(args) == 1 { + value = args[0] + } else { + // Read from Stdin + if isatty.IsTerminal(os.Stdin.Fd()) { + fmt.Println("Enter the token:") + } + reader := bufio.NewReader(os.Stdin) + raw, err := reader.ReadString('\n') + if err != nil { + return fmt.Errorf("reading from stdin: %v", err) + } + value = strings.TrimSuffix(raw, "\n") + } + + 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 = auth.Store(repo, token) + if err != nil { + return err + } + + fmt.Printf("token %s added\n", token.ID()) + return nil +} + +var bridgeAuthAddTokenCmd = &cobra.Command{ + Use: "add-token []", + Short: "Store a new token", + PreRunE: loadRepoEnsureUser, + RunE: runBridgeTokenAdd, + Args: cobra.MaximumNArgs(1), +} + +func init() { + bridgeAuthCmd.AddCommand(bridgeAuthAddTokenCmd) + bridgeAuthAddTokenCmd.Flags().StringVarP(&bridgeAuthAddTokenTarget, "target", "t", "", + fmt.Sprintf("The target of the bridge. Valid values are [%s]", strings.Join(bridge.Targets(), ","))) + bridgeAuthAddTokenCmd.Flags().SortFlags = false +} 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 []", 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 []", 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 []", 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 []