Skip to content

Commit

Permalink
pkg/cli/alpha/config-gen: command docs no longer depend on local envi…
Browse files Browse the repository at this point in the history
…ronment
  • Loading branch information
estroz committed Mar 18, 2021
1 parent 911f342 commit aca589e
Showing 1 changed file with 26 additions and 20 deletions.
46 changes: 26 additions & 20 deletions pkg/cli/alpha/config-gen/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package configgen
import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
Expand All @@ -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.
Expand All @@ -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{
Expand Down Expand Up @@ -252,40 +251,48 @@ 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)

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()
Expand All @@ -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
}

0 comments on commit aca589e

Please sign in to comment.