From f3d8da10750d58fce482042cff87455a1e6f36e0 Mon Sep 17 00:00:00 2001 From: Amine Hilaly Date: Mon, 2 Sep 2019 09:24:53 +0200 Subject: bridge/core: add tokenStdin field to bridgeParams commands: move tokenStdin handling logic to bridge.Configure --- bridge/core/bridge.go | 9 +++++---- bridge/github/config.go | 9 ++++++++- bridge/gitlab/config.go | 9 ++++++++- commands/bridge_configure.go | 16 +++------------- 4 files changed, 24 insertions(+), 19 deletions(-) diff --git a/bridge/core/bridge.go b/bridge/core/bridge.go index 9161b418..6fd28b03 100644 --- a/bridge/core/bridge.go +++ b/bridge/core/bridge.go @@ -31,10 +31,11 @@ var bridgeImpl map[string]reflect.Type // BridgeParams holds parameters to simplify the bridge configuration without // having to make terminal prompts. type BridgeParams struct { - Owner string - Project string - URL string - Token string + Owner string + Project string + URL string + Token string + TokenStdin bool } // Bridge is a wrapper around a BridgeImpl that will bind low-level diff --git a/bridge/github/config.go b/bridge/github/config.go index bc4911e0..16abfa09 100644 --- a/bridge/github/config.go +++ b/bridge/github/config.go @@ -45,7 +45,7 @@ func (g *Github) Configure(repo repository.RepoCommon, params core.BridgeParams) var owner string var project string - if params.Token != "" && + if (params.Token != "" || params.TokenStdin) && (params.URL == "" && (params.Project == "" || params.Owner == "")) { return nil, fmt.Errorf("you must provide a project URL or Owner/Name to configure this bridge with a token") } @@ -91,6 +91,13 @@ func (g *Github) Configure(repo repository.RepoCommon, params core.BridgeParams) if params.Token != "" { token = params.Token + } else if params.TokenStdin { + reader := bufio.NewReader(os.Stdin) + token, err = reader.ReadString('\n') + if err != nil { + return nil, fmt.Errorf("reading from stdin: %v", err) + } + token = strings.TrimSuffix(token, "\n") } else { token, err = promptTokenOptions(owner, project) if err != nil { diff --git a/bridge/gitlab/config.go b/bridge/gitlab/config.go index d1d85030..a673af8c 100644 --- a/bridge/gitlab/config.go +++ b/bridge/gitlab/config.go @@ -33,7 +33,7 @@ func (g *Gitlab) Configure(repo repository.RepoCommon, params core.BridgeParams) var url string var token string - if params.Token != "" && params.URL == "" { + if (params.Token != "" || params.TokenStdin) && params.URL == "" { return nil, fmt.Errorf("you must provide a project URL to configure this bridge with a token") } @@ -58,6 +58,13 @@ func (g *Gitlab) Configure(repo repository.RepoCommon, params core.BridgeParams) // get user token if params.Token != "" { token = params.Token + } else if params.TokenStdin { + reader := bufio.NewReader(os.Stdin) + token, err = reader.ReadString('\n') + if err != nil { + return nil, fmt.Errorf("reading from stdin: %v", err) + } + token = strings.TrimSuffix(token, "\n") } else { token, err = promptToken() if err != nil { diff --git a/commands/bridge_configure.go b/commands/bridge_configure.go index bb32c84e..12cc35e3 100644 --- a/commands/bridge_configure.go +++ b/commands/bridge_configure.go @@ -21,7 +21,6 @@ const ( ) var ( - tokenStdin bool bridgeConfigureName string bridgeConfigureTarget string bridgeParams core.BridgeParams @@ -35,8 +34,8 @@ func runBridgeConfigure(cmd *cobra.Command, args []string) error { defer backend.Close() interrupt.RegisterCleaner(backend.Close) - if (tokenStdin || bridgeParams.Token != "") && (bridgeConfigureName == "" || bridgeConfigureTarget == "") { - return fmt.Errorf("you must bridge name and target to configure a bridge with a token") + if (bridgeParams.TokenStdin || bridgeParams.Token != "") && (bridgeConfigureName == "" || bridgeConfigureTarget == "") { + return fmt.Errorf("you must provide a bridge name and target to configure a bridge with a token") } if bridgeConfigureTarget == "" { @@ -58,15 +57,6 @@ func runBridgeConfigure(cmd *cobra.Command, args []string) error { return err } - if tokenStdin { - reader := bufio.NewReader(os.Stdin) - token, err := reader.ReadString('\n') - if err != nil { - return fmt.Errorf("reading from stdin: %v", err) - } - bridgeParams.Token = strings.TrimSuffix(token, "\n") - } - err = b.Configure(bridgeParams) if err != nil { return err @@ -195,7 +185,7 @@ func init() { 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().BoolVar(&tokenStdin, "token-stdin", false, "Will read the token from stdin and ignore --token") + 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().SortFlags = false } -- cgit