Skip to content
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

Merged
merged 1 commit into from
Dec 17, 2019

Conversation

dearyhud
Copy link
Contributor

@dearyhud dearyhud commented Dec 13, 2019

Closes #16214

Consistently applies env vars to cli commands using flags that could be interchanged with env vars (ie. --org-id INFLUX_ORG_ID, --bucket-name INFLUX_BUCKET_NAME, etc). See #16048 for a more detailed list of env vars.

Also fixes a small bug where we were not setting globally the INFLUX prefix before any cobra commands were init.

@dearyhud dearyhud requested a review from a team December 13, 2019 21:51
@dearyhud dearyhud force-pushed the 16214/consistently-apply-env-vars branch 3 times, most recently from 03b265b to 6b79682 Compare December 13, 2019 21:59
@@ -83,6 +83,9 @@ func influxCmd() *cobra.Command {
cmd.Usage()
},
}

viper.SetEnvPrefix("INFLUX")
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

@dearyhud dearyhud force-pushed the 16214/consistently-apply-env-vars branch from 6b79682 to 9aa343a Compare December 13, 2019 22:02
@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The 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

@@ -77,6 +78,10 @@ func authCreateCmd() *cobra.Command {

cmd.Flags().StringVarP(&authCreateFlags.org, "org", "o", "", "The organization name (required)")
cmd.MarkFlagRequired("org")
Copy link
Contributor

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

// rename I don't know what to call it 😬 
type orgStuffs struct {
    orgID string
    organization string
}

func (o *orgStuffs) register(cmd *cobra.Cmd) {

       viper.BindEnv("ORG")
       cmd.Flags().StringVarp(&o.organization,"org", "o", "", "The organization name (required)")
      ... viper stuffs
   
   ... wash rise repeat for org-id in here
}

then just make that a dep of the builders or inline it wherever, a step towards reusable patterns for influx cli tool 🤷‍♂

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good.

@dearyhud dearyhud force-pushed the 16214/consistently-apply-env-vars branch 4 times, most recently from 8bae028 to 116f90b Compare December 17, 2019 16:47
@dearyhud dearyhud requested a review from jsteenb2 December 17, 2019 16:48
@dearyhud dearyhud force-pushed the 16214/consistently-apply-env-vars branch from 116f90b to 6dc9f14 Compare December 17, 2019 16:58
orgID string
org string
name string
organization
Copy link
Contributor

Choose a reason for hiding this comment

The 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 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

return fmt.Errorf("must specify org-id, or org name")
} else if bucketCreateFlags.orgID != "" && bucketCreateFlags.org != "" {
} else if bucketCreateFlags.organization.id != "" && bucketCreateFlags.organization.name != "" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can make this a method on the organization type itself and call it here 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

@jsteenb2 jsteenb2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one small nit on embedding but that's should be simple change

id, name string
}

func (org *organization) register(cmd *cobra.Command) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks much improved 👍

orgID string
org string
quiet bool
organization
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets not embed this here, namespace it by a field name, something as simple as the following would be fine.

org organization

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fair enough!

@dearyhud dearyhud force-pushed the 16214/consistently-apply-env-vars branch from 6dc9f14 to a38d2fd Compare December 17, 2019 17:53
@dearyhud dearyhud force-pushed the 16214/consistently-apply-env-vars branch from a38d2fd to e959a63 Compare December 17, 2019 19:03
@dearyhud dearyhud merged commit d5f8a5f into master Dec 17, 2019
@dearyhud dearyhud deleted the 16214/consistently-apply-env-vars branch December 17, 2019 19:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

consistently apply env vars in CLI
2 participants