Skip to content

Commit 68db068

Browse files
committed
fix: make command flags available everywhere for get_flags
Signed-off-by: Russell Centanni <russell.centanni@gmail.com>
1 parent 8803d5a commit 68db068

5 files changed

Lines changed: 51 additions & 16 deletions

File tree

cmd/run_pipeline.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ func (cmd *RunPipelineCmd) Run(cobraCmd *cobra.Command, args []string, f factory
247247

248248
// set command in context
249249
if cobraCmd != nil {
250-
cmd.Ctx = values.WithFlags(cmd.Ctx, cobraCmd.Flags())
250+
cmd.Ctx = values.WithCommandFlags(cmd.Ctx, cobraCmd.Flags())
251251
}
252252
options := cmd.BuildOptions(cmd.ToConfigOptions())
253253
ctx, err := initialize(cmd.Ctx, f, options, cmd.Log)

e2e/tests/pipelines/pipelines.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,30 +42,41 @@ var _ = DevSpaceDescribe("pipelines", func() {
4242
framework.ExpectNoError(err)
4343
defer framework.ExpectDeleteNamespace(kubeClient, ns)
4444

45+
rootCmd := cmd.NewRootCmd(f)
46+
persistentFlags := rootCmd.PersistentFlags()
47+
globalFlags := flags.SetGlobalFlags(persistentFlags)
48+
globalFlags.NoWarn = true
49+
globalFlags.Namespace = ns
50+
globalFlags.Profiles = []string{"profile1"}
51+
52+
cmdCtx := values.WithCommandFlags(context.Background(), globalFlags.Flags)
53+
cmdCtx = values.WithFlagsMap(cmdCtx, map[string]string{
54+
"test": "test",
55+
"test2": "",
56+
})
57+
4558
devCmd := &cmd.RunPipelineCmd{
46-
GlobalFlags: &flags.GlobalFlags{
47-
NoWarn: true,
48-
Namespace: ns,
49-
},
50-
Pipeline: "dev",
51-
Ctx: values.WithFlagsMap(context.Background(), map[string]string{
52-
"test": "test",
53-
"test2": "",
54-
}),
59+
GlobalFlags: globalFlags,
60+
Pipeline: "dev",
61+
Ctx: cmdCtx,
5562
}
5663
err = devCmd.RunDefault(f)
5764
framework.ExpectNoError(err)
5865

5966
framework.ExpectLocalFileContentsImmediately("test.txt", "test\n")
6067
framework.ExpectLocalFileContentsImmediately("test2.txt", "\n")
68+
framework.ExpectLocalFileContentsImmediately("dev-profile.txt", "profile1\n")
6169
framework.ExpectLocalFileContentsImmediately("other.txt", "test\n")
6270
framework.ExpectLocalFileContentsImmediately("other2.txt", "false\n")
6371
framework.ExpectLocalFileContentsImmediately("other3.txt", "true\n")
72+
framework.ExpectLocalFileContentsImmediately("other-profile.txt", "profile1\n")
6473
framework.ExpectLocalFileContentsImmediately("dep1-test.txt", "test\n")
6574
framework.ExpectLocalFileContentsImmediately("dep1-test2.txt", "true\n")
75+
framework.ExpectLocalFileContentsImmediately("dep1-dev-profile.txt", "profile1\n")
6676
framework.ExpectLocalFileContentsImmediately("dep1-other.txt", "test\n")
6777
framework.ExpectLocalFileContentsImmediately("dep1-other2.txt", "false\n")
6878
framework.ExpectLocalFileContentsImmediately("dep1-other3.txt", "false\n")
79+
framework.ExpectLocalFileContentsImmediately("dep1-other-profile.txt", "profile1\n")
6980
})
7081

7182
ginkgo.It("should exec container", func() {

e2e/tests/pipelines/testdata/flags/dep1.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pipelines:
1919
echo $(get_flag other) > dep1-other.txt
2020
echo $(get_flag other2) > dep1-other2.txt
2121
echo $(get_flag other3) > dep1-other3.txt
22+
echo $(get_flag profile) > dep1-other-profile.txt
2223
2324
dev:
2425
flags:
@@ -32,4 +33,5 @@ pipelines:
3233
exit 1
3334
fi
3435
echo $(get_flag test3) > dep1-test2.txt
36+
echo $(get_flag profile) > dep1-dev-profile.txt
3537
run_pipelines other --set-flag other2=false

e2e/tests/pipelines/testdata/flags/devspace.yaml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pipelines:
2323
echo $(get_flag other) > other.txt
2424
echo $(get_flag other2) > other2.txt
2525
echo $(get_flag other3) > other3.txt
26+
echo $(get_flag profile) > other-profile.txt
2627
2728
dev:
2829
flags:
@@ -33,11 +34,19 @@ pipelines:
3334
run: |-
3435
echo "$(get_flag test)" > test.txt
3536
echo "$(get_flag test2)" > test2.txt
37+
echo "$(get_flag profile)" > dev-profile.txt
3638
3739
run_pipelines other --set-flag other2=false
3840
run_dependencies dep1 --set-flag test3=true --pipeline dev
3941
40-
41-
42+
profiles:
43+
- name: profile1
44+
patches:
45+
- op: add
46+
path: pipelines
47+
value:
48+
noop:
49+
run: |-
50+
echo "noop"
4251
4352

pkg/devspace/context/values/values.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const (
1717
rootNameKey
1818
devContextKey
1919
flagsKey
20+
commandFlagsKey
2021
)
2122

2223
// WithFlagsMap creates a new context with the given flags
@@ -25,7 +26,7 @@ func WithFlagsMap(parent context.Context, flagsMap map[string]string) context.Co
2526
}
2627

2728
// WithFlags creates a new context with the given flags
28-
func WithFlags(parent context.Context, flagSet *flag.FlagSet) context.Context {
29+
func WithCommandFlags(parent context.Context, flagSet *flag.FlagSet) context.Context {
2930
flagsMap := map[string]string{}
3031
flagSet.VisitAll(func(f *flag.Flag) {
3132
sliceType, ok := f.Value.(flag.SliceValue)
@@ -36,13 +37,15 @@ func WithFlags(parent context.Context, flagSet *flag.FlagSet) context.Context {
3637
}
3738
})
3839

39-
return WithFlagsMap(parent, flagsMap)
40+
gfc := WithValue(parent, commandFlagsKey, flagsMap)
41+
return WithValue(gfc, flagsKey, flagsMap)
4042
}
4143

4244
// FlagsFrom returns a context used to start and stop dev configurations
4345
func FlagsFrom(ctx context.Context) (map[string]string, bool) {
44-
flags, ok := ctx.Value(flagsKey).(map[string]string)
45-
return flags, ok
46+
flags, fOk := ctx.Value(flagsKey).(map[string]string)
47+
commandFlags, cOk := ctx.Value(commandFlagsKey).(map[string]string)
48+
return mergeFlags(commandFlags, flags), fOk && cOk
4649
}
4750

4851
// WithDevContext creates a new context with the dev context
@@ -102,3 +105,13 @@ func IsDependencyFrom(ctx context.Context) (bool, bool) {
102105
isDependency, ok := ctx.Value(dependencyKey).(bool)
103106
return isDependency, ok
104107
}
108+
109+
func mergeFlags(maps ...map[string]string) map[string]string {
110+
merged := map[string]string{}
111+
for _, m := range maps {
112+
for k, v := range m {
113+
merged[k] = v
114+
}
115+
}
116+
return merged
117+
}

0 commit comments

Comments
 (0)