Skip to content

Commit 57a8391

Browse files
committed
Add the json output, adjust the documentation
1 parent 027843d commit 57a8391

File tree

11 files changed

+553
-134
lines changed

11 files changed

+553
-134
lines changed

.task/checksum/install

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1b7ffb9a935aa0b08ea1c774c0758749
1+
9192b1e72423ee1c4741793e0965836d

README.md

Lines changed: 124 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ tfsumpy is a Python-based tool that summarizes Terraform plan files to provide a
88
## Features
99

1010
- 🔍 Detailed plan analysis with change breakdown
11-
- 📊 Clear summary statistics for resource changes
11+
- 📊 Multiple output formats (default, markdown, JSON)
1212
- 🔒 Automatic sensitive information redaction
1313
- 🎨 Color-coded output for better readability
1414
- 🔄 Detailed attribute change tracking
15+
- 📝 Template-based markdown output
16+
- 🔧 Extensible plugin system
1517

1618
## Installation
1719

@@ -25,6 +27,7 @@ Or install from source:
2527
cd tfsumpy
2628
pip install .
2729
```
30+
2831
## Usage
2932

3033
### Basic Usage
@@ -44,70 +47,158 @@ Basic summary:
4447

4548
Show detailed changes:
4649
```bash
47-
tfsumpy plan.json --changes
50+
tfsumpy plan.json --hide-changes=false
4851
```
4952

5053
Show resource details:
5154
```bash
52-
tfsumpy plan.json --details
55+
tfsumpy plan.json --detailed
5356
```
5457

55-
### Example Output
58+
### Output Formats
59+
60+
tfsumpy supports three output formats:
61+
62+
1. Default (console output):
63+
```bash
64+
tfsumpy plan.json
65+
```
5666

67+
2. Markdown:
5768
```bash
58-
Terraform Plan Analysis
59-
======================
60-
Total Changes: 3
61-
Create: 1
62-
Update: 1
63-
Delete: 1
69+
tfsumpy plan.json --output markdown
70+
```
6471

65-
Resource Changes:
66-
CREATE aws_s3_bucket: data_bucket
67-
+ bucket = "new-bucket"
72+
3. JSON:
73+
```bash
74+
tfsumpy plan.json --output json
75+
```
6876

69-
UPDATE aws_instance: web_server
70-
~ instance_type = t2.micro -> t2.small
77+
### Example Outputs
7178

72-
DELETE aws_security_group: old_sg
73-
- name = "old-sg"
79+
#### Default Output
80+
```
81+
Terraform Plan Analysis
82+
======================
83+
Total Changes: 3
84+
Create: 1
85+
Update: 1
86+
Delete: 1
87+
88+
Resource Changes:
89+
CREATE aws_s3_bucket: data_bucket
90+
+ bucket = "new-bucket"
91+
92+
UPDATE aws_instance: web_server
93+
~ instance_type = t2.micro -> t2.small
94+
95+
DELETE aws_security_group: old_sg
96+
- name = "old-sg"
7497
```
7598

76-
### Configuration
99+
#### Markdown Output
100+
```markdown
101+
# Terraform Plan Analysis Report
77102

78-
Create a custom configuration file (config.json):
103+
## Summary
104+
- **Total Resources**: 3
105+
- **Resources to Add**: 1
106+
- **Resources to Change**: 1
107+
- **Resources to Destroy**: 1
108+
109+
## Resource Changes
110+
### aws_s3_bucket.data_bucket
111+
#### Changes:
112+
- **bucket**: null → "new-bucket"
113+
114+
### aws_instance.web_server
115+
#### Changes:
116+
- **instance_type**: "t2.micro" → "t2.small"
117+
118+
### aws_security_group.old_sg
119+
#### Changes:
120+
- **name**: "old-sg" → null
79121

