Skip to content

Commit

Permalink
Merge pull request #14 from ned1313/v0.0.8a
Browse files Browse the repository at this point in the history
V0.0.8a
  • Loading branch information
ned1313 authored Jun 12, 2024
2 parents b11fca3 + 4e77efc commit 6ad09f3
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 38 deletions.
21 changes: 13 additions & 8 deletions cmd/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,22 @@ var checkCmd = &cobra.Command{
return err
}

// Check to see if the .terraform directory exists
slog.Debug("check to see if the .terraform directory exists")
slog.Debug("check to see if terraform has been initialized")
msg, init := terraformInitialized(path)

// Make sure terraform is initialized
if err := terraformInitialized(path); err != nil {
return fmt.Errorf("terraform not initialized: %v", err)
if !init {
slog.Warn(msg)
slog.Warn("has terraform init been run?")
return nil
}

// Load the sourced modules
return check(path)

},
}

func check(path string) error {
// Load the sourced modules
// Get the modules used by the configuration
slog.Debug("get the modules used by the configuration")
sourcedMods, err := processModules(path)
Expand Down Expand Up @@ -110,8 +117,6 @@ var checkCmd = &cobra.Command{

fmt.Println("All modules match the mod lock file")
return nil

},
}

func init() {
Expand Down
22 changes: 18 additions & 4 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,28 @@ var initCmd = &cobra.Command{
return err
}

slog.Debug("check to see if the .terraform directory exists")
if err := terraformInitialized(path); err != nil {
return fmt.Errorf("terraform not initialized: %v", err)
slog.Debug("check to see if terraform has been initialized")
msg, init := terraformInitialized(path)

if !init {
slog.Warn(msg)
slog.Warn("has terraform init been run?")
return nil
}

// If the modFile already exists, run a check to see if any changes
// are required, otherwise create the modFile
slog.Debug("check to see if the " + modFileName + "file exists")
if _, err := os.Stat(path + modFileName); err == nil {
return fmt.Errorf("%v file already exists", modFileName)
fmt.Println("Existing mod lock file found")
checkErr := check(path)
if checkErr != nil {
slog.Error("Changes detected between configuration and lock file")
slog.Error(checkErr.Error())
return fmt.Errorf("run terrhash upgrade to update the mod lock file")
}
fmt.Println("No changes to mod lock file required")
return nil
}

slog.Debug("get the modules used by the configuration")
Expand Down
8 changes: 4 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,20 @@ func init() {
// when this action is called directly.
}

func terraformInitialized(path string) error {
func terraformInitialized(path string) (string, bool) {
// Check to see if Terraform has been initialized
slog.Debug("checking to see if Terraform has been initialized")
if !checkInit(path + ".terraform") {
return fmt.Errorf("terraform has not been initialized")
return ".terraform directory was not found", false
}

//Check to see if the modules.json file exists
slog.Debug("check to see if the modules.json file exists")
if _, err := os.Stat(path + ".terraform/modules/modules.json"); err != nil {
return fmt.Errorf("no modules found in .terraform/modules directory: %v", err)
return "modules.json file was not found", false
}

return nil
return "terraform has been initialized", true
}

func processModules(path string) (modules, error) {
Expand Down
18 changes: 11 additions & 7 deletions cmd/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"fmt"
"log/slog"
"os"
"slices"
"strings"

"github.com/jedib0t/go-pretty/v6/table"
Expand All @@ -37,12 +38,14 @@ var upgradeCmd = &cobra.Command{
if err != nil {
return err
}
// Check to see if the .terraform directory exists
slog.Debug("check to see if the .terraform directory exists")

// Make sure terraform is initialized
if err := terraformInitialized(path); err != nil {
return fmt.Errorf("terraform not initialized: %v", err)
slog.Debug("check to see if terraform has been initialized")
msg, init := terraformInitialized(path)

if !init {
slog.Warn(msg)
slog.Warn("has terraform init been run?")
return nil
}

// Load the sourced modules
Expand Down Expand Up @@ -112,8 +115,9 @@ var upgradeCmd = &cobra.Command{
if !autoApprove {
fmt.Print("Confirm changes by entering yes: ")
var in = bufio.NewReader(os.Stdin)
name, _ := in.ReadString('\n')
if strings.TrimSpace(name) != "yes" {
confirm := []string{"yes","y","Yes","Y"}
resp, _ := in.ReadString('\n')
if !(slices.Contains(confirm, strings.TrimSpace(resp))){
slog.Debug("changes not accepted for mod lock file update")
return fmt.Errorf("changes not accepted for mod lock file update")
}
Expand Down
26 changes: 13 additions & 13 deletions testing/basic_vnet/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
"Source": "registry.terraform.io/cloudposse/label/null",
"Version": "0.25.0",
"Dir": ".terraform/modules/label",
"Hash": "7ea228e333d6fc2d26f1406c37c80132bbca087a346595e3e87ea14d65e27afb"
"Hash": "5cd866e85fb83e0aa128ca796979736145242a92f78d2a36c46bf7cb6ad9f22c"
},
"vnet": {
"Key": "vnet",
"Source": "registry.terraform.io/Azure/vnet/azurerm",
"Version": "4.1.0",
"Dir": ".terraform/modules/vnet",
"Hash": "e746848b12092c4a1618efaed81278ac2065e060589a869c50143d8482255382"
"Hash": "7d60d2f0d9a29de849badce1ec13ad0763e812b217d69f79de79b3999e597682"
}
}
}

0 comments on commit 6ad09f3

Please sign in to comment.