Skip to content

Commit 5ed6c78

Browse files
committed
Do not print remediation messages not on --status
1 parent 0c1c5aa commit 5ed6c78

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

cmd/common.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -205,24 +205,29 @@ func prettyPrint(r *check.Controls, summary check.Summary) {
205205

206206
// Print remediations.
207207
if !noRemediations {
208+
var remediationOutput strings.Builder
208209
if summary.Fail > 0 || summary.Warn > 0 {
209-
colors[check.WARN].Printf("== Remediations %s ==\n", r.Type)
210210
for _, g := range r.Groups {
211211
for _, c := range g.Checks {
212-
if c.State == check.FAIL {
213-
fmt.Printf("%s %s\n", c.ID, c.Remediation)
212+
if c.State == check.FAIL && printStatus(check.FAIL) {
213+
remediationOutput.WriteString(fmt.Sprintf("%s %s\n", c.ID, c.Remediation))
214214
}
215-
if c.State == check.WARN {
215+
if c.State == check.WARN && printStatus(check.WARN) {
216216
// Print the error if test failed due to problem with the audit command
217217
if c.Reason != "" && c.Type != "manual" {
218-
fmt.Printf("%s audit test did not run: %s\n", c.ID, c.Reason)
218+
remediationOutput.WriteString(fmt.Sprintf("%s audit test did not run: %s\n", c.ID, c.Reason))
219219
} else {
220-
fmt.Printf("%s %s\n", c.ID, c.Remediation)
220+
remediationOutput.WriteString(fmt.Sprintf("%s %s\n", c.ID, c.Remediation))
221221
}
222222
}
223223
}
224224
}
225-
fmt.Println()
225+
output := remediationOutput.String()
226+
if len(output) > 0 {
227+
remediationOutput.WriteString("\n")
228+
fmt.Printf(colors[check.WARN].Sprintf("== Remediations %s ==\n", r.Type))
229+
fmt.Printf(remediationOutput.String())
230+
}
226231
}
227232
}
228233

cmd/common_test.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -756,27 +756,35 @@ func TestWriteStdoutOutputStatusList(t *testing.T) {
756756
statusList string
757757

758758
notContains []string
759+
contains []string
759760
}
760761
testCases := []testCase{
761762
{
762763
name: "statusList PASS",
763764
statusList: "PASS",
764-
notContains: []string{"INFO", "WARN", "FAIL"},
765+
notContains: []string{"INFO", "WARN", "FAIL", "== Remediations controlplane =="},
765766
},
766767
{
767768
name: "statusList PASS,INFO",
768769
statusList: "PASS,INFO",
769-
notContains: []string{"WARN", "FAIL"},
770+
notContains: []string{"WARN", "FAIL", "== Remediations controlplane =="},
771+
},
772+
{
773+
name: "statusList WARN",
774+
statusList: "WARN",
775+
notContains: []string{"INFO", "FAIL", "PASS"},
776+
contains: []string{"== Remediations controlplane =="},
770777
},
771778
{
772779
name: "statusList FAIL",
773780
statusList: "FAIL",
774-
notContains: []string{"INFO", "WARN", "PASS"},
781+
notContains: []string{"INFO", "WARN", "PASS", "== Remediations controlplane =="},
775782
},
776783
{
777784
name: "statusList empty",
778785
statusList: "",
779786
notContains: nil,
787+
contains: []string{"== Remediations controlplane =="},
780788
},
781789
}
782790

@@ -801,6 +809,10 @@ func TestWriteStdoutOutputStatusList(t *testing.T) {
801809
for _, n := range tt.notContains {
802810
assert.NotContains(t, string(out), n)
803811
}
812+
813+
for _, c := range tt.contains {
814+
assert.Contains(t, string(out), c)
815+
}
804816
}
805817
}
806818

0 commit comments

Comments
 (0)