aboutsummaryrefslogtreecommitdiffstats
path: root/commands/bridge_token_add.go
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2019-11-10 14:46:55 +0100
committerMichael Muré <batolettre@gmail.com>2019-11-10 14:53:40 +0100
commite2445edcb99ed0aa7fcd40fe7484d71e73367334 (patch)
treef5f38c8d6d6f426631fda7feaddef34332b6040c /commands/bridge_token_add.go
parent45653bd31d9ec91123ee42803cd5c46b43d18528 (diff)
downloadgit-bug-e2445edcb99ed0aa7fcd40fe7484d71e73367334.tar.gz
bridge: various improvement on the global token PR
Diffstat (limited to 'commands/bridge_token_add.go')
-rw-r--r--commands/bridge_token_add.go51
1 files changed, 44 insertions, 7 deletions
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
}