diff --git a/pkg/cli/alpha/config-gen/cmd.go b/pkg/cli/alpha/config-gen/cmd.go index cda0696024f..bb280cda289 100644 --- a/pkg/cli/alpha/config-gen/cmd.go +++ b/pkg/cli/alpha/config-gen/cmd.go @@ -19,6 +19,7 @@ package configgen import ( "fmt" "io/ioutil" + "log" "os" "path/filepath" "strings" @@ -41,8 +42,7 @@ func NewCommand() *cobra.Command { legacyPlugin := os.Getenv("KUSTOMIZE_PLUGIN_CONFIG_STRING") err := yaml.Unmarshal([]byte(legacyPlugin), kp) if err != nil { - fmt.Fprintln(os.Stderr, err.Error()) - return nil + log.Fatal(err) } // Eager check to make sure pkged templates are found. @@ -51,8 +51,7 @@ func NewCommand() *cobra.Command { }) if err != nil { // this shouldn't fail if it was compiled correctly - fmt.Fprintln(os.Stderr, err.Error()) - return nil + log.Fatal(err) } c := framework.TemplateCommand{ @@ -252,32 +251,34 @@ EOF kustomize build --enable-alpha-plugins . `) - dir, dirErr := getPluginDir() - pluginFile := filepath.Join(dir, "KubebuilderConfigGen") - // command for installing the plugin install := &cobra.Command{ Use: "install-as-plugin", Short: "Install config-gen as a kustomize plugin", - Long: strings.TrimSpace(fmt.Sprintf(` -Write a script to %s for kustomize to locate as a plugin. -`, pluginFile)), - Example: strings.TrimSpace(` + Long: fmt.Sprintf(`Write a script to %s for kustomize to locate as a plugin. +This path will be written to $XDG_CONFIG_HOME if set, otherwise $HOME. +`, pluginScriptPath), + Example: ` kubebuilder alpha config-gen install-as-plugin -`), +`, RunE: func(cmd *cobra.Command, args []string) error { - if dirErr != nil { - return dirErr + hd, err := getPluginHomeDir() + if err != nil { + log.Fatal(err) } - fmt.Fprintf(cmd.OutOrStdout(), "writing kustomize plugin file at %s\n", pluginFile) + fullScriptPath := filepath.Join(hd, pluginScriptPath) + + fmt.Fprintf(cmd.OutOrStdout(), "writing kustomize plugin file at %s\n", fullScriptPath) + + dir, _ := filepath.Split(fullScriptPath) err = os.MkdirAll(dir, 0700) if err != nil { return err } - return ioutil.WriteFile(pluginFile, []byte(`#!/bin/bash + return ioutil.WriteFile(fullScriptPath, []byte(`#!/bin/bash KUSTOMIZE_FUNCTION=true kubebuilder alpha config-gen -`), 0500) +`), 0700) }, } c.AddCommand(install) @@ -285,7 +286,13 @@ KUSTOMIZE_FUNCTION=true kubebuilder alpha config-gen return c } -func getPluginDir() (string, error) { +var ( + // Qualified directory containing the config-gen plugin script. Child of plugin home dir. + pluginScriptPath = filepath.Join("kustomize", "plugin", "kubebuilder.sigs.k8s.io", "v1alpha1", + "kubebuilderconfiggen", "KubebuilderConfigGen") +) + +func getPluginHomeDir() (string, error) { xdg := os.Getenv("XDG_CONFIG_HOME") if xdg == "" { dir, err := os.UserHomeDir() @@ -294,6 +301,5 @@ func getPluginDir() (string, error) { } xdg = filepath.Join(dir, ".config") } - dir := filepath.Join(xdg, "kustomize", "plugin", "kubebuilder.sigs.k8s.io", "v1alpha1", "kubebuilderconfiggen") - return dir, nil + return xdg, nil }