Skip to content

Commit

Permalink
chore(cmd/influx): refactor user.go
Browse files Browse the repository at this point in the history
feat(cmd/influx): add user tests
fix(cmd/influx): make golint happier
  • Loading branch information
kelwang committed Jan 15, 2020
1 parent b5d14f9 commit a29ccea
Show file tree
Hide file tree
Showing 6 changed files with 585 additions and 171 deletions.
9 changes: 5 additions & 4 deletions cmd/influx/bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"reflect"
"testing"
"io/ioutil"
"time"

"github.com/influxdata/influxdb"
Expand Down Expand Up @@ -102,7 +103,7 @@ func TestCmdBucket(t *testing.T) {
return nil
}

builder := newCmdBucketBuilder(fakeSVCFn(svc), out(new(bytes.Buffer)))
builder := newCmdBucketBuilder(fakeSVCFn(svc), out(ioutil.Discard))
cmd := builder.cmdCreate()
cmd.RunE = builder.cmdCreateRunEFn
return cmd
Expand Down Expand Up @@ -151,7 +152,7 @@ func TestCmdBucket(t *testing.T) {
return nil
}

builder := newCmdBucketBuilder(fakeSVCFn(svc), out(new(bytes.Buffer)))
builder := newCmdBucketBuilder(fakeSVCFn(svc), out(ioutil.Discard))
cmd := builder.cmdDelete()
cmd.RunE = builder.cmdDeleteRunEFn
return cmd
Expand Down Expand Up @@ -258,7 +259,7 @@ func TestCmdBucket(t *testing.T) {
return nil, 0, nil
}

builder := newCmdBucketBuilder(fakeSVCFn(svc), in(new(bytes.Buffer)), out(new(bytes.Buffer)))
builder := newCmdBucketBuilder(fakeSVCFn(svc), in(new(bytes.Buffer)), out(ioutil.Discard))
cmd := builder.cmdFind()
cmd.RunE = builder.cmdFindRunEFn
return cmd, calls
Expand Down Expand Up @@ -352,7 +353,7 @@ func TestCmdBucket(t *testing.T) {
return &influxdb.Bucket{}, nil
}

builder := newCmdBucketBuilder(fakeSVCFn(svc), out(new(bytes.Buffer)))
builder := newCmdBucketBuilder(fakeSVCFn(svc), out(ioutil.Discard))
cmd := builder.cmdUpdate()
cmd.RunE = builder.cmdUpdateRunEFn
return cmd
Expand Down
2 changes: 1 addition & 1 deletion cmd/influx/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func influxCmd(opts ...genericCLIOptFn) *cobra.Command {
cmdREPL(),
cmdSetup(),
cmdTask(),
cmdUser(),
cmdUser(runEWrapper),
cmdWrite(),
)

Expand Down
15 changes: 8 additions & 7 deletions cmd/influx/organization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"fmt"
"io/ioutil"
"reflect"
"testing"

Expand Down Expand Up @@ -63,7 +64,7 @@ func TestCmdOrg(t *testing.T) {
return nil
}

builder := newCmdOrgBuilder(fakeOrgSVCFn(svc), out(new(bytes.Buffer)))
builder := newCmdOrgBuilder(fakeOrgSVCFn(svc), out(ioutil.Discard))
cmd := builder.cmdCreate()
return cmd
}
Expand Down Expand Up @@ -109,7 +110,7 @@ func TestCmdOrg(t *testing.T) {
return nil
}

builder := newCmdOrgBuilder(fakeOrgSVCFn(svc), out(new(bytes.Buffer)))
builder := newCmdOrgBuilder(fakeOrgSVCFn(svc), out(ioutil.Discard))
cmd := builder.cmdDelete()
return cmd
}
Expand Down Expand Up @@ -181,7 +182,7 @@ func TestCmdOrg(t *testing.T) {
return nil, 0, nil
}

builder := newCmdOrgBuilder(fakeOrgSVCFn(svc), in(new(bytes.Buffer)), out(new(bytes.Buffer)))
builder := newCmdOrgBuilder(fakeOrgSVCFn(svc), in(new(bytes.Buffer)), out(ioutil.Discard))
cmd := builder.cmdFind()
return cmd, calls
}
Expand Down Expand Up @@ -268,7 +269,7 @@ func TestCmdOrg(t *testing.T) {
return &influxdb.Organization{}, nil
}

builder := newCmdOrgBuilder(fakeOrgSVCFn(svc), out(new(bytes.Buffer)))
builder := newCmdOrgBuilder(fakeOrgSVCFn(svc), out(ioutil.Discard))
cmd := builder.cmdUpdate()
return cmd
}
Expand Down Expand Up @@ -366,7 +367,7 @@ func TestCmdOrg(t *testing.T) {
return &influxdb.Organization{ID: 1}, nil
}

builder := newCmdOrgBuilder(fakeOrgSVCFn(svc), in(new(bytes.Buffer)), out(new(bytes.Buffer)))
builder := newCmdOrgBuilder(fakeOrgSVCFn(svc), in(new(bytes.Buffer)), out(ioutil.Discard))
cmd := builder.cmdMemberList()
return cmd, calls
}
Expand Down Expand Up @@ -394,7 +395,7 @@ func TestCmdOrg(t *testing.T) {
return nil
}

