Skip to content

Commit

Permalink
Refactor and modify required flag phrase
Browse files Browse the repository at this point in the history
  • Loading branch information
sigmaith committed Sep 2, 2024
1 parent 7609511 commit 7cad611
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 23 deletions.
60 changes: 39 additions & 21 deletions cmd/yorkie/user/change-password.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
}

Expand All @@ -87,14 +70,48 @@ 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(
&username,
"username",
"u",
"",
"Username (required if password is set)",
"Username (required)",
)
cmd.Flags().StringVar(
&rpcAddr,
Expand All @@ -108,5 +125,6 @@ func init() {
false,
"Skip the TLS connection of the client",
)
_ = cmd.MarkFlagRequired("username")
SubCmd.AddCommand(cmd)
}
3 changes: 2 additions & 1 deletion cmd/yorkie/user/delete-account.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func init() {
"username",
"u",
"",
"Username (required if password is set)",
"Username (required)",
)
cmd.Flags().StringVar(
&rpcAddr,
Expand All @@ -131,5 +131,6 @@ func init() {
false,
"Skip the TLS connection of the client",
)
_ = cmd.MarkFlagRequired("username")
SubCmd.AddCommand(cmd)
}
2 changes: 1 addition & 1 deletion cmd/yorkie/user/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func init() {
"username",
"u",
"",
"Username (required if password is set)",
"Username (required)",
)
cmd.Flags().StringVar(
&rpcAddr,
Expand Down

0 comments on commit 7cad611

Please sign in to comment.