Skip to content

Commit d2cb281

Browse files
authored
Merge pull request #12 from HENNGE/remove-refresh
Modify plan output instead of doing terraform refresh
2 parents 82d3b52 + eafb881 commit d2cb281

File tree

3 files changed

+19
-15
lines changed

3 files changed

+19
-15
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
.idea/
1+
.idea/
2+
.vscode

templates/tfcheck.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
<details><summary>Show Plan</summary>
1111

12-
```terraform {{ refresh_output }}{{ plan_output }}
12+
```terraform {{ plan_output }}
1313
```
1414

1515
</details>

tfcheck.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
FMT_CMD = "terraform fmt -check=true -write=false -recursive -diff"
1010
INIT_CMD = "terraform init -no-color"
1111
VALIDATE_CMD = "terraform validate -no-color"
12-
REFRESH_CMD = "terraform refresh -no-color"
13-
PLAN_CMD = "terraform plan -detailed-exitcode -refresh=false -no-color"
12+
PLAN_CMD = "terraform plan -detailed-exitcode -no-color"
1413

1514
parser = argparse.ArgumentParser()
1615
parser.add_argument("path", help="path to run terraform checks", type=str)
@@ -44,8 +43,6 @@ class CheckResult:
4443
fmt_output: str
4544
validate_success: bool
4645
validate_output: str
47-
refresh_success: bool
48-
refresh_output: str
4946
plan_returncode: int
5047
plan_success: bool
5148
plan_changes: bool
@@ -75,9 +72,6 @@ def fmt_result(self) -> str:
7572
def validate_result(self) -> str:
7673
return "success" if self.validate_success else "failed"
7774

78-
def refresh_result(self) -> str:
79-
return "success" if self.refresh_success else "failed"
80-
8175
def plan_result(self) -> str:
8276
return "success" if self.plan_success else "failed"
8377

@@ -115,9 +109,6 @@ def check(path: str) -> CheckResult:
115109
# Terraform validate
116110
validate = run(VALIDATE_CMD, path)
117111

118-
# Terraform refresh
119-
refresh = run(REFRESH_CMD, path)
120-
121112
# Terraform plan
122113
plan = run(PLAN_CMD, path)
123114

@@ -129,8 +120,6 @@ def check(path: str) -> CheckResult:
129120
fmt_output=fmt.stdout,
130121
validate_success=not validate.returncode,
131122
validate_output=validate.stdout,
132-
refresh_success=refresh.returncode,
133-
refresh_output=refresh.stdout,
134123
# Terraform plan return code 2 = Succeeded with non-empty diff (changes present)
135124
plan_returncode=plan.returncode,
136125
plan_success=plan.returncode in [0, 2],
@@ -145,6 +134,21 @@ def check(path: str) -> CheckResult:
145134
result = check(args.path)
146135
print(f"Terraform check on {args.path} {result.check_result_msg()}")
147136

137+
# Remove refreshing messages from plan output
138+
if args.hide_refresh:
139+
result.plan_output = "\n".join(
140+
line
141+
for line in result.plan_output.split("\n")
142+
if not any(
143+
pattern in line
144+
for pattern in (
145+
"Refreshing state...",
146+
"Reading...",
147+
"Read complete after",
148+
)
149+
)
150+
)
151+
148152
if args.report:
149153
args.report.write(
150154
template.render(
@@ -153,7 +157,6 @@ def check(path: str) -> CheckResult:
153157
check_result=result.check_result_msg(),
154158
fmt_result=result.fmt_result(),
155159
validate_result=result.validate_result(),
156-
refresh_output="" if args.hide_refresh else result.refresh_output,
157160
plan_result=result.plan_result(),
158161
plan_output=result.plan_output,
159162
plan_msg=result.plan_msg(),

0 commit comments

Comments
 (0)