From 8c65e47100dfc8e3c28adbda9f66d47879b6d2f3 Mon Sep 17 00:00:00 2001 From: Mahendra Bagul Date: Tue, 1 Nov 2022 14:17:25 +0530 Subject: [PATCH] passed on devspace vars to plugin env --- cmd/root.go | 3 +++ pkg/devspace/plugin/plugin.go | 23 ++++++++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/cmd/root.go b/cmd/root.go index fcc7aac440..2037572186 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -294,6 +294,9 @@ Additional run commands: rootCmd.AddCommand(NewPurgeCmd(f, globalFlags, rawConfig)) // Add plugin commands + if rawConfig != nil && rawConfig.OriginalRawConfig != nil { + plugin.AddDevspaceVarsToPluginEnv(rawConfig.OriginalRawConfig["vars"]) + } plugin.AddPluginCommands(rootCmd, plugins, "") variable.AddPredefinedVars(plugins) return rootCmd diff --git a/pkg/devspace/plugin/plugin.go b/pkg/devspace/plugin/plugin.go index eabc226ce1..03464b5e15 100644 --- a/pkg/devspace/plugin/plugin.go +++ b/pkg/devspace/plugin/plugin.go @@ -8,6 +8,7 @@ import ( "github.com/ghodss/yaml" "github.com/loft-sh/devspace/pkg/devspace/config/constants" "github.com/loft-sh/devspace/pkg/util/log" + "sync" "io" "io/ioutil" @@ -35,6 +36,9 @@ func init() { } } +var devspaceVars = map[string]string{} +var devspaceVarsOnce sync.Once + type NewestVersionError struct { version string } @@ -346,6 +350,23 @@ func Decode(encoded string) ([]byte, error) { return encoding.DecodeString(encoded) } +func AddDevspaceVarsToPluginEnv(vars interface{}) { + devspaceVarsOnce.Do(func() { + if vars != nil { + devspaceVar, isMapStringInterface := vars.(map[string]interface{}) + if isMapStringInterface { + for key, value := range devspaceVar { + // only map[string]string will be processed, map[string]Variable will be skipped + vString, isString := value.(string) + if isString { + devspaceVars[key] = vString + } + } + } + } + }) +} + func AddPluginCommands(base *cobra.Command, plugins []Metadata, subCommand string) { for _, plugin := range plugins { pluginFolder := plugin.PluginFolder @@ -364,7 +385,7 @@ func AddPluginCommands(base *cobra.Command, plugins []Metadata, subCommand strin newArgs := []string{} newArgs = append(newArgs, md.BaseArgs...) newArgs = append(newArgs, args...) - return CallPluginExecutable(filepath.Join(pluginFolder, PluginBinary), newArgs, nil, os.Stdout) + return CallPluginExecutable(filepath.Join(pluginFolder, PluginBinary), newArgs, devspaceVars, os.Stdout) }, // This passes all the flags to the subcommand. DisableFlagParsing: true,