Skip to content

Commit

Permalink
Implement --binary to set path to alternative kustomize
Browse files Browse the repository at this point in the history
  • Loading branch information
ioboi committed Apr 8, 2024
1 parent 8a59629 commit f1b3511
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
9 changes: 8 additions & 1 deletion cmd/kustomize.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

var outputDotDir *string
var version *bool
var binary *string

var kustomizeCmd = &cobra.Command{
Use: "kustomize [rootDir]",
Expand All @@ -24,8 +25,13 @@ var kustomizeCmd = &cobra.Command{
},
Long: `Generate a DOT file to visualize the dependencies between your kustomize components`,
RunE: func(cmd *cobra.Command, args []string) error {
kustomizeCmd := "kustomize"
if *binary != "" {
kustomizeCmd = *binary
}

if *version {
kustomizeCmd := exec.CommandContext(cmd.Context(), "kustomize", "version")
kustomizeCmd := exec.CommandContext(cmd.Context(), kustomizeCmd, "version")
kustomizeCmd.Stdout = cmd.OutOrStdout()
kustomizeCmd.Stderr = cmd.OutOrStderr()
if err := kustomizeCmd.Run(); err != nil {
Expand All @@ -43,6 +49,7 @@ func init() {
kustomizeCmd.AddCommand(kustomize.KustomizeBuildCmd)
rootCmd.AddCommand(kustomizeCmd)

binary = kustomizeCmd.Flags().String("binary", "", "Alternative kustomize binary")
version = kustomizeCmd.Flags().BoolP("version", "v", false, "Display version of kustomize")
outputDotDir = kustomizeCmd.Flags().StringP("output-dir", "o", ".", "Output directory")
}
6 changes: 5 additions & 1 deletion cmd/kustomize/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ var KustomizeBuildCmd = &cobra.Command{
Args: cobra.ExactArgs(1),
Long: `Build all kustomize file within parent directory`,
RunE: func(cmd *cobra.Command, args []string) error {
return kustomize.BuildAll(args[0], *outputBuildDir)
kustomizeCommand, err := cmd.Flags().GetString("binary")
if err != nil {
return err
}
return kustomize.BuildAll(kustomizeCommand, args[0], *outputBuildDir)
},
}

Expand Down
4 changes: 2 additions & 2 deletions kustomize/kustomize.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/puzzle/goff/util"
)

func BuildAll(sourceDir, targetDir string) error {
func BuildAll(kustomizeCommand, sourceDir, targetDir string) error {

dirs, err := kustomizationfile.New().GetDirectories(sourceDir)
if err != nil {
Expand All @@ -27,7 +27,7 @@ func BuildAll(sourceDir, targetDir string) error {
var stdout strings.Builder

// TODO: Make customizable
cmd := exec.Command("kustomize", "build", absoluteKustomizationPath)
cmd := exec.Command(kustomizeCommand, "build", absoluteKustomizationPath)
cmd.Stdout = &stdout
if err := cmd.Run(); err != nil {
return fmt.Errorf("error running kustomize: %w", err)
Expand Down

0 comments on commit f1b3511

Please sign in to comment.