Browse Source

etcdctl: accept user:pass format for add user

Otherwise needed an interactive terminal to create a user.
Anthony Romano 9 years ago
parent
commit
02d9aa481b
2 changed files with 9 additions and 3 deletions
  1. 4 2
      etcdctl/command/user_commands.go
  2. 5 1
      etcdctl/command/util.go

+ 4 - 2
etcdctl/command/user_commands.go

@@ -109,15 +109,17 @@ func actionUserList(c *cli.Context) {
 }
 }
 
 
 func actionUserAdd(c *cli.Context) {
 func actionUserAdd(c *cli.Context) {
-	api, user := mustUserAPIAndName(c)
+	api, userarg := mustUserAPIAndName(c)
 	ctx, cancel := contextWithTotalTimeout(c)
 	ctx, cancel := contextWithTotalTimeout(c)
 	defer cancel()
 	defer cancel()
+	user, _, _ := getUsernamePassword("", userarg+":")
 	currentUser, err := api.GetUser(ctx, user)
 	currentUser, err := api.GetUser(ctx, user)
 	if currentUser != nil {
 	if currentUser != nil {
 		fmt.Fprintf(os.Stderr, "User %s already exists\n", user)
 		fmt.Fprintf(os.Stderr, "User %s already exists\n", user)
 		os.Exit(1)
 		os.Exit(1)
 	}
 	}
-	pass, err := speakeasy.Ask("New password: ")
+
+	_, pass, err := getUsernamePassword("New password: ", userarg)
 	if err != nil {
 	if err != nil {
 		fmt.Fprintln(os.Stderr, "Error reading password:", err)
 		fmt.Fprintln(os.Stderr, "Error reading password:", err)
 		os.Exit(1)
 		os.Exit(1)

+ 5 - 1
etcdctl/command/util.go

@@ -172,11 +172,15 @@ func getTransport(c *cli.Context) (*http.Transport, error) {
 }
 }
 
 
 func getUsernamePasswordFromFlag(usernameFlag string) (username string, password string, err error) {
 func getUsernamePasswordFromFlag(usernameFlag string) (username string, password string, err error) {
+	return getUsernamePassword("Password: ", usernameFlag)
+}
+
+func getUsernamePassword(prompt, usernameFlag string) (username string, password string, err error) {
 	colon := strings.Index(usernameFlag, ":")
 	colon := strings.Index(usernameFlag, ":")
 	if colon == -1 {
 	if colon == -1 {
 		username = usernameFlag
 		username = usernameFlag
 		// Prompt for the password.
 		// Prompt for the password.
-		password, err = speakeasy.Ask("Password: ")
+		password, err = speakeasy.Ask(prompt)
 		if err != nil {
 		if err != nil {
 			return "", "", err
 			return "", "", err
 		}
 		}