From 73b4a58ac06abb526fc35e25f14df87de0279191 Mon Sep 17 00:00:00 2001 From: Jonathan Bazan Date: Tue, 25 Oct 2016 12:55:30 -0300 Subject: [PATCH 1/2] etcdctl: allow to add a user within one command line This makes the "user add usr:pwd" feature available for ctlv3 without asking for the password in a new prompt. --- etcdctl/ctlv3/command/user_command.go | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/etcdctl/ctlv3/command/user_command.go b/etcdctl/ctlv3/command/user_command.go index fb09a27b9..481cccf0d 100644 --- a/etcdctl/ctlv3/command/user_command.go +++ b/etcdctl/ctlv3/command/user_command.go @@ -124,19 +124,30 @@ func userAddCommandFunc(cmd *cobra.Command, args []string) { } var password string + var user string - if !passwordInteractive { - fmt.Scanf("%s", &password) + 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 { - password = readPasswordInteractive(args[0]) + user = splitted[0] + password = splitted[1] + if len(user) == 0 { + ExitWithError(ExitBadArgs, fmt.Errorf("empty user name is not allowed.")) + } } - _, err := mustClientFromCmd(cmd).Auth.UserAdd(context.TODO(), args[0], password) + _, err := mustClientFromCmd(cmd).Auth.UserAdd(context.TODO(), user, password) if err != nil { ExitWithError(ExitError, err) } - fmt.Printf("User %s created\n", args[0]) + fmt.Printf("User %s created\n", user) } // userDeleteCommandFunc executes the "user delete" command. From d51a7dba43856f9518f01359c922170a862b1221 Mon Sep 17 00:00:00 2001 From: Jonathan Bazan Date: Wed, 26 Oct 2016 15:02:36 -0300 Subject: [PATCH 2/2] etcdctl: Adding e2e tests for userAddTest --- e2e/ctl_v3_user_test.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/e2e/ctl_v3_user_test.go b/e2e/ctl_v3_user_test.go index 85cbdd466..392ab4e42 100644 --- a/e2e/ctl_v3_user_test.go +++ b/e2e/ctl_v3_user_test.go @@ -39,6 +39,18 @@ func userAddTest(cx ctlCtx) { expectedStr: "User username created", stdIn: []string{"password"}, }, + // Adds a user name using the usertest:password syntax. + { + args: []string{"add", "usertest:password"}, + expectedStr: "User usertest created", + stdIn: []string{}, + }, + // Tries to add a user with empty username. + { + args: []string{"add", ":password"}, + expectedStr: "empty user name is not allowed.", + stdIn: []string{}, + }, // Tries to add a user name that already exists. { args: []string{"add", "username", "--interactive=false"},