-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from zekroTJA/main
Add parameter for terraform executable (for compatibility with OpenTofu)
- Loading branch information
Showing
3 changed files
with
35 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,9 @@ Copyright © 2023 Takafumi Miyanaga [email protected] | |
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
@@ -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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters