@@ -4,10 +4,8 @@ import (
44 "encoding/json"
55 "fmt"
66 "os"
7- "os/signal"
87 "path"
98 "strings"
10- "syscall"
119 "time"
1210
1311 "github.com/privateerproj/privateer-sdk/config"
@@ -32,12 +30,9 @@ type EvaluationSuite struct {
3230 loader DataLoader // loader is the function to load the payload
3331 config * config.Config // config is the global configuration for the plugin
3432
35- assessmentSuccesses int // successes is the number of successful evaluations
36- assessmentWarnings int // attempts is the number of evaluations attempted
37- assessmentFailures int // failures is the number of failed evaluations
38- evalSuccesses int // successes is the number of successful evaluations
39- evalFailures int // failures is the number of failed evaluations
40- evalWarnings int // warnings is the number of evaluations that need review
33+ evalSuccesses int // successes is the number of successful evaluations
34+ evalFailures int // failures is the number of failed evaluations
35+ evalWarnings int // warnings is the number of evaluations that need review
4136}
4237
4338// Execute is used to execute a list of ControlEvaluations provided by a Plugin and customized by user config
@@ -62,11 +57,15 @@ func (e *EvaluationSuite) Evaluate(name string) error {
6257 // Log each assessment result as a separate line
6358 for _ , assessment := range evaluation .Assessments {
6459 message := fmt .Sprintf ("%s: %s" , assessment .Requirement_Id , assessment .Message )
65- if assessment .Result == layer4 .Passed {
60+ // switch case the code below
61+ switch assessment .Result {
62+ case layer4 .Passed :
6663 e .config .Logger .Info (message )
67- } else if assessment . Result == layer4 .NeedsReview {
64+ case layer4 .NeedsReview :
6865 e .config .Logger .Warn (message )
69- } else if assessment .Result == layer4 .Failed || assessment .Result == layer4 .Unknown {
66+ case layer4 .Failed :
67+ e .config .Logger .Error (message )
68+ case layer4 .Unknown :
7069 e .config .Logger .Error (message )
7170 }
7271 }
@@ -90,11 +89,12 @@ func (e *EvaluationSuite) Evaluate(name string) error {
9089 if e .Corrupted_State {
9190 return CORRUPTION_FOUND ()
9291 }
93- if e .Result == layer4 .Passed {
92+ switch e .Result {
93+ case layer4 .Passed :
9494 e .config .Logger .Info (output )
95- } else if e . Result == layer4 .NotRun {
95+ case layer4 .NotRun :
9696 e .config .Logger .Trace (output )
97- } else {
97+ default :
9898 e .config .Logger .Error (output )
9999 }
100100 return nil
@@ -158,7 +158,9 @@ func (e *EvaluationSuite) writeControlEvaluationsToFile(serviceName string, resu
158158 e .config .Logger .Error ("Error opening file" , "filepath" , filepath )
159159 return err
160160 }
161- defer file .Close ()
161+ defer func () {
162+ _ = file .Close ()
163+ }()
162164
163165 _ , err = file .Write (result )
164166 if err != nil {
@@ -177,24 +179,3 @@ func (e *EvaluationSuite) cleanup() (passed bool) {
177179 }
178180 return ! e .Corrupted_State
179181}
180-
181- // closeHandler creates a 'listener' on a new goroutine which will notify the
182- // program if it receives an interrupt from the OS. We then handle this by calling
183- // our clean up procedure and exiting the program.
184- // Ref: https://golangcode.com/handle-ctrl-c-exit-in-terminal/
185- func (e * EvaluationSuite ) closeHandler () {
186- c := make (chan os.Signal , 1 )
187- signal .Notify (c , os .Interrupt , syscall .SIGTERM )
188- go func () {
189- <- c
190- e .config .Logger .Error ("[ERROR] Execution aborted - %v" , "SIGTERM" )
191- e .config .Logger .Warn ("[WARN] Attempting to revert changes made by the terminated Plugin. Do not interrupt this process." )
192- if e .cleanup () {
193- e .config .Logger .Info ("Cleanup did not encounter an error." )
194- os .Exit (0 )
195- } else {
196- e .config .Logger .Error ("[ERROR] Cleanup returned an error, and may not be complete." )
197- os .Exit (2 )
198- }
199- }()
200- }
0 commit comments