From 7cad611728e8717e9afa2d2cb90681ad6a3aba42 Mon Sep 17 00:00:00 2001 From: Smith <125195487+sigmaith@users.noreply.github.com> Date: Mon, 2 Sep 2024 16:30:27 +0900 Subject: [PATCH] Refactor and modify required flag phrase --- cmd/yorkie/user/change-password.go | 60 +++++++++++++++++++----------- cmd/yorkie/user/delete-account.go | 3 +- cmd/yorkie/user/login.go | 2 +- 3 files changed, 42 insertions(+), 23 deletions(-) diff --git a/cmd/yorkie/user/change-password.go b/cmd/yorkie/user/change-password.go index cf64ccb80..ca4e51f6a 100644 --- a/cmd/yorkie/user/change-password.go +++ b/cmd/yorkie/user/change-password.go @@ -39,21 +39,10 @@ func changePasswordCmd() *cobra.Command { Short: "Change user password", PreRunE: config.Preload, RunE: func(cmd *cobra.Command, args []string) error { - fmt.Print("Enter Password: ") - bytePassword, err := term.ReadPassword(int(os.Stdin.Fd())) + password, newPassword, err := getPasswords() if err != nil { - return fmt.Errorf("failed to read password: %w", err) - } - password = string(bytePassword) - fmt.Println() - - fmt.Print("Enter New Password: ") - bytePassword, err = term.ReadPassword(int(os.Stdin.Fd())) - if err != nil { - return fmt.Errorf("failed to read password: %w", err) + return err } - newPassword = string(bytePassword) - fmt.Println() if rpcAddr == "" { rpcAddr = viper.GetString("rpcAddr") @@ -72,13 +61,7 @@ func changePasswordCmd() *cobra.Command { return err } - conf, err := config.Load() - if err != nil { - return err - } - - delete(conf.Auths, rpcAddr) - if err := config.Save(conf); err != nil { + if err := deleteAuthSession(rpcAddr); err != nil { return err } @@ -87,6 +70,40 @@ func changePasswordCmd() *cobra.Command { } } +func getPasswords() (string, string, error) { + fmt.Print("Enter Password: ") + bytePassword, err := term.ReadPassword(int(os.Stdin.Fd())) + if err != nil { + return "", "", fmt.Errorf("failed to read password: %w", err) + } + password := string(bytePassword) + fmt.Println() + + fmt.Print("Enter New Password: ") + bytePassword, err = term.ReadPassword(int(os.Stdin.Fd())) + if err != nil { + return "", "", fmt.Errorf("failed to read password: %w", err) + } + newPassword := string(bytePassword) + fmt.Println() + + return password, newPassword, nil +} + +func deleteAuthSession(rpcAddr string) error { + conf, err := config.Load() + if err != nil { + return err + } + + delete(conf.Auths, rpcAddr) + if err := config.Save(conf); err != nil { + return err + } + + return nil +} + func init() { cmd := changePasswordCmd() cmd.Flags().StringVarP( @@ -94,7 +111,7 @@ func init() { "username", "u", "", - "Username (required if password is set)", + "Username (required)", ) cmd.Flags().StringVar( &rpcAddr, @@ -108,5 +125,6 @@ func init() { false, "Skip the TLS connection of the client", ) + _ = cmd.MarkFlagRequired("username") SubCmd.AddCommand(cmd) } diff --git a/cmd/yorkie/user/delete-account.go b/cmd/yorkie/user/delete-account.go index 50e447cbb..6e0eaf28b 100644 --- a/cmd/yorkie/user/delete-account.go +++ b/cmd/yorkie/user/delete-account.go @@ -117,7 +117,7 @@ func init() { "username", "u", "", - "Username (required if password is set)", + "Username (required)", ) cmd.Flags().StringVar( &rpcAddr, @@ -131,5 +131,6 @@ func init() { false, "Skip the TLS connection of the client", ) + _ = cmd.MarkFlagRequired("username") SubCmd.AddCommand(cmd) } diff --git a/cmd/yorkie/user/login.go b/cmd/yorkie/user/login.go index 79fb6af11..ac39a6b1b 100644 --- a/cmd/yorkie/user/login.go +++ b/cmd/yorkie/user/login.go @@ -93,7 +93,7 @@ func init() { "username", "u", "", - "Username (required if password is set)", + "Username (required)", ) cmd.Flags().StringVar( &rpcAddr,