Repository: datreeio/datree
Pull request #419 (2022-02-16 12:44:24)

feat: change test command to exit(2) when miconfigurations were found


Patch:
diff --git a/go.mod b/go.mod
index 80ca979..3bb27dc 100644
--- a/go.mod
+++ b/go.mod
@@ -35,6 +35,7 @@ require (
 	github.com/mattn/go-runewidth v0.0.9 // indirect
 	github.com/mitchellh/mapstructure v1.1.2 // indirect
 	github.com/pelletier/go-toml v1.2.0 // indirect
+	github.com/pkg/errors v0.9.1
 	github.com/pmezard/go-difflib v1.0.0 // indirect
 	github.com/spf13/afero v1.1.2 // indirect
 	github.com/spf13/cast v1.3.0 // indirect
diff --git a/go.sum b/go.sum
index e5b1822..abf02b4 100644
--- a/go.sum
+++ b/go.sum
@@ -165,6 +165,8 @@ github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 h1:KoWmjvw+nsYOo29YJK9
 github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8/go.mod h1:HKlIX3XHQyzLZPlr7++PzdhaXEj94dEiJgZDTsxEqUI=
 github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
 github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
+github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
+github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
 github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
 github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
 github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
diff --git a/main.go b/main.go
index 7612713..ee45b0c 100644
--- a/main.go
+++ b/main.go
@@ -1,13 +1,21 @@
 package main
 
 import (
+	"errors"
 	"os"
 
 	"github.com/datreeio/datree/cmd"
+	"github.com/datreeio/datree/cmd/test"
 )
 
+const DEFAULT_ERR_EXIT_CODE = 1
+const VIOLATIONS_FOUND_EXIT_CODE = 2
+
 func main() {
 	if err := cmd.Execute(); err != nil {
-		os.Exit(1)
+		if errors.Is(err, test.ViolationsFoundError) {
+			os.Exit(VIOLATIONS_FOUND_EXIT_CODE)
+		}
+		os.Exit(DEFAULT_ERR_EXIT_CODE)
 	}
 }


Test patch:
diff --git a/cmd/test/main.go b/cmd/test/main.go
index 0529997..0891d76 100644
--- a/cmd/test/main.go
+++ b/cmd/test/main.go
@@ -13,6 +13,7 @@ import (
 	"github.com/datreeio/datree/bl/validation"
 	"github.com/datreeio/datree/pkg/ciContext"
 	"github.com/datreeio/datree/pkg/cliClient"
+	"github.com/pkg/errors"
 
 	"github.com/datreeio/datree/pkg/extractor"
 	"github.com/datreeio/datree/pkg/localConfig"
@@ -90,6 +91,8 @@ type LocalConfig interface {
 	GetLocalConfiguration() (*localConfig.ConfigContent, error)
 }
 
+var ViolationsFoundError = errors.New("")
+
 type TestCommandContext struct {
 	CliVersion   string
 	LocalConfig  LocalConfig
@@ -281,15 +284,15 @@ func test(ctx *TestCommandContext, paths []string, flags TestCommandFlags) error
 		}
 	}
 
-	var invocationFailedErr error = nil
-
 	if err != nil {
-		invocationFailedErr = err
-	} else if validationManager.InvalidYamlFilesCount() > 0 || validationManager.InvalidK8sFilesCount() > 0 || results.EvaluationResults.Summary.TotalFailedRules > 0 {
-		invocationFailedErr = fmt.Errorf("")
+		return err
+	}
+
+	if wereViolationsFound(validationManager, &results) {
+		return ViolationsFoundError
 	}
 
-	return invocationFailedErr
+	return nil
 }
 
 func evaluate(ctx *TestCommandContext, filesPaths []string, flags TestCommandFlags, cliId string) (*ValidationManager, *cliClient.CreateEvaluationResponse, evaluation.ResultType, error) {
@@ -370,3 +373,7 @@ func evaluate(ctx *TestCommandContext, filesPaths []string, flags TestCommandFla
 
 	return validationManager, createEvaluationResponse, results, err
 }
+
+func wereViolationsFound(validationManager *ValidationManager, results *evaluation.ResultType) bool {
+	return (validationManager.InvalidYamlFilesCount() > 0 || validationManager.InvalidK8sFilesCount() > 0 || results.EvaluationResults.Summary.TotalFailedRules > 0)
+}