feat: allow passing password via flag in user password
(#191)
This commit is contained in:
@ -138,11 +138,12 @@ func (c Client) Update(ctx context.Context, params *UpdateParmas) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type SetPasswordParams struct {
|
type SetPasswordParams struct {
|
||||||
Id influxid.ID
|
Id influxid.ID
|
||||||
Name string
|
Name string
|
||||||
|
Password string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c Client) SetPassword(ctx context.Context, params *SetPasswordParams) error {
|
func (c Client) SetPassword(ctx context.Context, params *SetPasswordParams) (err error) {
|
||||||
if !params.Id.Valid() && params.Name == "" {
|
if !params.Id.Valid() && params.Name == "" {
|
||||||
return ErrMustSpecifyUser
|
return ErrMustSpecifyUser
|
||||||
}
|
}
|
||||||
@ -160,9 +161,12 @@ func (c Client) SetPassword(ctx context.Context, params *SetPasswordParams) erro
|
|||||||
id = (*users.Users)[0].GetId()
|
id = (*users.Users)[0].GetId()
|
||||||
}
|
}
|
||||||
|
|
||||||
password, err := c.StdIO.GetPassword(fmt.Sprintf("Please type new password for %q", displayName))
|
password := params.Password
|
||||||
if err != nil {
|
if password == "" {
|
||||||
return err
|
password, err = c.StdIO.GetPassword(fmt.Sprintf("Please type new password for %q", displayName))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
body := api.PasswordResetBody{Password: password}
|
body := api.PasswordResetBody{Password: password}
|
||||||
|
@ -11,7 +11,7 @@ import (
|
|||||||
"github.com/influxdata/influx-cli/v2/api"
|
"github.com/influxdata/influx-cli/v2/api"
|
||||||
"github.com/influxdata/influx-cli/v2/clients"
|
"github.com/influxdata/influx-cli/v2/clients"
|
||||||
"github.com/influxdata/influx-cli/v2/clients/user"
|
"github.com/influxdata/influx-cli/v2/clients/user"
|
||||||
"github.com/influxdata/influx-cli/v2/config"
|
"github.com/influxdata/influx-cli/v2/config"
|
||||||
"github.com/influxdata/influx-cli/v2/internal/mock"
|
"github.com/influxdata/influx-cli/v2/internal/mock"
|
||||||
"github.com/influxdata/influx-cli/v2/internal/testutils"
|
"github.com/influxdata/influx-cli/v2/internal/testutils"
|
||||||
"github.com/influxdata/influx-cli/v2/pkg/influxid"
|
"github.com/influxdata/influx-cli/v2/pkg/influxid"
|
||||||
@ -542,6 +542,24 @@ func TestClient_SetPassword(t *testing.T) {
|
|||||||
})).Return(nil)
|
})).Return(nil)
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "with password via flag",
|
||||||
|
params: user.SetPasswordParams{
|
||||||
|
Id: id2,
|
||||||
|
Password: "mypassword",
|
||||||
|
},
|
||||||
|
noExpectAsk: true,
|
||||||
|
registerExpectations: func(t *testing.T, usersApi *mock.MockUsersApi) {
|
||||||
|
usersApi.EXPECT().PostUsersIDPassword(gomock.Any(), gomock.Eq(id2.String())).
|
||||||
|
Return(api.ApiPostUsersIDPasswordRequest{ApiService: usersApi}.UserID(id2.String()))
|
||||||
|
usersApi.EXPECT().PostUsersIDPasswordExecute(tmock.MatchedBy(func(in api.ApiPostUsersIDPasswordRequest) bool {
|
||||||
|
body := in.GetPasswordResetBody()
|
||||||
|
return assert.NotNil(t, body) &&
|
||||||
|
assert.Equal(t, id2.String(), in.GetUserID()) &&
|
||||||
|
assert.Equal(t, "mypassword", body.GetPassword())
|
||||||
|
})).Return(nil)
|
||||||
|
},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: "user not found",
|
name: "user not found",
|
||||||
params: user.SetPasswordParams{
|
params: user.SetPasswordParams{
|
||||||
|
@ -157,6 +157,11 @@ func newUserSetPasswordCmd() cli.Command {
|
|||||||
Usage: "The user name",
|
Usage: "The user name",
|
||||||
Destination: ¶ms.Name,
|
Destination: ¶ms.Name,
|
||||||
},
|
},
|
||||||
|
&cli.StringFlag{
|
||||||
|
Name: "password, p",
|
||||||
|
Usage: "Password to set on the user",
|
||||||
|
Destination: ¶ms.Password,
|
||||||
|
},
|
||||||
),
|
),
|
||||||
Before: middleware.WithBeforeFns(withCli(), withApi(true)),
|
Before: middleware.WithBeforeFns(withCli(), withApi(true)),
|
||||||
Action: func(ctx *cli.Context) error {
|
Action: func(ctx *cli.Context) error {
|
||||||
|
Reference in New Issue
Block a user