From 7663f2a7f7f5f7bc37790a8c84aaa19ddd782262 Mon Sep 17 00:00:00 2001 From: Navendu Pottekkat Date: Mon, 23 Oct 2023 14:30:45 +0530 Subject: [PATCH 1/3] feat: add token flag and hide token input Signed-off-by: Navendu Pottekkat --- cmd/configure.go | 16 ++++++++++++---- go.mod | 1 + go.sum | 2 ++ 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/cmd/configure.go b/cmd/configure.go index c4b91631..d3389c80 100644 --- a/cmd/configure.go +++ b/cmd/configure.go @@ -17,6 +17,7 @@ import ( "github.com/fatih/color" "github.com/spf13/cobra" "github.com/spf13/viper" + "golang.org/x/term" ) // newConfigureCmd represents the configure command @@ -32,8 +33,9 @@ func newConfigureCmd() *cobra.Command { cmd.Flags().BoolP("overwrite", "f", false, "overwrite existed configuration file") - cmd.Flags().StringP("address", "a", "", "apisix server address") + cmd.Flags().StringP("address", "a", "", "APISIX server address") + cmd.Flags().StringP("token", "t", "", "APISIX token") cmd.Flags().String("capath", "", "ca path for mtls connection") cmd.Flags().String("cert", "", "certificate for mtls connection") cmd.Flags().String("cert-key", "", "certificate key for mtls connection") @@ -56,7 +58,13 @@ func saveConfiguration(cmd *cobra.Command) error { rootConfig.Server, err = cmd.Flags().GetString("address") if err != nil { - color.Red("Failed to get apisix address: %v", err) + color.Red("Failed to get APISIX address: %v", err) + return err + } + + rootConfig.Token, err = cmd.Flags().GetString("token") + if err != nil { + color.Red("Failed to get token: %v", err) return err } @@ -168,11 +176,11 @@ func saveConfiguration(cmd *cobra.Command) error { if rootConfig.Token == "" || overwrite { fmt.Println("Please enter the APISIX token: ") - token, err := reader.ReadString('\n') + byteToken, err := term.ReadPassword(0) if err != nil { return err } - rootConfig.Token = strings.TrimSpace(string(token)) + rootConfig.Token = strings.TrimSpace(string(byteToken)) } // use viper to save the configuration diff --git a/go.mod b/go.mod index 74a79540..aa980b0c 100644 --- a/go.mod +++ b/go.mod @@ -19,6 +19,7 @@ require ( github.com/stretchr/testify v1.8.4 go.uber.org/multierr v1.11.0 go.uber.org/zap v1.26.0 + golang.org/x/term v0.13.0 sigs.k8s.io/yaml v1.3.0 ) diff --git a/go.sum b/go.sum index cea15de0..692637d9 100644 --- a/go.sum +++ b/go.sum @@ -496,6 +496,8 @@ golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE= golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.13.0 h1:bb+I9cTfFazGW51MZqBVmZy7+JEJMouUHTUSKVQLBek= +golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= From 4755f18c16067b4244a82cf4a20a2f1a874ad986 Mon Sep 17 00:00:00 2001 From: Navendu Pottekkat Date: Mon, 23 Oct 2023 15:17:20 +0530 Subject: [PATCH 2/3] use syscall.stdin Signed-off-by: Navendu Pottekkat --- cmd/configure.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cmd/configure.go b/cmd/configure.go index d3389c80..ac1e5fe8 100644 --- a/cmd/configure.go +++ b/cmd/configure.go @@ -13,6 +13,7 @@ import ( "os" "path/filepath" "strings" + "syscall" "github.com/fatih/color" "github.com/spf13/cobra" @@ -176,7 +177,7 @@ func saveConfiguration(cmd *cobra.Command) error { if rootConfig.Token == "" || overwrite { fmt.Println("Please enter the APISIX token: ") - byteToken, err := term.ReadPassword(0) + byteToken, err := term.ReadPassword(syscall.Stdin) if err != nil { return err } From def12f50341f14f7a9de238a13352915977911f8 Mon Sep 17 00:00:00 2001 From: Navendu Pottekkat Date: Thu, 26 Oct 2023 16:01:16 +0530 Subject: [PATCH 3/3] fix: handle non-terminal environments Signed-off-by: Navendu Pottekkat --- cmd/configure.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/cmd/configure.go b/cmd/configure.go index ac1e5fe8..661e641e 100644 --- a/cmd/configure.go +++ b/cmd/configure.go @@ -177,11 +177,19 @@ func saveConfiguration(cmd *cobra.Command) error { if rootConfig.Token == "" || overwrite { fmt.Println("Please enter the APISIX token: ") - byteToken, err := term.ReadPassword(syscall.Stdin) - if err != nil { - return err + if term.IsTerminal(syscall.Stdin) { + token, err := term.ReadPassword(syscall.Stdin) + if err != nil { + return err + } + rootConfig.Token = strings.TrimSpace(string(token)) + } else { + token, err := reader.ReadString('\n') + if err != nil { + return err + } + rootConfig.Token = strings.TrimSpace(string(token)) } - rootConfig.Token = strings.TrimSpace(string(byteToken)) } // use viper to save the configuration