Skip to content

Commit

Permalink
feat(cmd): apply env vars consistently across cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
dearyhud committed Dec 13, 2019
1 parent 8e8cfc7 commit 9aa343a
Show file tree
Hide file tree
Showing 9 changed files with 210 additions and 27 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
## v2.0.0-alpha.20 [UNRELEASED]

### Features

### Bug Fixes
1. [16225](https://github.com/influxdata/influxdb/pull/16225): Ensures env vars are applied consistently across cmd, and fixes issue where INFLUX_ env var prefix was not set globally.

### UI Improvements

## v2.0.0-alpha.21 [2019-12-13]

### Features
Expand Down
14 changes: 14 additions & 0 deletions cmd/influx/authorization.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/influxdata/influxdb/cmd/influx/internal"
"github.com/influxdata/influxdb/http"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// AuthorizationCreateFlags are command line args used when creating a authorization
Expand Down Expand Up @@ -77,6 +78,10 @@ func authCreateCmd() *cobra.Command {

cmd.Flags().StringVarP(&authCreateFlags.org, "org", "o", "", "The organization name (required)")
cmd.MarkFlagRequired("org")
viper.BindEnv("ORG")
if h := viper.GetString("ORG"); h != "" {
authCreateFlags.org = h
}

cmd.Flags().StringVarP(&authCreateFlags.user, "user", "u", "", "The user name")

Expand Down Expand Up @@ -297,7 +302,16 @@ func authFindCmd() *cobra.Command {
cmd.Flags().StringVarP(&authorizationFindFlags.user, "user", "u", "", "The user")
cmd.Flags().StringVarP(&authorizationFindFlags.userID, "user-id", "", "", "The user ID")
cmd.Flags().StringVarP(&authorizationFindFlags.org, "org", "o", "", "The org")
viper.BindEnv("ORG")
if h := viper.GetString("ORG"); h != "" {
authorizationFindFlags.org = h
}

cmd.Flags().StringVarP(&authorizationFindFlags.orgID, "org-id", "", "", "The org ID")
viper.BindEnv("ORG_ID")
if h := viper.GetString("ORG_ID"); h != "" {
authorizationFindFlags.orgID = h
}
cmd.Flags().StringVarP(&authorizationFindFlags.id, "id", "i", "", "The authorization ID")

return cmd
Expand Down
27 changes: 27 additions & 0 deletions cmd/influx/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/influxdata/influxdb/cmd/influx/internal"
"github.com/influxdata/influxdb/http"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// Bucket Command
Expand Down Expand Up @@ -43,7 +44,15 @@ func init() {
bucketCreateCmd.Flags().StringVarP(&bucketCreateFlags.name, "name", "n", "", "Name of bucket that will be created")
bucketCreateCmd.Flags().DurationVarP(&bucketCreateFlags.retention, "retention", "r", 0, "Duration in nanoseconds data will live in bucket")
bucketCreateCmd.Flags().StringVarP(&bucketCreateFlags.orgID, "org-id", "", "", "The ID of the organization that owns the bucket")
viper.BindEnv("ORG_ID")
if h := viper.GetString("ORG_ID"); h != "" {
bucketFindFlags.orgID = h
}
bucketCreateCmd.Flags().StringVarP(&bucketCreateFlags.org, "org", "o", "", "The org name")
viper.BindEnv("ORG")
if h := viper.GetString("ORG"); h != "" {
bucketFindFlags.org = h
}
bucketCreateCmd.MarkFlagRequired("name")

bucketCmd.AddCommand(bucketCreateCmd)
Expand Down Expand Up @@ -132,9 +141,22 @@ func init() {
}

bucketFindCmd.Flags().StringVarP(&bucketFindFlags.name, "name", "n", "", "The bucket name")
viper.BindEnv("BUCKET_NAME")
if h := viper.GetString("BUCKET_NAME"); h != "" {
bucketFindFlags.name = h
}
bucketFindCmd.Flags().StringVarP(&bucketFindFlags.id, "id", "i", "", "The bucket ID")
bucketFindCmd.Flags().StringVarP(&bucketFindFlags.orgID, "org-id", "", "", "The bucket organization ID")
viper.BindEnv("ORG_ID")
if h := viper.GetString("ORG_ID"); h != "" {
bucketFindFlags.orgID = h
}

bucketFindCmd.Flags().StringVarP(&bucketFindFlags.org, "org", "o", "", "The bucket organization name")
viper.BindEnv("ORG")
if h := viper.GetString("ORG"); h != "" {
bucketFindFlags.org = h
}
bucketFindCmd.Flags().BoolVar(&bucketFindFlags.headers, "headers", true, "To print the table headers; defaults true")

bucketCmd.AddCommand(bucketFindCmd)
Expand Down Expand Up @@ -219,6 +241,11 @@ func init() {

bucketUpdateCmd.Flags().StringVarP(&bucketUpdateFlags.id, "id", "i", "", "The bucket ID (required)")
bucketUpdateCmd.Flags().StringVarP(&bucketUpdateFlags.name, "name", "n", "", "New bucket name")
viper.BindEnv("BUCKET_NAME")
if h := viper.GetString("BUCKET_NAME"); h != "" {
bucketFindFlags.name = h
}

bucketUpdateCmd.Flags().DurationVarP(&bucketUpdateFlags.retention, "retention", "r", 0, "New duration data will live in bucket")
bucketUpdateCmd.MarkFlagRequired("id")

Expand Down
5 changes: 3 additions & 2 deletions cmd/influx/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ func influxCmd() *cobra.Command {
cmd.Usage()
},
}

viper.SetEnvPrefix("INFLUX")

cmd.AddCommand(
authCmd(),
bucketCmd,
Expand All @@ -99,8 +102,6 @@ func influxCmd() *cobra.Command {
writeCmd,
)

viper.SetEnvPrefix("INFLUX")

cmd.PersistentFlags().StringVarP(&flags.token, "token", "t", "", "API token to be used throughout client calls")
viper.BindEnv("TOKEN")
if h := viper.GetString("TOKEN"); h != "" {
Expand Down
49 changes: 48 additions & 1 deletion cmd/influx/organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/influxdata/influxdb/cmd/influx/internal"
"github.com/influxdata/influxdb/http"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func organizationCmd() *cobra.Command {
Expand Down Expand Up @@ -113,7 +114,15 @@ func orgFindCmd() *cobra.Command {
}

cmd.Flags().StringVarP(&organizationFindFlags.name, "name", "n", "", "The organization name")
viper.BindEnv("ORG")
if h := viper.GetString("ORG"); h != "" {
organizationFindFlags.name = h
}
cmd.Flags().StringVarP(&organizationFindFlags.id, "id", "i", "", "The organization ID")
viper.BindEnv("ORG_ID")
if h := viper.GetString("ORG_ID"); h != "" {
organizationFindFlags.id = h
}

return cmd
}
Expand Down Expand Up @@ -174,8 +183,17 @@ func orgUpdateCmd() *cobra.Command {
}

cmd.Flags().StringVarP(&organizationUpdateFlags.id, "id", "i", "", "The organization ID (required)")
cmd.Flags().StringVarP(&organizationUpdateFlags.name, "name", "n", "", "The organization name")
cmd.MarkFlagRequired("id")
viper.BindEnv("ORG_ID")
if h := viper.GetString("ORG_ID"); h != "" {
organizationUpdateFlags.id = h
}

cmd.Flags().StringVarP(&organizationUpdateFlags.name, "name", "n", "", "The organization name")
viper.BindEnv("ORG")
if h := viper.GetString("ORG"); h != "" {
organizationUpdateFlags.name = h
}

return cmd
}
Expand Down Expand Up @@ -268,6 +286,10 @@ func orgDeleteCmd() *cobra.Command {

cmd.Flags().StringVarP(&organizationDeleteFlags.id, "id", "i", "", "The organization ID (required)")
cmd.MarkFlagRequired("id")
viper.BindEnv("ORG_ID")
if h := viper.GetString("ORG_ID"); h != "" {
organizationUpdateFlags.id = h
}

return cmd
}
Expand Down Expand Up @@ -340,7 +362,15 @@ func orgMembersListCmd() *cobra.Command {
}

cmd.Flags().StringVarP(&organizationMembersListFlags.id, "id", "i", "", "The organization ID")
viper.BindEnv("ORG_ID")
if h := viper.GetString("ORG_ID"); h != "" {
organizationMembersListFlags.id = h
}
cmd.Flags().StringVarP(&organizationMembersListFlags.name, "name", "n", "", "The organization name")
viper.BindEnv("ORG")
if h := viper.GetString("ORG"); h != "" {
organizationMembersListFlags.name = h
}

return cmd
}
Expand Down Expand Up @@ -411,7 +441,16 @@ func orgMembersAddCmd() *cobra.Command {
}

cmd.Flags().StringVarP(&organizationMembersAddFlags.id, "id", "i", "", "The organization ID")
viper.BindEnv("ORG_ID")
if h := viper.GetString("ORG_ID"); h != "" {
organizationMembersAddFlags.id = h
}
cmd.Flags().StringVarP(&organizationMembersAddFlags.name, "name", "n", "", "The organization name")
viper.BindEnv("ORG")
if h := viper.GetString("ORG"); h != "" {
organizationMembersAddFlags.name = h
}

cmd.Flags().StringVarP(&organizationMembersAddFlags.memberID, "member", "o", "", "The member ID")
cmd.MarkFlagRequired("member")

Expand Down Expand Up @@ -478,7 +517,15 @@ func orgMembersRemoveCmd() *cobra.Command {
}

cmd.Flags().StringVarP(&organizationMembersRemoveFlags.id, "id", "i", "", "The organization ID")
viper.BindEnv("ORG_ID")
if h := viper.GetString("ORG_ID"); h != "" {
organizationMembersAddFlags.id = h
}
cmd.Flags().StringVarP(&organizationMembersRemoveFlags.name, "name", "n", "", "The organization name")
viper.BindEnv("ORG")
if h := viper.GetString("ORG"); h != "" {
organizationMembersRemoveFlags.name = h
}
cmd.Flags().StringVarP(&organizationMembersRemoveFlags.memberID, "member", "o", "", "The member ID")
cmd.MarkFlagRequired("member")

Expand Down
19 changes: 17 additions & 2 deletions cmd/influx/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/influxdata/influxdb/pkger"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
"github.com/spf13/viper"
input "github.com/tcnksm/go-input"
"gopkg.in/yaml.v3"
)
Expand Down Expand Up @@ -93,10 +94,16 @@ func (b *cmdPkgBuilder) cmdPkgApply() *cobra.Command {
cmd.MarkFlagFilename("file", "yaml", "yml", "json")
cmd.Flags().BoolVarP(&b.quiet, "quiet", "q", false, "disable output printing")
cmd.Flags().StringVar(&b.applyOpts.force, "force", "", `TTY input, if package will have destructive changes, proceed if set "true".`)

cmd.Flags().StringVarP(&b.orgID, "org-id", "", "", "The ID of the organization that owns the bucket")
viper.BindEnv("ORG_ID")
if h := viper.GetString("ORG_ID"); h != "" {
b.orgID = h
}
cmd.Flags().StringVarP(&b.org, "org", "o", "", "The name of the organization that owns the bucket")

viper.BindEnv("ORG")
if h := viper.GetString("ORG"); h != "" {
b.org = h
}
cmd.Flags().BoolVarP(&b.hasColor, "color", "c", true, "Enable color in output, defaults true")
cmd.Flags().BoolVar(&b.hasTableBorders, "table-borders", true, "Enable table borders, defaults true")

Expand Down Expand Up @@ -298,7 +305,15 @@ func (b *cmdPkgBuilder) cmdPkgExportAll() *cobra.Command {

cmd.Flags().StringVarP(&b.file, "file", "f", "", "output file for created pkg; defaults to std out if no file provided; the extension of provided file (.yml/.json) will dictate encoding")
cmd.Flags().StringVarP(&b.orgID, "org-id", "", "", "organization id")
viper.BindEnv("ORG_ID")
if h := viper.GetString("ORG_ID"); h != "" {
b.orgID = h
}
cmd.Flags().StringVarP(&b.org, "org", "o", "", "The name of the organization that owns the bucket")
viper.BindEnv("ORG")
if h := viper.GetString("ORG"); h != "" {
b.org = h
}
cmd.Flags().StringVarP(&b.meta.Name, "name", "n", "", "name for new pkg")
cmd.Flags().StringVarP(&b.meta.Description, "description", "d", "", "description for new pkg")
cmd.Flags().StringVarP(&b.meta.Version, "version", "v", "", "version for new pkg")
Expand Down
86 changes: 64 additions & 22 deletions cmd/influx/pkg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,42 @@ func Test_Pkg(t *testing.T) {
Version: "new version",
},
},
{
pkgFileArgs: pkgFileArgs{
name: "yaml out",
encoding: pkger.EncodingYAML,
filename: "pkg_0.yml",
flags: []flagArg{
{name: "name", val: "new name"},
{name: "description", val: "new desc"},
{name: "version", val: "new version"},
},
envVars: []struct{ key, val string }{{key: "INFLUX_ORG", val: "influxdata"}},
},
expectedMeta: pkger.Metadata{
Name: "new name",
Description: "new desc",
Version: "new version",
},
},
{
pkgFileArgs: pkgFileArgs{
name: "yaml out",
encoding: pkger.EncodingYAML,
filename: "pkg_0.yml",
flags: []flagArg{
{name: "name", val: "new name"},
{name: "description", val: "new desc"},
{name: "version", val: "new version"},
},
envVars: []struct{ key, val string }{{key: "INFLUX_ORG_ID", val: expectedOrgID.String()}},
},
expectedMeta: pkger.Metadata{
Name: "new name",
Description: "new desc",
Version: "new version",
},
},
}

cmdFn := func() *cobra.Command {
Expand Down Expand Up @@ -294,28 +330,6 @@ func Test_Pkg(t *testing.T) {
Version: "new version",
},
},
{
pkgFileArgs: pkgFileArgs{
name: "mixed",
encoding: pkger.EncodingYAML,
filename: "pkg_0.yml",
flags: []flagArg{
{name: "name", val: "new name"},
{name: "description", val: "new desc"},
{name: "version", val: "new version"},
},
},
bucketIDs: []influxdb.ID{1, 2},
dashIDs: []influxdb.ID{3, 4},
labelIDs: []influxdb.ID{5, 6},
varIDs: []influxdb.ID{7, 8},
telegrafIDs: []influxdb.ID{9, 10},
expectedMeta: pkger.Metadata{
Name: "new name",
Description: "new desc",
Version: "new version",
},
},
}

cmdFn := func() *cobra.Command {
Expand Down Expand Up @@ -418,15 +432,43 @@ type pkgFileArgs struct {
filename string
encoding pkger.Encoding
flags []flagArg
envVars []struct {
key, val string
}
}

func testPkgWrites(t *testing.T, newCmdFn func() *cobra.Command, args pkgFileArgs, assertFn func(t *testing.T, pkg *pkger.Pkg)) {
t.Helper()
wrappedCmdFn := func() *cobra.Command {
cmd := newCmdFn()
cmd.SetArgs([]string{}) // clears mess from test runner coming into cobra cli via stdin
return cmd
}

// we'll memoize the current env vars if defined in args.envVars, then set the env vars defined in each test
var initialEnvVars []struct{ key, val string }
for _, envVar := range args.envVars {
if k := os.Getenv(envVar.key); k != "" {
initialEnvVars = append(initialEnvVars, struct{ key, val string }{
key: envVar.key,
val: os.Getenv(envVar.key),
})
}

require.NoError(t, os.Setenv(envVar.key, envVar.val))
}

defer func() {
// unset the env vars set by the test
for _, envVar := range args.envVars {
require.NoError(t, os.Unsetenv(envVar.key))
}

// set the test env vars back to the initial state
for _, envVar := range initialEnvVars {
require.NoError(t, os.Setenv(envVar.key, envVar.val))
}
}()
t.Run(path.Join(args.name, "file"), testPkgWritesFile(wrappedCmdFn, args, assertFn))
t.Run(path.Join(args.name, "buffer"), testPkgWritesToBuffer(wrappedCmdFn, args, assertFn))
}
Expand Down
Loading

0 comments on commit 9aa343a

Please sign in to comment.