From 070eec1e573be6b224df5f3de0aed486f272c848 Mon Sep 17 00:00:00 2001 From: Philippe Martin Date: Tue, 8 Nov 2022 09:23:48 +0100 Subject: [PATCH] Pass the env config into the context passed to CLI methods --- pkg/config/context/context.go | 25 +++++++++++++++++++++++++ pkg/odo/genericclioptions/runnable.go | 2 ++ 2 files changed, 27 insertions(+) create mode 100644 pkg/config/context/context.go diff --git a/pkg/config/context/context.go b/pkg/config/context/context.go new file mode 100644 index 00000000000..db401c90bec --- /dev/null +++ b/pkg/config/context/context.go @@ -0,0 +1,25 @@ +package context + +import ( + "context" + + "github.com/redhat-developer/odo/pkg/config" +) + +type contextKey struct{} + +var key = contextKey{} + +// WithEnvConfig sets the environment configuration in ctx +func WithEnvConfig(ctx context.Context, val config.Configuration) context.Context { + return context.WithValue(ctx, key, val) +} + +// GetEnvConfig returns the environment configuration from ctx +func GetEnvConfig(ctx context.Context) config.Configuration { + value := ctx.Value(key) + if cast, ok := value.(config.Configuration); ok { + return cast + } + panic("GetEnvConfig can be called only after WithEnvConfig has been called") +} diff --git a/pkg/odo/genericclioptions/runnable.go b/pkg/odo/genericclioptions/runnable.go index 7767f06e165..7c3674aabd1 100644 --- a/pkg/odo/genericclioptions/runnable.go +++ b/pkg/odo/genericclioptions/runnable.go @@ -32,6 +32,7 @@ import ( "k8s.io/klog" "k8s.io/utils/pointer" + envcontext "github.com/redhat-developer/odo/pkg/config/context" fcontext "github.com/redhat-developer/odo/pkg/odo/commonflags/context" odocontext "github.com/redhat-developer/odo/pkg/odo/context" "github.com/redhat-developer/odo/pkg/preference" @@ -186,6 +187,7 @@ func GenericRun(o Runnable, cmd *cobra.Command, args []string) { o.SetClientset(deps) ctx := cmdLineObj.Context() + ctx = envcontext.WithEnvConfig(ctx, *envConfig) ctx = fcontext.WithJsonOutput(ctx, commonflags.GetJsonOutputValue(cmdLineObj)) ctx = fcontext.WithRunOn(ctx, platform) ctx = odocontext.WithApplication(ctx, defaultAppName)