122+
---
123+
*Generated by tfsumpy on 2024-03-14 15:30:45*
124+
```
125+
126+
#### JSON Output
80127
```json
128+
{
129+
"metadata": {
130+
"timestamp": "2024-03-14T15:30:45.123456",
131+
"version": "1.0",
132+
"format": "json"
133+
},
134+
"summary": {
135+
"total_resources": 3,
136+
"resources_to_add": 1,
137+
"resources_to_change": 1,
138+
"resources_to_destroy": 1
139+
},
140+
"resources": [
81141
{
82-
"sensitive_patterns": [
142+
"type": "aws_s3_bucket",
143+
"name": "data_bucket",
144+
"action": "create",
145+
"provider": "aws",
146+
"module": "root",
147+
"changes": [
83148
{
84-
"pattern": "\\b(?:password|secret|key)\\b",
85-
"replacement": "[REDACTED]"
149+
"attribute": "bucket",
150+
"before": null,
151+
"after": "new-bucket"
86152
}
87-
],
88-
"risk_rules": {
89-
"high": [
90-
{
91-
"pattern": "\\bdelete\\b.*\\b(database|storage)\\b",
92-
"message": "Critical resource deletion"
93-
}
94-
]
95-
}
153+
]
154+
}
155+
]
156+
}
157+
```
158+
159+
### Deprecated Arguments
160+
161+
The following arguments are deprecated and will be removed in a future version:
162+
163+
- `--changes` → Use `--hide-changes=false` instead
164+
- `--details` → Use `--detailed` instead
165+
- `--markdown` → Use `--output markdown` instead
166+
167+
### Configuration
168+
169+
Create a custom configuration file (config.json):
170+
171+
```json
172+
{
173+
"sensitive_patterns": [
174+
{
175+
"pattern": "\\b(?:password|secret|key)\\b",
176+
"replacement": "[REDACTED]"
96177
}
178+
],
179+
"risk_rules": {
180+
"high": [
181+
{
182+
"pattern": "\\bdelete\\b.*\\b(database|storage)\\b",
183+
"message": "Critical resource deletion"
184+
}
185+
]
186+
}
187+
}
97188
```
98189

99190
Use the configuration:
100191

101192
```bash
102-
tfsumpy plan.json --config config.json
193+
tfsumpy plan.json --config config.json
103194
```
104195

105196
### Debug Mode
106197

107198
For troubleshooting or detailed logging:
108199

109200
```bash
110-
tfsumpy plan.json --debug
201+
tfsumpy plan.json --debug
111202
```
112203

113204
This will:
@@ -136,24 +227,6 @@ Please make sure to update tests as appropriate.
136227

137228
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
138229

139-
## Beta Markdown Output (New!)
140-
141-
You can generate a Markdown summary of your Terraform plan with:
142-
143-
```bash
144-
tfsumpy plan.json --markdown > plan_summary.md
145-
```
146-
147-
This will create a Markdown file with sections for summary, created, updated, and destroyed resources, and JSON code blocks for each resource change.
148-
149-
- **Created Resources**: 🟩
150-
- **Updated Resources**: 🟦
151-
- **Destroyed Resources**: 🟥
152-
153-
Each resource is shown as a JSON code block. For updates, both before and after states are shown.
154-
155-
> **Note:** Markdown output is a beta feature. Please report any issues or suggestions!
156-
157230
## Project Status
158231

159232
**Status:** Beta

docs/api/reporters.md

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Overview
44

5-
Reporters in **tfsumpy** are responsible for formatting and displaying the results of plan analysis. Each reporter implements a specific output format (e.g., CLI, Markdown, JSON). The primary built-in reporter is the `PlanReporter`, which provides human-friendly and Markdown output for Terraform plan summaries.
5+
Reporters in **tfsumpy** are responsible for formatting and displaying the results of plan analysis. Each reporter implements a specific output format (e.g., CLI, Markdown, JSON). The primary built-in reporter is the `PlanReporter`, which provides multiple output formats for Terraform plan summaries.
66

77
---
88

@@ -36,26 +36,54 @@ class ReporterInterface(ABC):
3636

3737
## PlanReporter
3838

39-
The `PlanReporter` is the default reporter for plan summaries. It supports both colorized CLI output and Markdown output for sharing or documentation.
39+
The `PlanReporter` is the default reporter for plan summaries. It supports three output formats:
40+
- Console output (default)
41+
- Markdown output (template-based)
42+
- JSON output (structured)
4043

4144
**Example usage:**
4245

4346
```python
4447
from tfsumpy.plan.reporter import PlanReporter
4548

4649
reporter = PlanReporter()
50+
51+
# Console output
4752
reporter.print_report(plan_results, show_changes=True)
53+
54+
# Markdown output
4855
reporter.print_report_markdown(plan_results, show_changes=True)
56+
57+
# JSON output
58+
reporter.print_report_json(plan_results, show_changes=True)
4959
```
5060

5161
**Parameters:**
5262
- `data`: The analysis results (from `PlanAnalyzer`)
5363
- `show_changes`: Show detailed attribute changes (bool)
5464
- `show_details`: Show full resource details (bool)
5565

56-
**Output:**
57-
- Prints formatted summary to stdout (or to a file/stream if specified)
58-
- Markdown output is suitable for PRs, documentation, or compliance
66+
**Output Formats:**
67+
68+
1. Console Output:
69+
- Color-coded text
70+
- Human-readable format
71+
- Interactive terminal display
72+
73+
2. Markdown Output:
74+
- Template-based formatting
75+
- Summary statistics
76+
- Resource changes
77+
- Detailed information (if enabled)
78+
- Timestamp and metadata
79+
80+
3. JSON Output:
81+
- Structured data format
82+
- Metadata (timestamp, version)
83+
- Summary statistics
84+
- Resource changes
85+
- Detailed information (if enabled)
86+
- Analysis results (if available)
5987

6088
---
6189

@@ -84,6 +112,8 @@ Register your custom reporter with the tfsumpy `Context` to use it in your workf
84112
---
85113

86114
## Notes
87-
- The `PlanReporter` supports both CLI and Markdown output.
115+
- The `PlanReporter` supports three output formats: console, markdown, and JSON.
88116
- You can direct output to a file or stream by passing a custom `output` to `BaseReporter`.
117+
- Markdown output uses Jinja2 templates for consistent formatting.
118+
- JSON output provides a structured format suitable for integration with other tools.
89119
- See the [Models API](models.md) for details on the data structures passed to reporters.

0 commit comments

Comments
 (0)