-
-
Notifications
You must be signed in to change notification settings - Fork 134
feat: add disable changelog flag and env var #8969
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1b329bd
feat: add disable changelog flag and env var
omer-cengel c5f4937
Merge branch 'main' into feat/disable-changelog
olblak c2dc5c9
test: set disable changelog env var in flag registration test
omer-cengel c4e5fcb
chore: use env var constant in disable changelog flag help
omer-cengel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "os" | ||
| "strconv" | ||
| "strings" | ||
|
|
||
| "github.com/sirupsen/logrus" | ||
| ) | ||
|
|
||
| const DisableChangelogEnvVar = "UPDATECLI_DISABLE_CHANGELOG" | ||
|
|
||
| // getEnvBoolOrDefault reads a boolean environment variable. | ||
| // It returns defaultValue when the variable is unset or invalid. | ||
| func getEnvBoolOrDefault(envVar string, defaultValue bool) bool { | ||
| value, ok := os.LookupEnv(envVar) | ||
| if !ok { | ||
| return defaultValue | ||
| } | ||
|
|
||
| parsed, err := strconv.ParseBool(strings.TrimSpace(value)) | ||
| if err != nil { | ||
| logrus.Debugf( | ||
| "invalid boolean value for environment variable %q: %q, defaulting to %t", | ||
| envVar, | ||
| value, | ||
| defaultValue, | ||
| ) | ||
| return defaultValue | ||
| } | ||
|
|
||
| return parsed | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestGetEnvBoolOrDefault(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| envVar string | ||
| envValue string | ||
| setEnv bool | ||
| defaultValue bool | ||
| expected bool | ||
| }{ | ||
| { | ||
| name: "not_set_returns_default_true", | ||
| envVar: "UNDEFINED_VAR_1", | ||
| defaultValue: true, | ||
| expected: true, | ||
| }, | ||
| { | ||
| name: "not_set_returns_default_false", | ||
| envVar: "UNDEFINED_VAR_2", | ||
| defaultValue: false, | ||
| expected: false, | ||
| }, | ||
| { | ||
| name: "set_to_true", | ||
| envVar: "TEST_VAR_TRUE", | ||
| envValue: "true", | ||
| setEnv: true, | ||
| defaultValue: false, | ||
| expected: true, | ||
| }, | ||
| { | ||
| name: "set_to_false", | ||
| envVar: "TEST_VAR_FALSE", | ||
| envValue: "false", | ||
| setEnv: true, | ||
| defaultValue: true, | ||
| expected: false, | ||
| }, | ||
| { | ||
| name: "set_to_1", | ||
| envVar: "TEST_VAR_1", | ||
| envValue: "1", | ||
| setEnv: true, | ||
| defaultValue: false, | ||
| expected: true, | ||
| }, | ||
| { | ||
| name: "set_to_0", | ||
| envVar: "TEST_VAR_0", | ||
| envValue: "0", | ||
| setEnv: true, | ||
| defaultValue: true, | ||
| expected: false, | ||
| }, | ||
| { | ||
| name: "whitespace_trimmed", | ||
| envVar: "TEST_VAR_SPACE", | ||
| envValue: " true ", | ||
| setEnv: true, | ||
| defaultValue: false, | ||
| expected: true, | ||
| }, | ||
| { | ||
| name: "invalid_returns_default_true", | ||
| envVar: "TEST_VAR_INVALID_1", | ||
| envValue: "invalid", | ||
| setEnv: true, | ||
| defaultValue: true, | ||
| expected: true, | ||
| }, | ||
| { | ||
| name: "invalid_returns_default_false", | ||
| envVar: "TEST_VAR_INVALID_2", | ||
| envValue: "maybe", | ||
| setEnv: true, | ||
| defaultValue: false, | ||
| expected: false, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| if tt.setEnv { | ||
| t.Setenv(tt.envVar, tt.envValue) | ||
| } | ||
|
|
||
| result := getEnvBoolOrDefault(tt.envVar, tt.defaultValue) | ||
|
|
||
| if result != tt.expected { | ||
| t.Errorf("got %v, expected %v", result, tt.expected) | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package cmd | ||
|
|
||
| import "github.com/spf13/cobra" | ||
|
|
||
| // addDisableChangelogFlag registers the shared --disable-changelog flag on the | ||
| // provided command, using the value from UPDATECLI_DISABLE_CHANGELOG as the | ||
| // default when the flag is not explicitly passed. | ||
| func addDisableChangelogFlag(cmd *cobra.Command, dest *bool) { | ||
| cmd.Flags().BoolVar( | ||
| dest, | ||
| "disable-changelog", | ||
| getEnvBoolOrDefault(DisableChangelogEnvVar, false), | ||
| "Disable changelog retrieval to avoid unnecessary requests (env: "+DisableChangelogEnvVar+")", | ||
| ) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| func TestAddDisableChangelogFlagRegistration(t *testing.T) { | ||
| t.Setenv(DisableChangelogEnvVar, "false") | ||
|
|
||
| cmd := &cobra.Command{ | ||
| Use: "test", | ||
| } | ||
|
|
||
| var disableChangelog bool | ||
| addDisableChangelogFlag(cmd, &disableChangelog) | ||
|
|
||
| // Check that flag exists | ||
| flag := cmd.Flags().Lookup("disable-changelog") | ||
| if flag == nil { | ||
| t.Fatal("flag not registered") | ||
| } | ||
|
|
||
| // Check flag has help text | ||
| if flag.Usage == "" { | ||
| t.Error("flag help text is empty") | ||
| } | ||
|
|
||
| // Check that env var name appears in help text | ||
| if !strings.Contains(flag.Usage, DisableChangelogEnvVar) { | ||
| t.Errorf( | ||
| "flag help text does not mention env var %q: help text is %q", | ||
| DisableChangelogEnvVar, | ||
| flag.Usage, | ||
| ) | ||
| } | ||
|
|
||
| // Check default value is "false" when env var not set | ||
| if flag.DefValue != "false" { | ||
| t.Errorf( | ||
| "flag default value when no env var: got %q, expected %q", | ||
| flag.DefValue, | ||
| "false", | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| func TestAddDisableChangelogFlagUsesEnvDefault(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| envValue string | ||
| expectedDef string | ||
| }{ | ||
| { | ||
| name: "env_var_true", | ||
| envValue: "true", | ||
| expectedDef: "true", | ||
| }, | ||
| { | ||
| name: "env_var_false", | ||
| envValue: "false", | ||
| expectedDef: "false", | ||
| }, | ||
| { | ||
| name: "env_var_1", | ||
| envValue: "1", | ||
| expectedDef: "true", | ||
| }, | ||
| { | ||
| name: "env_var_0", | ||
| envValue: "0", | ||
| expectedDef: "false", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| t.Setenv(DisableChangelogEnvVar, tt.envValue) | ||
|
|
||
| cmd := &cobra.Command{ | ||
| Use: "test", | ||
| } | ||
|
|
||
| var disableChangelog bool | ||
| addDisableChangelogFlag(cmd, &disableChangelog) | ||
|
|
||
| flag := cmd.Flags().Lookup("disable-changelog") | ||
| if flag == nil { | ||
| t.Fatal("flag not registered") | ||
| } | ||
|
|
||
| if flag.DefValue != tt.expectedDef { | ||
| t.Errorf( | ||
| "flag default value: got %q, expected %q", | ||
| flag.DefValue, | ||
| tt.expectedDef, | ||
| ) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestDisableChangelogFlagOverridesEnv(t *testing.T) { | ||
| t.Setenv(DisableChangelogEnvVar, "true") | ||
|
|
||
| cmd := &cobra.Command{ | ||
| Use: "test", | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| return nil | ||
| }, | ||
| } | ||
|
|
||
| var disableChangelog bool | ||
| addDisableChangelogFlag(cmd, &disableChangelog) | ||
|
|
||
| cmd.SetArgs([]string{"--disable-changelog=false"}) | ||
|
|
||
| err := cmd.Execute() | ||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
|
|
||
| if disableChangelog { | ||
| t.Error("expected flag to override env var") | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.