diff --git a/.gitignore b/.gitignore index 131d11df14..df3728508a 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ # Created by devspace init /.devspace/ /chart/ +/test # Build files *.exe diff --git a/cmd/flags/flags.go b/cmd/flags/flags.go index 1955dd587b..301fff40d2 100644 --- a/cmd/flags/flags.go +++ b/cmd/flags/flags.go @@ -50,6 +50,7 @@ func (gf *GlobalFlags) ToConfigOptions() *loader.ConfigOptions { Profile: gf.Profile, ConfigPath: gf.ConfigPath, KubeContext: gf.KubeContext, + Namespace: gf.Namespace, Vars: gf.Vars, } } diff --git a/cmd/root.go b/cmd/root.go index f0f2e30fe4..3d3df33c2d 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -13,6 +13,7 @@ import ( "github.com/devspace-cloud/devspace/cmd/status" "github.com/devspace-cloud/devspace/cmd/update" "github.com/devspace-cloud/devspace/cmd/use" + "github.com/devspace-cloud/devspace/pkg/devspace/config/loader" "github.com/devspace-cloud/devspace/pkg/devspace/plugin" "github.com/devspace-cloud/devspace/pkg/devspace/upgrade" "github.com/devspace-cloud/devspace/pkg/util/analytics/cloudanalytics" @@ -149,5 +150,6 @@ func BuildRoot(f factory.Factory) *cobra.Command { // Add plugin commands plugin.AddPluginCommands(rootCmd, plugins, "") + loader.AddPredefinedVars(plugins) return rootCmd } diff --git a/pkg/devspace/config/loader/get.go b/pkg/devspace/config/loader/get.go index 29d986185b..048cf9037b 100644 --- a/pkg/devspace/config/loader/get.go +++ b/pkg/devspace/config/loader/get.go @@ -123,6 +123,7 @@ func (l *configLoader) New() *latest.Config { type ConfigOptions struct { Profile string KubeContext string + Namespace string ConfigPath string GeneratedConfig *generated.Config diff --git a/pkg/devspace/config/loader/predefined_vars.go b/pkg/devspace/config/loader/predefined_vars.go index 51de5487e8..817ff51de5 100644 --- a/pkg/devspace/config/loader/predefined_vars.go +++ b/pkg/devspace/config/loader/predefined_vars.go @@ -1,7 +1,10 @@ package loader import ( + "bytes" "fmt" + "github.com/devspace-cloud/devspace/pkg/devspace/plugin" + "github.com/pkg/errors" "path/filepath" "strconv" "strings" @@ -162,6 +165,27 @@ var predefinedVars = map[string]func(loader *configLoader) (string, error){ }, } +func AddPredefinedVars(plugins []plugin.Metadata) { + for _, p := range plugins { + pluginFolder := p.PluginFolder + for _, variable := range p.Vars { + v := variable + predefinedVars[variable.Name] = func(configLoader *configLoader) (string, error) { + buffer := &bytes.Buffer{} + err := plugin.CallPluginExecutable(filepath.Join(pluginFolder, plugin.PluginBinary), v.BaseArgs, map[string]string{ + "DEVSPACE_PLUGIN_KUBE_CONTEXT_FLAG": configLoader.options.KubeContext, + "DEVSPACE_PLUGIN_KUBE_NAMESPACE_FLAG": configLoader.options.Namespace, + }, buffer) + if err != nil { + return "", errors.Wrapf(err, "executing plugin: %s", buffer.String()) + } + + return strings.TrimSpace(buffer.String()), nil + } + } + } +} + func (l *configLoader) resolvePredefinedVar(name string) (bool, string, error) { name = strings.ToUpper(name) if getVar, ok := predefinedVars[name]; ok { diff --git a/pkg/devspace/deploy/deployer/kubectl/kubectl.go b/pkg/devspace/deploy/deployer/kubectl/kubectl.go index e607045546..51f5f7c6d6 100644 --- a/pkg/devspace/deploy/deployer/kubectl/kubectl.go +++ b/pkg/devspace/deploy/deployer/kubectl/kubectl.go @@ -330,10 +330,10 @@ func (d *DeployConfig) buildManifests(manifest string) ([]*unstructured.Unstruct } func (d *DeployConfig) isKustomizeInstalled(path string) bool { - out, err := d.commandExecuter.RunCommand(path, []string{"version"}) + _, err := d.commandExecuter.RunCommand(path, []string{"version"}) if err != nil { return false } - return strings.Index(string(out), `kustomize`) != -1 + return true } diff --git a/pkg/devspace/plugin/plugin.go b/pkg/devspace/plugin/plugin.go index d56feeceb1..44ad3d45c3 100644 --- a/pkg/devspace/plugin/plugin.go +++ b/pkg/devspace/plugin/plugin.go @@ -11,6 +11,7 @@ import ( "github.com/mitchellh/go-homedir" "github.com/pkg/errors" "github.com/spf13/cobra" + "io" "io/ioutil" "os" "os/exec" @@ -22,11 +23,11 @@ var encoding = base32.StdEncoding.WithPadding('0') const pluginYaml = "plugin.yaml" -var pluginBinary = "binary" +var PluginBinary = "binary" func init() { if runtime.GOOS == "windows" { - pluginBinary += ".exe" + PluginBinary += ".exe" } } @@ -96,6 +97,7 @@ func (c *client) install(path, version string) error { return err } + pluginFolder = filepath.Join(pluginFolder, Encode(path)) err = os.MkdirAll(pluginFolder, 0755) if err != nil { return err @@ -106,13 +108,12 @@ func (c *client) install(path, version string) error { return err } - pluginFolder = filepath.Join(pluginFolder, Encode(path)) err = ioutil.WriteFile(filepath.Join(pluginFolder, pluginYaml), out, 0666) if err != nil { return err } - outBinaryPath := filepath.Join(pluginFolder, pluginBinary) + outBinaryPath := filepath.Join(pluginFolder, PluginBinary) err = c.installer.DownloadBinary(path, version, binaryPath, outBinaryPath) if err != nil { return errors.Wrap(err, "download plugin binary") @@ -292,7 +293,6 @@ func Decode(encoded string) ([]byte, error) { func AddPluginCommands(base *cobra.Command, plugins []Metadata, subCommand string) { for _, plugin := range plugins { pluginFolder := plugin.PluginFolder - for _, pluginCommand := range plugin.Commands { if pluginCommand.SubCommand == subCommand { md := pluginCommand @@ -308,7 +308,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) + return CallPluginExecutable(filepath.Join(pluginFolder, PluginBinary), newArgs, nil, os.Stdout) }, // This passes all the flags to the subcommand. DisableFlagParsing: true, @@ -322,12 +322,16 @@ func AddPluginCommands(base *cobra.Command, plugins []Metadata, subCommand strin // This function is used to setup the environment for the plugin and then // call the executable specified by the parameter 'main' -func callPluginExecutable(main string, argv []string) error { +func CallPluginExecutable(main string, argv []string, extraEnvVars map[string]string, out io.Writer) error { env := os.Environ() + for k, v := range extraEnvVars { + env = append(env, k + "=" + v) + } + prog := exec.Command(main, argv...) prog.Env = env prog.Stdin = os.Stdin - prog.Stdout = os.Stdout + prog.Stdout = out prog.Stderr = os.Stderr if err := prog.Run(); err != nil { if eerr, ok := err.(*exec.ExitError); ok { diff --git a/pkg/util/factory/factory.go b/pkg/util/factory/factory.go index d9811dbc44..9fb47bb717 100644 --- a/pkg/util/factory/factory.go +++ b/pkg/util/factory/factory.go @@ -20,11 +20,9 @@ import ( "github.com/devspace-cloud/devspace/pkg/devspace/plugin" "github.com/devspace-cloud/devspace/pkg/devspace/registry" "github.com/devspace-cloud/devspace/pkg/devspace/services" - "github.com/devspace-cloud/devspace/pkg/devspace/services" "github.com/devspace-cloud/devspace/pkg/devspace/services/targetselector" "github.com/devspace-cloud/devspace/pkg/util/kubeconfig" "github.com/devspace-cloud/devspace/pkg/util/log" - "github.com/docker/docker/pkg/plugins" ) // Factory is the main interface for various client creations diff --git a/test/plugin.yaml b/test/plugin.yaml new file mode 100644 index 0000000000..7b799f878e --- /dev/null +++ b/test/plugin.yaml @@ -0,0 +1,15 @@ +name: test +version: 0.0.3 +commands: + - name: "test" + baseArgs: ["test"] + - name: "subtest" + baseArgs: ["sutest"] + subCommand: "add" +vars: + - name: DEVSPACE_TEST + baseArgs: ["var"] +binaries: + - os: darwin + arch: amd64 + path: main \ No newline at end of file diff --git a/test/test.go b/test/test.go index f7bc20bfa1..aca2758a4e 100644 --- a/test/test.go +++ b/test/test.go @@ -2,9 +2,66 @@ package main import ( "fmt" + "github.com/spf13/cobra" "os" ) func main() { - fmt.Println(os.RemoveAll("saagdafshkjakfjhasdhkjahksdhkjdashkjadskhjdhkjaskjhasdjk")) + rootCmd := &cobra.Command{ + Use: "sdfssfd", + Short: "Welcome to the DevSpace!", + Long: `Example bois`, + } + + testCmd := &cobra.Command{ + Use: "test", + Short: "Welcome to the DevSpace!", + Long: `Example bois`, + } + + testCmd.AddCommand(&cobra.Command{ + Use: "abc", + Short: "Welcome to the DevSpace!", + Long: `Example bois`, + RunE: func(cmd *cobra.Command, args []string) error { + fmt.Printf("abc v2 %v", args) + return nil + }, + }) + testCmd.AddCommand(&cobra.Command{ + Use: "def", + Short: "Welcome to the DevSpace!", + Long: `Example bois`, + RunE: func(cmd *cobra.Command, args []string) error { + fmt.Printf("def %v", args) + return nil + }, + }) + + rootCmd.AddCommand(testCmd) + rootCmd.AddCommand(&cobra.Command{ + Use: "sutest", + Short: "Welcome to the DevSpace!", + Long: `Example bois`, + RunE: func(cmd *cobra.Command, args []string) error { + fmt.Printf("sutest %v", args) + return nil + }, + }) + rootCmd.AddCommand(&cobra.Command{ + Use: "var", + Short: "Welcome to the DevSpace!", + Long: `Example bois`, + RunE: func(cmd *cobra.Command, args []string) error { + for _, v := range os.Environ() { + fmt.Println(v) + } + return nil + }, + }) + + err := rootCmd.Execute() + if err != nil { + fmt.Println(err) + } } \ No newline at end of file