versionedTerraform/cmd/main.go

119 lines
3.3 KiB
Go
Raw Permalink Normal View History

2022-02-23 18:15:12 +00:00
package main
import (
"flag"
"fmt"
"os"
"os/exec"
"versionedTerraform"
)
const (
configFileLocation = "config"
shortConfigDirString = "/.versionedTerraform"
pwd = "."
terraformPrefix = "/terraform_"
)
var needsStable = true
2022-02-23 18:15:12 +00:00
func main() {
homeDir, _ := os.UserHomeDir()
configDirString := homeDir + shortConfigDirString
2022-06-18 13:41:00 +00:00
// Create configuration directory if it does not exist
_, err := os.Stat(configDirString)
if os.IsNotExist(err) {
err = versionedTerraform.CreateConfig(configDirString, configFileLocation)
}
2022-06-18 13:41:00 +00:00
// Create configuration file if it does not exist
_, err = os.Stat(configDirString + "/" + configFileLocation)
if os.IsNotExist(err) {
err = versionedTerraform.CreateConfig(configDirString, configFileLocation)
}
2022-06-18 13:41:00 +00:00
// Let the user know if we couldn't create the config directory or file
if err != nil {
fmt.Printf("Unable to create config directory: %v", err)
}
2022-02-23 18:15:12 +00:00
configDir := os.DirFS(configDirString)
workingDir := os.DirFS(pwd)
var versionsFromConfig []versionedTerraform.SemVersion
flag.Parse()
args := flag.Args()
2022-06-18 13:41:00 +00:00
//Load available versions from configuration file
versionsFromConfig, err = versionedTerraform.LoadVersionsFromConfig(configDir, configFileLocation)
if err != nil {
2022-04-27 23:20:10 +00:00
fmt.Printf("Unable to read config: %v\n", err)
os.Exit(1)
}
2022-06-18 13:41:00 +00:00
//Check if we need to update available versions with terraform's website
//Then update configuration if we do
//todo move this above loading the config
2022-02-23 18:15:12 +00:00
needsUpdate, err := versionedTerraform.NeedToUpdateAvailableVersions(configDir, configFileLocation)
if os.ErrNotExist == err {
2022-04-27 23:20:10 +00:00
fmt.Printf("Unable to update version: %v\n", err)
}
fileHandle, _ := os.OpenFile(configDirString+"/"+configFileLocation, os.O_RDWR, 0666)
defer fileHandle.Close()
2022-02-23 18:15:12 +00:00
if needsUpdate {
versionedTerraform.UpdateConfig(*fileHandle)
}
2022-06-18 13:41:00 +00:00
// Load a slice of versions which have already been installed
2022-02-23 18:15:12 +00:00
installedVersions, err := versionedTerraform.LoadInstalledVersions(configDir)
if err != nil {
fmt.Printf("Unable to verify installed verisons: %v", err)
os.Exit(1)
}
var vSlice []string
for _, v := range versionsFromConfig {
vSlice = append(vSlice, v.ToString())
}
2022-06-18 13:41:00 +00:00
// Check if stable version of terraform is required
needsStable, err = versionedTerraform.ConfigRequiresStable(*fileHandle)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to open config file, defaulting to stable versions of terraform only")
}
2022-06-18 13:41:00 +00:00
// Load version required from terraform directory
ver, err := versionedTerraform.GetVersionFromFile(workingDir, vSlice, needsStable)
2022-02-23 18:15:12 +00:00
if err != nil {
fmt.Printf("Unable to retrieve terraform version from files: %v", err)
}
if !ver.Version.VersionInSlice(installedVersions) {
fmt.Printf("Installing terraform version %s\n\n", ver.Version.ToString())
err = ver.InstallTerraformVersion()
if err != nil {
fmt.Printf("Unable to install terraform version: %v", err)
}
}
2022-06-18 13:41:00 +00:00
// Execute terraform
2022-02-23 18:15:12 +00:00
terraformFile := configDirString + terraformPrefix + ver.VersionToString()
argsForTerraform := append([]string{""}, args...)
cmd := exec.Cmd{
Path: terraformFile,
Args: argsForTerraform,
Env: os.Environ(),
Dir: pwd,
Stdin: os.Stdin,
Stdout: os.Stdout,
Stderr: os.Stderr,
}
2022-10-25 13:02:00 +00:00
if err := cmd.Run(); err != nil {
2022-10-21 13:39:06 +00:00
if exitError, ok := err.(*exec.ExitError); ok {
2022-10-25 13:02:00 +00:00
os.Exit(exitError.ExitCode())
2022-10-21 13:39:06 +00:00
}
}
2022-02-23 18:15:12 +00:00
}