Skip to content

Commit

Permalink
Merge pull request #31 from zekroTJA/main
Browse files Browse the repository at this point in the history
Add parameter for terraform executable (for compatibility with OpenTofu)
  • Loading branch information
orangekame3 authored Dec 20, 2024
2 parents 79fa5f4 + 06ccdd0 commit 1eeeffa
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
25 changes: 25 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ Copyright © 2023 Takafumi Miyanaga [email protected]
package cmd

import (
"fmt"
"os"
"os/exec"

"github.com/spf13/cobra"
)
Expand All @@ -18,11 +20,34 @@ You can interactivity select resource to ( plan | appply | destroy ) with target
`,
}

func init() {
executable := os.Getenv("TFTARGET_EXECUTABLE")
if executable == "" {
if existsInPath("terraform") {
executable = "terraform"
} else if existsInPath("tofu") {
executable = "tofu"
} else {
fmt.Println("Error: no terraform executable found in PATH")
os.Exit(1)
}
}
rootCmd.PersistentFlags().StringP("executable", "e", executable, "The name or path of the terraform executable")
}

// existsInPath returns true if name is available in any of the paths listed
// in the PATH variable.
func existsInPath(name string) bool {
_, err := exec.LookPath(name)
return err == nil
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
s.Suffix = " loading ..."
s.Color("green")

err := rootCmd.Execute()
if err != nil {
os.Exit(1)
Expand Down
9 changes: 6 additions & 3 deletions cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ func executePlan(cmd *cobra.Command, option string) ([]string, error) {
if err := validateFlagValue(action, validValues); action != "" && err != nil {
return nil, err
}

executable, _ := cmd.Flags().GetString("executable")
p, _ := cmd.Flags().GetInt("parallel")
planCmd := exec.Command("terraform", "plan", "-no-color", fmt.Sprintf("--parallelism=%d", p))
planCmd := exec.Command(executable, "plan", "-no-color", fmt.Sprintf("--parallelism=%d", p))
if option != "" {
planCmd = exec.Command("terraform", "plan", option, "-no-color", fmt.Sprintf("--parallelism=%d", p))
planCmd = exec.Command(executable, "plan", option, "-no-color", fmt.Sprintf("--parallelism=%d", p))
}

out, err := planCmd.CombinedOutput()
Expand Down Expand Up @@ -103,7 +105,8 @@ func slice2String(slice []string) string {

func genTargetCmd(cmd *cobra.Command, action, target string) bytes.Buffer {
var buf bytes.Buffer
buf.WriteString("terraform " + action + " -target=" + target)
executable, _ := cmd.Flags().GetString("executable")
buf.WriteString(executable + " " + action + " -target=" + target)
p, _ := cmd.Flags().GetInt("parallel")
buf.WriteString(fmt.Sprintf(" --parallelism=%d", p))
return buf
Expand Down
4 changes: 4 additions & 0 deletions cmd/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,10 @@ func Test_genTargetCmd(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// The --executable flag is a persistent flag set by the root command, but because the default value of it
// is not passed to the command because it was not executed through the root command, the flag is set here
// as flag with the expected test value set as default value.
tt.cmd.Flags().String("executable", "terraform", "")
got := genTargetCmd(tt.cmd, tt.action, tt.input)
if diff := cmp.Diff(got.String(), tt.want); diff != "" {
t.Errorf("genTargetCmd (-got +want):%s", diff)
Expand Down

0 comments on commit 1eeeffa

Please sign in to comment.