diff options
Diffstat (limited to 'commands')
-rw-r--r-- | commands/bridge_token.go | 26 | ||||
-rw-r--r-- | commands/bridge_token_add.go | 51 | ||||
-rw-r--r-- | commands/bridge_token_rm.go | 15 |
3 files changed, 63 insertions, 29 deletions
diff --git a/commands/bridge_token.go b/commands/bridge_token.go index 7467691e..b9e2d49f 100644 --- a/commands/bridge_token.go +++ b/commands/bridge_token.go @@ -6,27 +6,14 @@ import ( "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" text "github.com/MichaelMure/go-term-text" -) -var ( - bridgeTokenLocal bool - bridgeTokenGlobal bool + "github.com/MichaelMure/git-bug/bridge/core" + "github.com/MichaelMure/git-bug/util/colors" ) func runTokenBridge(cmd *cobra.Command, args []string) error { - backend, err := cache.NewRepoCache(repo) - if err != nil { - return err - } - defer backend.Close() - interrupt.RegisterCleaner(backend.Close) - - tokens, err := core.ListTokens(backend) + tokens, err := core.ListTokens(repo) if err != nil { return err } @@ -43,13 +30,12 @@ func runTokenBridge(cmd *cobra.Command, args []string) error { } func printToken(token *core.Token) { - idFmt := text.LeftPadMaxLine(token.ID.Human(), 7, 0) valueFmt := text.LeftPadMaxLine(token.Value, 15, 0) targetFmt := text.LeftPadMaxLine(token.Target, 7, 0) createTimeFmt := text.LeftPadMaxLine(token.CreateTime.Format(time.RFC822), 20, 0) fmt.Printf("%s %s %s %s\n", - idFmt, + token.ID().Human(), colors.Magenta(targetFmt), valueFmt, createTimeFmt, @@ -58,7 +44,7 @@ func printToken(token *core.Token) { var bridgeTokenCmd = &cobra.Command{ Use: "token", - Short: "List all stored tokens.", + Short: "List all known tokens.", PreRunE: loadRepo, RunE: runTokenBridge, Args: cobra.NoArgs, @@ -66,7 +52,5 @@ var bridgeTokenCmd = &cobra.Command{ func init() { bridgeCmd.AddCommand(bridgeTokenCmd) - bridgeTokenCmd.Flags().BoolVarP(&bridgeTokenLocal, "local", "l", false, "") - bridgeTokenCmd.Flags().BoolVarP(&bridgeTokenGlobal, "global", "g", false, "") bridgeTokenCmd.Flags().SortFlags = false } diff --git a/commands/bridge_token_add.go b/commands/bridge_token_add.go index 58e9f472..d2c697fa 100644 --- a/commands/bridge_token_add.go +++ b/commands/bridge_token_add.go @@ -1,37 +1,74 @@ 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 ( - bridgeTokenValue string bridgeTokenTarget string ) func runBridgeTokenAdd(cmd *cobra.Command, args []string) error { - token := core.NewToken(bridgeTokenValue, bridgeTokenTarget) + var value string + + if bridgeTokenTarget == "" { + return fmt.Errorf("token target is required") + } + + if !core.TargetExist(bridgeTokenTarget) { + 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, bridgeTokenTarget) if err := token.Validate(); err != nil { return errors.Wrap(err, "invalid token") } - return core.StoreToken(repo, token) + err := core.StoreToken(repo, token) + if err != nil { + return err + } + + fmt.Printf("token %s added\n", token.ID()) + return nil } var bridgeTokenAddCmd = &cobra.Command{ Use: "add", - Short: "Create and store a new token", + Short: "Store a new token", PreRunE: loadRepo, RunE: runBridgeTokenAdd, - Args: cobra.NoArgs, + Args: cobra.MaximumNArgs(1), } func init() { bridgeTokenCmd.AddCommand(bridgeTokenAddCmd) - bridgeTokenAddCmd.Flags().StringVarP(&bridgeTokenValue, "value", "v", "", "") - bridgeTokenAddCmd.Flags().StringVarP(&bridgeTokenTarget, "target", "t", "", "") + bridgeTokenAddCmd.Flags().StringVarP(&bridgeTokenTarget, "target", "t", "", + fmt.Sprintf("The target of the bridge. Valid values are [%s]", strings.Join(bridge.Targets(), ","))) bridgeTokenAddCmd.Flags().SortFlags = false } diff --git a/commands/bridge_token_rm.go b/commands/bridge_token_rm.go index 7296f95a..1f4ca4e8 100644 --- a/commands/bridge_token_rm.go +++ b/commands/bridge_token_rm.go @@ -1,13 +1,26 @@ package commands import ( + "fmt" + "github.com/spf13/cobra" "github.com/MichaelMure/git-bug/bridge/core" ) func runBridgeTokenRm(cmd *cobra.Command, args []string) error { - return core.RemoveToken(repo, args[0]) + token, err := core.LoadTokenPrefix(repo, args[0]) + if err != nil { + return err + } + + err = core.RemoveToken(repo, token.ID()) + if err != nil { + return err + } + + fmt.Printf("token %s removed\n", token.ID()) + return nil } var bridgeTokenRmCmd = &cobra.Command{ |