diff --git a/main.go b/main.go index b399172..c338d2e 100644 --- a/main.go +++ b/main.go @@ -13,15 +13,19 @@ 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)) @@ -29,9 +33,15 @@ func main() { 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 @@ -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 diff --git a/test/tfplan b/test/tfplan new file mode 100644 index 0000000..d21a1c6 Binary files /dev/null and b/test/tfplan differ