Skip to content

Commit

Permalink
adding debug
Browse files Browse the repository at this point in the history
  • Loading branch information
Pacobart committed Nov 7, 2023
1 parent 083223e commit 87ace6f
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ This will create the following files:
- `imports.tf` file with all the import blocks for use with terraform 1.5 and newer
- `import.sh` file with all the import CLI commands for use with terraform 1.4 and older

## Flags
- `--debug` | `-d` = Debug mode. Prints api calls to std to determine if there are issues in responses

## Notes

This expects the terraform credentials token to exist in the following locations based on Operating System and currently only supports "app.terraform.io"
Expand Down
8 changes: 8 additions & 0 deletions internal/helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,20 @@ import (
"runtime"
)

var DEBUG = false

func Check(e error) {
if e != nil {
panic(e)
}
}

func Debug(s string) {
if DEBUG {
fmt.Println(s)
}
}

func GetTerraformTokenFromConfig() string {
homeDir, err := os.UserHomeDir()
Check(err)
Expand Down
1 change: 1 addition & 0 deletions internal/tfworkspaces/tfworkspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func GetWorkspaces(baseUrl string, token string, organization string) []Workspac

body, err := io.ReadAll(resp.Body)
helpers.Check(err)
helpers.Debug(string(body))

var workspaces WorkspaceList
err = json.Unmarshal(body, &workspaces)
Expand Down
6 changes: 6 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"fmt"
"os"
"slices"

"github.com/Pacobart/terraform-cloud-workspace-collector/internal/hcl"
"github.com/Pacobart/terraform-cloud-workspace-collector/internal/helpers"
Expand All @@ -13,6 +14,7 @@ import (
"github.com/Pacobart/terraform-cloud-workspace-collector/internal/tfworkspaces"
)

var DEBUG = false
var BASEURL = "https://app.terraform.io/api/v2"

func UpdateVariablesForWorkspace(ws *tfworkspaces.Workspace, variables []tfvariables.Variable) {
Expand All @@ -33,6 +35,10 @@ func main() {
os.Exit(1)
}

if slices.Contains(os.Args, "--debug") || slices.Contains(os.Args, "-d") {
helpers.DEBUG = true
}

orgName := os.Args[1]
apiToken := helpers.GetTerraformTokenFromConfig()
workspaces := tfworkspaces.GetWorkspaces(BASEURL, apiToken, orgName)
Expand Down

0 comments on commit 87ace6f

Please sign in to comment.