mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
Merge pull request #6724 from johnbazan/ctlv3_add_user_with_password_inline
etcdctl: allow to add a user within one command line
This commit is contained in:
commit
b64de4707d
@ -39,6 +39,18 @@ func userAddTest(cx ctlCtx) {
|
|||||||
expectedStr: "User username created",
|
expectedStr: "User username created",
|
||||||
stdIn: []string{"password"},
|
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.
|
// Tries to add a user name that already exists.
|
||||||
{
|
{
|
||||||
args: []string{"add", "username", "--interactive=false"},
|
args: []string{"add", "username", "--interactive=false"},
|
||||||
|
@ -124,19 +124,30 @@ func userAddCommandFunc(cmd *cobra.Command, args []string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var password string
|
var password string
|
||||||
|
var user string
|
||||||
|
|
||||||
|
splitted := strings.SplitN(args[0], ":", 2)
|
||||||
|
if len(splitted) < 2 {
|
||||||
|
user = args[0]
|
||||||
if !passwordInteractive {
|
if !passwordInteractive {
|
||||||
fmt.Scanf("%s", &password)
|
fmt.Scanf("%s", &password)
|
||||||
} else {
|
} else {
|
||||||
password = readPasswordInteractive(args[0])
|
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."))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
_, err := mustClientFromCmd(cmd).Auth.UserAdd(context.TODO(), args[0], password)
|
_, err := mustClientFromCmd(cmd).Auth.UserAdd(context.TODO(), user, password)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ExitWithError(ExitError, err)
|
ExitWithError(ExitError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("User %s created\n", args[0])
|
fmt.Printf("User %s created\n", user)
|
||||||
}
|
}
|
||||||
|
|
||||||
// userDeleteCommandFunc executes the "user delete" command.
|
// userDeleteCommandFunc executes the "user delete" command.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user