From 0381400bfda16f910ca87b0258ddda1c1732fbc7 Mon Sep 17 00:00:00 2001 From: Amine Hilaly Date: Wed, 21 Aug 2019 14:24:48 +0200 Subject: commands: read token from stdin in bridge configuration bridge: improve bridge config validation --- commands/bridge_configure.go | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'commands/bridge_configure.go') diff --git a/commands/bridge_configure.go b/commands/bridge_configure.go index 6bebc262..4cfe0ed5 100644 --- a/commands/bridge_configure.go +++ b/commands/bridge_configure.go @@ -6,10 +6,8 @@ import ( "os" "strconv" "strings" - "syscall" "github.com/spf13/cobra" - "golang.org/x/crypto/ssh/terminal" "github.com/MichaelMure/git-bug/bridge" "github.com/MichaelMure/git-bug/bridge/core" @@ -23,6 +21,7 @@ const ( ) var ( + tokenStdin bool bridgeConfigureName string bridgeConfigureTarget string bridgeParams core.BridgeParams @@ -36,15 +35,6 @@ func runBridgeConfigure(cmd *cobra.Command, args []string) error { defer backend.Close() interrupt.RegisterCleaner(backend.Close) - termState, err := terminal.GetState(int(syscall.Stdin)) - if err != nil { - return err - } - - interrupt.RegisterCleaner(func() error { - return terminal.Restore(int(syscall.Stdin), termState) - }) - if bridgeConfigureTarget == "" { bridgeConfigureTarget, err = promptTarget() if err != nil { @@ -64,6 +54,15 @@ 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 @@ -192,6 +191,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, "Expect to receive token from stdin and ignore token flag.") bridgeConfigureCmd.Flags().StringVarP(&bridgeParams.Project, "project", "p", "", "The name of the target repository") bridgeConfigureCmd.Flags().SortFlags = false } -- cgit