Unverified Commit b11c0d16 authored by hadar-co's avatar hadar-co Committed by GitHub
Browse files

Merge pull request #419 from datreeio/DAT-415-test-exit-code

feat: change test command to exit(2) when miconfigurations were found
parents 7630aa97 bebdf2e9
Showing with 25 additions and 7 deletions
+25 -7
......@@ -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)
}
......@@ -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
......
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)
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment