diff --git a/etcdctl/ctlv3/command/user_command.go b/etcdctl/ctlv3/command/user_command.go index c243561de0f3..1c93ded9d39b 100644 --- a/etcdctl/ctlv3/command/user_command.go +++ b/etcdctl/ctlv3/command/user_command.go @@ -47,6 +47,7 @@ func NewUserCommand() *cobra.Command { var ( passwordInteractive bool + passwordFromFlag string ) func newUserAddCommand() *cobra.Command { @@ -57,6 +58,7 @@ func newUserAddCommand() *cobra.Command { } cmd.Flags().BoolVar(&passwordInteractive, "interactive", true, "Read password from stdin instead of interactive terminal") + cmd.Flags().StringVar(&passwordFromFlag, "password", "", "Supply password from the command line flag") return &cmd } @@ -126,19 +128,24 @@ func userAddCommandFunc(cmd *cobra.Command, args []string) { var password string var user string - splitted := strings.SplitN(args[0], ":", 2) - if len(splitted) < 2 { + if passwordFromFlag != "" { user = args[0] - if !passwordInteractive { - fmt.Scanf("%s", &password) - } else { - password = readPasswordInteractive(args[0]) - } + password = passwordFromFlag } else { - user = splitted[0] - password = splitted[1] - if len(user) == 0 { - ExitWithError(ExitBadArgs, fmt.Errorf("empty user name is not allowed.")) + splitted := strings.SplitN(args[0], ":", 2) + if len(splitted) < 2 { + user = args[0] + if !passwordInteractive { + fmt.Scanf("%s", &password) + } else { + password = readPasswordInteractive(args[0]) + } + } else { + user = splitted[0] + password = splitted[1] + if len(user) == 0 { + ExitWithError(ExitBadArgs, fmt.Errorf("empty user name is not allowed.")) + } } }