Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat: add token flag #79

Merged
merged 3 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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