etcdctl: accept user:pass format for add user

Otherwise needed an interactive terminal to create a user.
This commit is contained in:
Anthony Romano 2016-03-04 13:29:06 -08:00
parent 3b185f130a
commit 02d9aa481b
2 changed files with 9 additions and 3 deletions

View File

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

View File

@ -172,11 +172,15 @@ func getTransport(c *cli.Context) (*http.Transport, 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, ":")
if colon == -1 {
username = usernameFlag
// Prompt for the password.
password, err = speakeasy.Ask("Password: ")
password, err = speakeasy.Ask(prompt)
if err != nil {
return "", "", err
}