Skip to content

Commit

Permalink
Merge pull request #5 from RoseSecurity/add-planfile-support
Browse files Browse the repository at this point in the history
Clean up error handling, support for plan files
  • Loading branch information
RoseSecurity authored Jun 6, 2024
2 parents 3bdf3eb + e20ff66 commit ae8408a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,35 @@ import (
)

func main() {
var tfPath, workingDir string
var tfPath, workingDir, planFile, outputFile string
var output string

flag.StringVar(&tfPath, "tfPath", "/usr/local/bin/terraform", "Path to Terraform binary")
flag.StringVar(&workingDir, "workingDir", ".", "Working directory for Terraform")
flag.StringVar(&planFile, "planFile", "", "Path to Terraform plan file")
flag.StringVar(&outputFile, "outputFile", "Terramaid.md", "Output file for Mermaid diagram")
flag.Parse()

ctx := context.Background()
tf, err := tfexec.NewTerraform(workingDir, tfPath)
if err != nil {
log.Fatalf("error creating new Terraform: %s", err)
log.Fatalf("error creating Terraform context: %s", err)
}

err = tf.Init(ctx, tfexec.Upgrade(true))
if err != nil {
log.Fatalf("error initializing Terraform: %s", err)
}

output, err := tf.Graph(ctx)
// Graph Terraform resources
if planFile != "" {
output, err = tf.Graph(ctx, tfexec.GraphPlan(planFile))
} else {
output, err = tf.Graph(ctx)
}

if err != nil {
log.Fatalf("error running tf.Graph: %s", err)
log.Fatalf("error running Terraform Graph command: %s", err)
}

// Parse the DOT output
Expand All @@ -50,7 +60,7 @@ func main() {

// Convert to Mermaid format
mermaidGraph := ConvertToMermaid(graph)
err = os.WriteFile("Terramaid.md", []byte(mermaidGraph), 0644)
err = os.WriteFile(outputFile, []byte(mermaidGraph), 0644)
if err != nil {
fmt.Println("Error writing to Terramaid file:", err)
return
Expand Down
Binary file added test/tfplan
Binary file not shown.

0 comments on commit ae8408a

Please sign in to comment.