builder := newCmdOrgBuilder(fakeOrgUrmSVCsFn(svc, urmSVC), in(new(bytes.Buffer)), out(new(bytes.Buffer)))
builder := newCmdOrgBuilder(fakeOrgUrmSVCsFn(svc, urmSVC), in(new(bytes.Buffer)), out(ioutil.Discard))
cmd := builder.cmdMemberAdd()
return cmd, calls
}
Expand Down Expand Up @@ -457,7 +458,7 @@ func TestCmdOrg(t *testing.T) {
return nil
}

builder := newCmdOrgBuilder(fakeOrgUrmSVCsFn(svc, urmSVC), in(new(bytes.Buffer)), out(new(bytes.Buffer)))
builder := newCmdOrgBuilder(fakeOrgUrmSVCsFn(svc, urmSVC), in(new(bytes.Buffer)), out(ioutil.Discard))
cmd := builder.cmdMemberRemove()
return cmd, calls
}
Expand Down
24 changes: 13 additions & 11 deletions cmd/influx/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func setupF(cmd *cobra.Command, args []string) error {
return fmt.Errorf("failed to write token to path %q: %v", dPath, err)
}

fmt.Println(promptWithColor("Your token has been stored in "+dPath+".", colorCyan))
fmt.Println(string(promptWithColor("Your token has been stored in "+dPath+".", colorCyan)))

w := internal.NewTabWriter(os.Stdout)
w.WriteHeaders(
Expand Down Expand Up @@ -144,7 +144,7 @@ func interactive() (req *platform.OnboardingRequest, err error) {
Reader: os.Stdin,
}
req = new(platform.OnboardingRequest)
fmt.Println(promptWithColor(`Welcome to InfluxDB 2.0!`, colorYellow))
fmt.Println(string(promptWithColor(`Welcome to InfluxDB 2.0!`, colorYellow)))
if setupFlags.username != "" {
req.User = setupFlags.username
} else {
Expand Down Expand Up @@ -200,8 +200,9 @@ var (
keyReset = []byte{keyEscape, '[', '0', 'm'}
)

func promptWithColor(s string, color []byte) string {
return string(color) + s + string(keyReset)
func promptWithColor(s string, color []byte) []byte {
bb := append(color, []byte(s)...)
return append(bb, keyReset...)
}

func getConfirm(ui *input.UI, or *platform.OnboardingRequest) bool {
Expand All @@ -211,14 +212,14 @@ func getConfirm(ui *input.UI, or *platform.OnboardingRequest) bool {
if or.RetentionPeriod > 0 {
rp = fmt.Sprintf("%d hrs", or.RetentionPeriod)
}
fmt.Print(promptWithColor(fmt.Sprintf(`
ui.Writer.Write(promptWithColor(fmt.Sprintf(`
You have entered:
Username: %s
Organization: %s
Bucket: %s
Retention Period: %s
`, or.User, or.Org, or.Bucket, rp), colorCyan))
result, err := ui.Ask(prompt, &input.Options{
result, err := ui.Ask(string(prompt), &input.Options{
HideOrder: true,
})
if err != nil {
Expand Down Expand Up @@ -247,12 +248,13 @@ func getPassword(ui *input.UI, showNew bool) (password string) {
}
var err error
enterPasswd:
query := promptWithColor("Please type your"+newStr+" password", colorCyan)
query := string(promptWithColor("Please type your"+newStr+" password", colorCyan))
for {
password, err = ui.Ask(query, &input.Options{
Required: true,
HideOrder: true,
Hide: true,
Mask: false,
ValidateFunc: func(s string) error {
if len(s) < 8 {
return errPasswordIsTooShort
Expand All @@ -264,7 +266,7 @@ enterPasswd:
case input.ErrInterrupted:
os.Exit(1)
case errPasswordIsTooShort:
fmt.Println(promptWithColor("Password too short - minimum length is 8 characters!", colorRed))
ui.Writer.Write(promptWithColor("Password too short - minimum length is 8 characters!", colorRed))
goto enterPasswd
default:
if password = strings.TrimSpace(password); password == "" {
Expand All @@ -273,7 +275,7 @@ enterPasswd:
}
break
}
query = promptWithColor("Please type your"+newStr+" password again", colorCyan)
query = string(promptWithColor("Please type your"+newStr+" password again", colorCyan))
for {
_, err = ui.Ask(query, &input.Options{
Required: true,
Expand All @@ -292,7 +294,7 @@ enterPasswd:
case nil:
// Nothing.
default:
fmt.Println(promptWithColor("Passwords do not match!", colorRed))
ui.Writer.Write(promptWithColor("Passwords do not match!", colorRed))
goto enterPasswd
}
break
Expand All @@ -309,7 +311,7 @@ func getInput(ui *input.UI, prompt, defaultValue string) string {
option.Default = defaultValue
option.HideDefault = true
}
prompt = promptWithColor(prompt, colorCyan)
prompt = string(promptWithColor(prompt, colorCyan))
for {
line, err := ui.Ask(prompt, option)
switch err {
Expand Down
Loading

0 comments on commit a29ccea

Please sign in to comment.