feat: add disable changelog flag and env var (#8969) · updatecli/updatecli@1ac1f5e · GitHub
Skip to content

Commit 1ac1f5e

Browse files
omer-cengelolblak
andauthored
feat: add disable changelog flag and env var (#8969)
* feat: add disable changelog flag and env var * test: set disable changelog env var in flag registration test * chore: use env var constant in disable changelog flag help --------- Co-authored-by: Olivier Vernin <olivier@vernin.me>
1 parent 1ee91e9 commit 1ac1f5e

14 files changed

Lines changed: 375 additions & 23 deletions

‎cmd/apply.go‎

Lines changed: 3 additions & 0 deletions

‎cmd/compose_apply.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ var (
5353
e.Options.Pipeline.Target.DryRun = false
5454
e.Options.Pipeline.Target.CleanGitBranches = composeApplyCleanGitBranches
5555
e.Options.Pipeline.Target.ExistingOnly = composeApplyExistingOnly
56+
e.Options.Pipeline.DisableChangelog = disableChangelog
5657

5758
err = run("compose/apply")
5859
if err != nil {
@@ -77,5 +78,7 @@ func init() {
7778
composeApplyCmd.Flags().StringArrayVar(&composeApplyOnlyPolicyIDs, "only-policy-ids", []string{}, "Filter policies to apply by their policy IDs, accepted as a comma separated list")
7879
composeApplyCmd.Flags().StringArrayVar(&composeApplyIgnoredPolicyIDs, "ignored-policy-ids", []string{}, "Filter policies to ignore by their policy IDs, accepted as a comma separated list")
7980

81+
addDisableChangelogFlag(composeApplyCmd, &disableChangelog)
82+
8083
composeCmd.AddCommand(composeApplyCmd)
8184
}

‎cmd/compose_diff.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ var (
4646
e.Options.Pipeline.Target.Push = false
4747
e.Options.Pipeline.Target.Clean = composeCmdClean
4848
e.Options.Pipeline.Target.DryRun = true
49+
e.Options.Pipeline.DisableChangelog = disableChangelog
4950

5051
err = run("compose/diff")
5152
if err != nil {
@@ -65,5 +66,8 @@ func init() {
6566
composeDiffCmd.Flags().StringArrayVar(&labels, "labels", []string{}, "Filter pipelines to apply by their labels, accepted as a comma separated list (key:value)")
6667
composeDiffCmd.Flags().StringArrayVar(&composeDiffOnlyPolicyIDs, "only-policy-ids", []string{}, "Filter policies to apply by their policy IDs, accepted as a comma separated list")
6768
composeDiffCmd.Flags().StringArrayVar(&composeDiffIgnoredPolicyIDs, "ignored-policy-ids", []string{}, "Filter policies to ignore by their policy IDs, accepted as a comma separated list")
69+
70+
addDisableChangelogFlag(composeDiffCmd, &disableChangelog)
71+
6872
composeCmd.AddCommand(composeDiffCmd)
6973
}

‎cmd/diff.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ var (
3535
e.Options.Pipeline.Target.Push = false
3636
e.Options.Pipeline.Target.Clean = diffClean
3737
e.Options.Pipeline.Target.DryRun = true
38+
e.Options.Pipeline.DisableChangelog = disableChangelog
3839

3940
logrus.Warningln("Deprecated command, please instead use `updatecli pipeline diff`")
4041

@@ -57,4 +58,6 @@ func init() {
5758
diffCmd.Flags().BoolVar(&disableTLS, "disable-tls", false, "Disable TLS verification like '--disable-tls=true'")
5859
diffCmd.Flags().StringArrayVar(&pipelineIds, "pipeline-ids", []string{}, "Filter pipelines to apply by their pipeline IDs, accepted a comma separated list")
5960
diffCmd.Flags().StringArrayVar(&labels, "labels", []string{}, "Filter pipelines to apply by their labels, accepted as a comma separated list (key:value)")
61+
62+
addDisableChangelogFlag(diffCmd, &disableChangelog)
6063
}

‎cmd/env.go‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package cmd
2+
3+
import (
4+
"os"
5+
"strconv"
6+
"strings"
7+
8+
"github.com/sirupsen/logrus"
9+
)
10+
11+
const DisableChangelogEnvVar = "UPDATECLI_DISABLE_CHANGELOG"
12+
13+
// getEnvBoolOrDefault reads a boolean environment variable.
14+
// It returns defaultValue when the variable is unset or invalid.
15+
func getEnvBoolOrDefault(envVar string, defaultValue bool) bool {
16+
value, ok := os.LookupEnv(envVar)
17+
if !ok {
18+
return defaultValue
19+
}
20+
21+
parsed, err := strconv.ParseBool(strings.TrimSpace(value))
22+
if err != nil {
23+
logrus.Debugf(
24+
"invalid boolean value for environment variable %q: %q, defaulting to %t",
25+
envVar,
26+
value,
27+
defaultValue,
28+
)
29+
return defaultValue
30+
}
31+
32+
return parsed
33+
}

‎cmd/env_test.go‎

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package cmd
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestGetEnvBoolOrDefault(t *testing.T) {
8+
tests := []struct {
9+
name string
10+
envVar string
11+
envValue string
12+
setEnv bool
13+
defaultValue bool
14+
expected bool
15+
}{
16+
{
17+
name: "not_set_returns_default_true",
18+
envVar: "UNDEFINED_VAR_1",
19+
defaultValue: true,
20+
expected: true,
21+
},
22+
{
23+
name: "not_set_returns_default_false",
24+
envVar: "UNDEFINED_VAR_2",
25+
defaultValue: false,
26+
expected: false,
27+
},
28+
{
29+
name: "set_to_true",
30+
envVar: "TEST_VAR_TRUE",
31+
envValue: "true",
32+
setEnv: true,
33+
defaultValue: false,
34+
expected: true,
35+
},
36+
{
37+
name: "set_to_false",
38+
envVar: "TEST_VAR_FALSE",
39+
envValue: "false",
40+
setEnv: true,
41+
defaultValue: true,
42+
expected: false,
43+
},
44+
{
45+
name: "set_to_1",
46+
envVar: "TEST_VAR_1",
47+
envValue: "1",
48+
setEnv: true,
49+
defaultValue: false,
50+
expected: true,
51+
},
52+
{
53+
name: "set_to_0",
54+
envVar: "TEST_VAR_0",
55+
envValue: "0",
56+
setEnv: true,
57+
defaultValue: true,
58+
expected: false,
59+
},
60+
{
61+
name: "whitespace_trimmed",
62+
envVar: "TEST_VAR_SPACE",
63+
envValue: " true ",
64+
setEnv: true,
65+
defaultValue: false,
66+
expected: true,
67+
},
68+
{
69+
name: "invalid_returns_default_true",
70+
envVar: "TEST_VAR_INVALID_1",
71+
envValue: "invalid",
72+
setEnv: true,
73+
defaultValue: true,
74+
expected: true,
75+
},
76+
{
77+
name: "invalid_returns_default_false",
78+
envVar: "TEST_VAR_INVALID_2",
79+
envValue: "maybe",
80+
setEnv: true,
81+
defaultValue: false,
82+
expected: false,
83+
},
84+
}
85+
86+
for _, tt := range tests {
87+
t.Run(tt.name, func(t *testing.T) {
88+
if tt.setEnv {
89+
t.Setenv(tt.envVar, tt.envValue)
90+
}
91+
92+
result := getEnvBoolOrDefault(tt.envVar, tt.defaultValue)
93+
94+
if result != tt.expected {
95+
t.Errorf("got %v, expected %v", result, tt.expected)
96+
}
97+
})
98+
}
99+
}

‎cmd/flags.go‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package cmd
2+
3+
import "github.com/spf13/cobra"
4+
5+
// addDisableChangelogFlag registers the shared --disable-changelog flag on the
6+
// provided command, using the value from UPDATECLI_DISABLE_CHANGELOG as the
7+
// default when the flag is not explicitly passed.
8+
func addDisableChangelogFlag(cmd *cobra.Command, dest *bool) {
9+
cmd.Flags().BoolVar(
10+
dest,
11+
"disable-changelog",
12+
getEnvBoolOrDefault(DisableChangelogEnvVar, false),
13+
"Disable changelog retrieval to avoid unnecessary requests (env: "+DisableChangelogEnvVar+")",
14+
)
15+
}

‎cmd/flags_test.go‎

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package cmd
2+
3+
import (
4+
"strings"
5+
"testing"
6+
7+
"github.com/spf13/cobra"
8+
)
9+
10+
func TestAddDisableChangelogFlagRegistration(t *testing.T) {
11+
t.Setenv(DisableChangelogEnvVar, "false")
12+
13+
cmd := &cobra.Command{
14+
Use: "test",
15+
}
16+
17+
var disableChangelog bool
18+
addDisableChangelogFlag(cmd, &disableChangelog)
19+
20+
// Check that flag exists
21+
flag := cmd.Flags().Lookup("disable-changelog")
22+
if flag == nil {
23+
t.Fatal("flag not registered")
24+
}
25+
26+
// Check flag has help text
27+
if flag.Usage == "" {
28+
t.Error("flag help text is empty")
29+
}
30+
31+
// Check that env var name appears in help text
32+
if !strings.Contains(flag.Usage, DisableChangelogEnvVar) {
33+
t.Errorf(
34+
"flag help text does not mention env var %q: help text is %q",
35+
DisableChangelogEnvVar,
36+
flag.Usage,
37+
)
38+
}
39+
40+
// Check default value is "false" when env var not set
41+
if flag.DefValue != "false" {
42+
t.Errorf(
43+
"flag default value when no env var: got %q, expected %q",
44+
flag.DefValue,
45+
"false",
46+
)
47+
}
48+
}
49+
50+
func TestAddDisableChangelogFlagUsesEnvDefault(t *testing.T) {
51+
tests := []struct {
52+
name string
53+
envValue string
54+
expectedDef string
55+
}{
56+
{
57+
name: "env_var_true",
58+
envValue: "true",
59+
expectedDef: "true",
60+
},
61+
{
62+
name: "env_var_false",
63+
envValue: "false",
64+
expectedDef: "false",
65+
},
66+
{
67+
name: "env_var_1",
68+
envValue: "1",
69+
expectedDef: "true",
70+
},
71+
{
72+
name: "env_var_0",
73+
envValue: "0",
74+
expectedDef: "false",
75+
},
76+
}
77+
78+
for _, tt := range tests {
79+
t.Run(tt.name, func(t *testing.T) {
80+
t.Setenv(DisableChangelogEnvVar, tt.envValue)
81+
82+
cmd := &cobra.Command{
83+
Use: "test",
84+
}
85+
86+
var disableChangelog bool
87+
addDisableChangelogFlag(cmd, &disableChangelog)
88+
89+
flag := cmd.Flags().Lookup("disable-changelog")
90+
if flag == nil {
91+
t.Fatal("flag not registered")
92+
}
93+
94+
if flag.DefValue != tt.expectedDef {
95+
t.Errorf(
96+
"flag default value: got %q, expected %q",
97+
flag.DefValue,
98+
tt.expectedDef,
99+
)
100+
}
101+
})
102+
}
103+
}
104+
105+
func TestDisableChangelogFlagOverridesEnv(t *testing.T) {
106+
t.Setenv(DisableChangelogEnvVar, "true")
107+
108+
cmd := &cobra.Command{
109+
Use: "test",
110+
RunE: func(cmd *cobra.Command, args []string) error {
111+
return nil
112+
},
113+
}
114+
115+
var disableChangelog bool
116+
addDisableChangelogFlag(cmd, &disableChangelog)
117+
118+
cmd.SetArgs([]string{"--disable-changelog=false"})
119+
120+
err := cmd.Execute()
121+
if err != nil {
122+
t.Fatalf("unexpected error: %v", err)
123+
}
124+
125+
if disableChangelog {
126+
t.Error("expected flag to override env var")
127+
}
128+
}

‎cmd/pipeline_apply.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ var (
4242
e.Options.Pipeline.Target.DryRun = false
4343
e.Options.Pipeline.Target.CleanGitBranches = applyCleanGitBranches
4444
e.Options.Pipeline.Target.ExistingOnly = applyExistingOnly
45+
e.Options.Pipeline.DisableChangelog = disableChangelog
4546

4647
err = run("pipeline/apply")
4748
if err != nil {
@@ -67,5 +68,7 @@ func init() {
6768
pipelineApplyCmd.Flags().StringArrayVar(&pipelineIds, "pipeline-ids", []string{}, "Filter pipelines to apply by their IDs, accepted as a comma separated list")
6869
pipelineApplyCmd.Flags().StringArrayVar(&labels, "labels", []string{}, "Filter pipelines to apply by their labels, accepted as a comma separated list (key:value)")
6970

71+
addDisableChangelogFlag(pipelineApplyCmd, &disableChangelog)
72+
7073
pipelineCmd.AddCommand(pipelineApplyCmd)
7174
}

‎cmd/pipeline_diff.go‎

Lines changed: 3 additions & 0 deletions

0 commit comments

Comments
 (0)