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

🐛 Load provider flags from environment variables #4847

Merged
merged 1 commit into from
Nov 13, 2024

Conversation

afiune
Copy link
Contributor

@afiune afiune commented Nov 13, 2024

This change allows cnquery to load all provider flags via environment variables. For instance, if a provider exposes a flag named foo, the user can export the environment variable MONDOO_FOO to provide a value.

The trick here was to switch loading the flag values from cobra to viper, since we already ran viper.BindPFlag() inside the command PreRun, we can now use viper.Get() directly.

Copy link
Contributor

github-actions bot commented Nov 13, 2024

Test Results

3 145 tests  ±0   3 144 ✅ ±0   1m 22s ⏱️ -17s
  371 suites ±0       1 💤 ±0 
   28 files   ±0       0 ❌ ±0 

Results for commit dd3bcd5. ± Comparison against base commit b8014a3.

♻️ This comment has been updated with latest results.

This change allows `cnquery` to load all provider flags via environment
variables. For instance, if a provider exposes a flag named `foo`, the
user can export the environment variable `MONDOO_FOO` to provide a
value.

The trick here was to switch loading the flag values from `cobra` to
`viper`, since we already ran `viper.BindPFlag()` inside the command
`PreRun`, we can now use `viper.Get()` directly.

Signed-off-by: Salim Afiune Maya <[email protected]>
@afiune afiune force-pushed the afiune/load-providers-flag-via-viper-config branch from 78f0767 to dd3bcd5 Compare November 13, 2024 00:48
switch flag.Type {
case plugin.FlagType_Bool:
v, err := cmd.Flags().GetBool(flag.Long)
Copy link
Member

Choose a reason for hiding this comment

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

Do we need a bind equivalent somewhere? Like in https://github.com/mondoohq/cnspec/blob/main/apps/cnspec/cmd/scan.go#L105

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We already have it for all flags inside a provider. Though you're pointing to cnspec code so I'm assuming you want to know if this will work there. I can test, but this is working for cnquery.

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
Member

Choose a reason for hiding this comment

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

Perfecto. Then lets do it.

@afiune afiune merged commit 0b4c641 into main Nov 13, 2024
16 checks passed
@afiune afiune deleted the afiune/load-providers-flag-via-viper-config branch November 13, 2024 15:13
@github-actions github-actions bot locked and limited conversation to collaborators Nov 13, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants