-
Notifications
You must be signed in to change notification settings - Fork 3.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(cmd): apply env vars consistently across cmd #16225
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this feels off to me, I would suggest flags take precendence over env vars personally |
||
} | ||
|
||
cmd.Flags().StringVarP(&authCreateFlags.user, "user", "u", "", "The user name") | ||
|
||
|
@@ -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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -25,9 +26,8 @@ func bucketF(cmd *cobra.Command, args []string) { | |
|
||
// BucketCreateFlags define the Create Command | ||
type BucketCreateFlags struct { | ||
name string | ||
orgID string | ||
org string | ||
name string | ||
organization | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd recommend not embedding the organization type, although, all this will likely get torched when a testable design comes in 🤔 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. totally agree, I wanted this to be easy to revert once we converge on a pattern we're implementing across the board. |
||
retention time.Duration | ||
} | ||
|
||
|
@@ -42,9 +42,8 @@ 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") | ||
bucketCreateCmd.Flags().StringVarP(&bucketCreateFlags.org, "org", "o", "", "The org name") | ||
bucketCreateCmd.MarkFlagRequired("name") | ||
bucketCreateFlags.organization.register(bucketCreateCmd) | ||
|
||
bucketCmd.AddCommand(bucketCreateCmd) | ||
} | ||
|
@@ -65,10 +64,8 @@ func newBucketService(f Flags) (platform.BucketService, error) { | |
} | ||
|
||
func bucketCreateF(cmd *cobra.Command, args []string) error { | ||
if bucketCreateFlags.orgID == "" && bucketCreateFlags.org == "" { | ||
return fmt.Errorf("must specify org-id, or org name") | ||
} else if bucketCreateFlags.orgID != "" && bucketCreateFlags.org != "" { | ||
return fmt.Errorf("must specify org-id, or org name not both") | ||
if err := bucketCreateFlags.organization.validOrgFlags(); err != nil { | ||
return err | ||
} | ||
|
||
s, err := newBucketService(flags) | ||
|
@@ -86,7 +83,7 @@ func bucketCreateF(cmd *cobra.Command, args []string) error { | |
return nil | ||
} | ||
|
||
b.OrgID, err = getOrgID(orgSvc, bucketCreateFlags.orgID, bucketCreateFlags.org) | ||
b.OrgID, err = bucketCreateFlags.organization.getID(orgSvc) | ||
if err != nil { | ||
return err | ||
} | ||
|
@@ -117,9 +114,8 @@ func bucketCreateF(cmd *cobra.Command, args []string) error { | |
type BucketFindFlags struct { | ||
name string | ||
id string | ||
org string | ||
orgID string | ||
headers bool | ||
organization | ||
} | ||
|
||
var bucketFindFlags BucketFindFlags | ||
|
@@ -132,10 +128,13 @@ 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") | ||
bucketFindCmd.Flags().StringVarP(&bucketFindFlags.org, "org", "o", "", "The bucket organization name") | ||
bucketFindCmd.Flags().BoolVar(&bucketFindFlags.headers, "headers", true, "To print the table headers; defaults true") | ||
bucketFindFlags.organization.register(bucketFindCmd) | ||
|
||
bucketCmd.AddCommand(bucketFindCmd) | ||
} | ||
|
@@ -159,20 +158,20 @@ func bucketFindF(cmd *cobra.Command, args []string) error { | |
filter.ID = id | ||
} | ||
|
||
if bucketFindFlags.orgID != "" && bucketFindFlags.org != "" { | ||
return fmt.Errorf("must specify at exactly one of org and org-id") | ||
if err := bucketFindFlags.organization.validOrgFlags(); err != nil { | ||
return err | ||
} | ||
|
||
if bucketFindFlags.orgID != "" { | ||
orgID, err := platform.IDFromString(bucketFindFlags.orgID) | ||
if bucketFindFlags.organization.id != "" { | ||
orgID, err := platform.IDFromString(bucketFindFlags.organization.id) | ||
if err != nil { | ||
return fmt.Errorf("failed to decode org id %q: %v", bucketFindFlags.orgID, err) | ||
return fmt.Errorf("failed to decode org id %q: %v", bucketFindFlags.organization.id, err) | ||
} | ||
filter.OrganizationID = orgID | ||
} | ||
|
||
if bucketFindFlags.org != "" { | ||
filter.Org = &bucketFindFlags.org | ||
if bucketFindFlags.organization.name != "" { | ||
filter.Org = &bucketFindFlags.organization.name | ||
} | ||
|
||
buckets, _, err := s.FindBuckets(context.Background(), filter) | ||
|
@@ -219,6 +218,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") | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -83,6 +83,9 @@ func influxCmd() *cobra.Command { | |
cmd.Usage() | ||
}, | ||
} | ||
|
||
viper.SetEnvPrefix("INFLUX") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. caught this when testing the cmd after these changes - we should've been calling this before adding any of the commands below. |
||
|
||
cmd.AddCommand( | ||
authCmd(), | ||
bucketCmd, | ||
|
@@ -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 != "" { | ||
|
@@ -230,22 +231,47 @@ func newLocalKVService() (*kv.Service, error) { | |
return kv.NewService(zap.NewNop(), store), nil | ||
} | ||
|
||
func getOrgID(orgSVC influxdb.OrganizationService, id string, name string) (influxdb.ID, error) { | ||
if id != "" { | ||
influxOrgID, err := influxdb.IDFromString(id) | ||
type organization struct { | ||
id, name string | ||
} | ||
|
||
func (org *organization) register(cmd *cobra.Command) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this looks much improved 👍 |
||
cmd.Flags().StringVarP(&org.id, "org-id", "", "", "The ID of the organization that owns the bucket") | ||
viper.BindEnv("ORG_ID") | ||
if h := viper.GetString("ORG_ID"); h != "" { | ||
org.id = h | ||
} | ||
cmd.Flags().StringVarP(&org.name, "org", "o", "", "The name of the organization that owns the bucket") | ||
viper.BindEnv("ORG") | ||
if h := viper.GetString("ORG"); h != "" { | ||
org.name = h | ||
} | ||
} | ||
|
||
func (org *organization) getID(orgSVC influxdb.OrganizationService) (influxdb.ID, error) { | ||
if org.id != "" { | ||
influxOrgID, err := influxdb.IDFromString(org.id) | ||
if err != nil { | ||
return 0, fmt.Errorf("invalid org ID provided: %s", err.Error()) | ||
} | ||
return *influxOrgID, nil | ||
} else if name != "" { | ||
} else if org.name != "" { | ||
org, err := orgSVC.FindOrganization(context.Background(), influxdb.OrganizationFilter{ | ||
Name: &name, | ||
Name: &org.name, | ||
}) | ||
if err != nil { | ||
return 0, fmt.Errorf("%v", err) | ||
} | ||
return org.ID, nil | ||
} | ||
return 0, fmt.Errorf("failed to locate an organization id") | ||
} | ||
|
||
return 0, fmt.Errorf("") | ||
func (org *organization) validOrgFlags() error { | ||
if org.id == "" && org.name == "" { | ||
return fmt.Errorf("must specify org-id, or org name") | ||
} else if org.id != "" && org.name != "" { | ||
return fmt.Errorf("must specify org-id, or org name not both") | ||
} | ||
return nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you make this into a reusable type? somethign like
then just make that a dep of the builders or inline it wherever, a step towards reusable patterns for influx cli tool 🤷♂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sounds good.