Skip to content

Commit

Permalink
feat: add token flag (#79)
Browse files Browse the repository at this point in the history
* feat: add token flag and hide token input

Signed-off-by: Navendu Pottekkat <navendu@apache.org>

* use syscall.stdin

Signed-off-by: Navendu Pottekkat <navendu@apache.org>

* fix: handle non-terminal environments

Signed-off-by: Navendu Pottekkat <navendu@apache.org>

---------

Signed-off-by: Navendu Pottekkat <navendu@apache.org>
  • Loading branch information
pottekkat committed Oct 30, 2023
1 parent 2feb07e commit faf0547
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
29 changes: 23 additions & 6 deletions cmd/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ import (
"os"
"path/filepath"
"strings"
"syscall"

"github.com/fatih/color"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"golang.org/x/term"
)

// newConfigureCmd represents the configure command
Expand All @@ -32,8 +34,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")
Expand All @@ -56,7 +59,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
}

Expand Down Expand Up @@ -168,11 +177,19 @@ func saveConfiguration(cmd *cobra.Command) error {

if rootConfig.Token == "" || overwrite {
fmt.Println("Please enter the APISIX token: ")
token, err := reader.ReadString('\n')
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(token))
}

// use viper to save the configuration
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit faf0547

Please sign in to comment.