Skip to content

Commit

Permalink
Pass the env config into the context passed to CLI methods
Browse files Browse the repository at this point in the history
  • Loading branch information
feloy committed Nov 8, 2022
1 parent 3d687a3 commit 070eec1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
25 changes: 25 additions & 0 deletions pkg/config/context/context.go
Original file line number Diff line number Diff line change
@@ -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")
}
2 changes: 2 additions & 0 deletions pkg/odo/genericclioptions/runnable.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 070eec1

Please sign in to comment.