Skip to content

Commit

Permalink
feat(cmd/influx): update password in cli
Browse files Browse the repository at this point in the history
  • Loading branch information
kelwang committed Jan 14, 2020
1 parent 10b033c commit 639d8db
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 4 deletions.
12 changes: 8 additions & 4 deletions cmd/influx/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func interactive() (req *platform.OnboardingRequest, err error) {
if setupFlags.password != "" {
req.Password = setupFlags.password
} else {
req.Password = getPassword(ui)
req.Password = getPassword(ui, false)
}
if setupFlags.token != "" {
req.Token = setupFlags.token
Expand Down Expand Up @@ -240,10 +240,14 @@ var (
errPasswordIsTooShort = fmt.Errorf("passwords is too short")
)

func getPassword(ui *input.UI) (password string) {
func getPassword(ui *input.UI, showNew bool) (password string) {
newStr := ""
if showNew {
newStr = " new"
}
var err error
enterPasswd:
query := promptWithColor("Please type your password", colorCyan)
query := promptWithColor("Please type your"+newStr+" password", colorCyan)
for {
password, err = ui.Ask(query, &input.Options{
Required: true,
Expand All @@ -269,7 +273,7 @@ enterPasswd:
}
break
}
query = promptWithColor("Please type your password again", colorCyan)
query = promptWithColor("Please type your"+newStr+" password again", colorCyan)
for {
_, err = ui.Ask(query, &input.Options{
Required: true,
Expand Down
69 changes: 69 additions & 0 deletions cmd/influx/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package main
import (
"context"
"errors"
"fmt"
"os"

platform "github.com/influxdata/influxdb"
"github.com/influxdata/influxdb/cmd/influx/internal"
"github.com/influxdata/influxdb/http"
"github.com/spf13/cobra"
input "github.com/tcnksm/go-input"
)

func cmdUser() *cobra.Command {
Expand All @@ -22,6 +24,7 @@ func cmdUser() *cobra.Command {
userDeleteCmd(),
userFindCmd(),
userUpdateCmd(),
userUpdatePasswordCmd(),
)

return cmd
Expand All @@ -32,6 +35,19 @@ var userUpdateFlags struct {
name string
}

func userUpdatePasswordCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "password",
Short: "Update user password",
RunE: wrapCheckSetup(userUpdatePasswordF),
}

cmd.Flags().StringVarP(&userFindFlags.id, "id", "i", "", "The user ID")
cmd.Flags().StringVarP(&userFindFlags.name, "name", "n", "", "The user name")

return cmd
}

func userUpdateCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "update",
Expand All @@ -46,6 +62,20 @@ func userUpdateCmd() *cobra.Command {
return cmd
}

func newPasswordService() (platform.PasswordsService, error) {
if flags.local {
return newLocalKVService()
}

client, err := newHTTPClient()
if err != nil {
return nil, err
}
return &http.PasswordService{
Client: client,
}, nil
}

func newUserService() (platform.UserService, error) {
if flags.local {
return newLocalKVService()
Expand All @@ -60,6 +90,45 @@ func newUserService() (platform.UserService, error) {
}, nil
}

func userUpdatePasswordF(cmd *cobra.Command, args []string) error {
ctx := context.Background()
us, err := newUserService()
if err != nil {
return err
}

filter := platform.UserFilter{}
if userFindFlags.name != "" {
filter.Name = &userFindFlags.name
}
if userFindFlags.id != "" {
id, err := platform.IDFromString(userFindFlags.id)
if err != nil {
return err
}
filter.ID = id
}
u, err := us.FindUser(ctx, filter)
if err != nil {
return err
}
ui := &input.UI{
Writer: os.Stdout,
Reader: os.Stdin,
}
password := getPassword(ui, true)

ps, err := newPasswordService()
if err != nil {
return err
}
if err = ps.SetPassword(ctx, u.ID, password); err != nil {
return err
}
fmt.Println("Your password has been successfully updated.")
return nil
}

func userUpdateF(cmd *cobra.Command, args []string) error {
s, err := newUserService()
if err != nil {
Expand Down

0 comments on commit 639d8db

Please sign in to comment.