Add support for -reject and update README

This commit is contained in:
Yann Hamon 2020-11-08 10:36:53 +01:00
parent 358f145023
commit b162c5b6f5
5 changed files with 47 additions and 12 deletions

View file

@ -13,6 +13,7 @@ type Config struct {
Files []string
SchemaLocations []string
SkipKinds map[string]bool
RejectKinds map[string]bool
OutputFormat string
KubernetesVersion string
NumberOfWorkers int
@ -34,7 +35,7 @@ func (ap *arrayParam) Set(value string) error {
return nil
}
func skipKinds(skipKindsCSV string) map[string]bool {
func splitCSV(skipKindsCSV string) map[string]bool {
splitKinds := strings.Split(skipKindsCSV, ",")
skipKinds := map[string]bool{}
@ -49,7 +50,7 @@ func skipKinds(skipKindsCSV string) map[string]bool {
func FromFlags(progName string, args []string) (Config, string, error) {
var schemaLocationsParam arrayParam
var skipKindsCSV string
var skipKindsCSV, rejectKindsCSV string
flags := flag.NewFlagSet(progName, flag.PanicOnError)
var buf bytes.Buffer
flags.SetOutput(&buf)
@ -60,6 +61,7 @@ func FromFlags(progName string, args []string) (Config, string, error) {
flags.StringVar(&c.KubernetesVersion, "kubernetes-version", "1.18.0", "version of Kubernetes to validate against")
flags.Var(&schemaLocationsParam, "schema-location", "override schemas location search path (can be specified multiple times)")
flags.StringVar(&skipKindsCSV, "skip", "", "comma-separated list of kinds to ignore")
flags.StringVar(&rejectKindsCSV, "reject", "", "comma-separated list of kinds to reject")
flags.BoolVar(&c.ExitOnError, "exit-on-error", false, "immediately stop execution when the first error is encountered")
flags.BoolVar(&c.IgnoreMissingSchemas, "ignore-missing-schemas", false, "skip files with missing schemas instead of failing")
flags.BoolVar(&c.Summary, "summary", false, "print a summary at the end")
@ -75,7 +77,8 @@ func FromFlags(progName string, args []string) (Config, string, error) {
err := flags.Parse(args)
c.SkipKinds = skipKinds(skipKindsCSV)
c.SkipKinds = splitCSV(skipKindsCSV)
c.RejectKinds = splitCSV(rejectKindsCSV)
c.SchemaLocations = schemaLocationsParam
if len(c.SchemaLocations) == 0 {
c.SchemaLocations = append(c.SchemaLocations, "https://kubernetesjsonschema.dev") // if not specified, default behaviour is to use kubernetesjson-schema.dev as registry

View file

@ -33,7 +33,7 @@ func TestSkipKindMaps(t *testing.T) {
},
},
} {
got := skipKinds(testCase.csvSkipKinds)
got := splitCSV(testCase.csvSkipKinds)
if !reflect.DeepEqual(got, testCase.expect) {
t.Errorf("%s - got %+v, expected %+v", testCase.name, got, testCase.expect)
}
@ -54,6 +54,7 @@ func TestFromFlags(t *testing.T) {
OutputFormat: "text",
SchemaLocations: []string{"https://kubernetesjsonschema.dev"},
SkipKinds: map[string]bool{},
RejectKinds: map[string]bool{},
},
},
{
@ -66,6 +67,7 @@ func TestFromFlags(t *testing.T) {
OutputFormat: "text",
SchemaLocations: []string{"https://kubernetesjsonschema.dev"},
SkipKinds: map[string]bool{},
RejectKinds: map[string]bool{},
},
},
{
@ -77,6 +79,7 @@ func TestFromFlags(t *testing.T) {
OutputFormat: "text",
SchemaLocations: []string{"https://kubernetesjsonschema.dev"},
SkipKinds: map[string]bool{"a": true, "b": true, "c": true},
RejectKinds: map[string]bool{},
},
},
{
@ -88,6 +91,7 @@ func TestFromFlags(t *testing.T) {
OutputFormat: "text",
SchemaLocations: []string{"https://kubernetesjsonschema.dev"},
SkipKinds: map[string]bool{},
RejectKinds: map[string]bool{},
Summary: true,
Verbose: true,
},
@ -95,7 +99,7 @@ func TestFromFlags(t *testing.T) {
{
[]string{"-ignore-missing-schemas", "-kubernetes-version", "1.16.0", "-n", "2", "-output", "json",
"-schema-location", "folder", "-schema-location", "anotherfolder", "-skip", "kinda,kindb", "-strict",
"-summary", "-verbose", "file1", "file2"},
"-reject", "kindc,kindd", "-summary", "-verbose", "file1", "file2"},
Config{
Files: []string{"file1", "file2"},
IgnoreMissingSchemas: true,
@ -104,6 +108,7 @@ func TestFromFlags(t *testing.T) {
OutputFormat: "json",
SchemaLocations: []string{"folder", "anotherfolder"},
SkipKinds: map[string]bool{"kinda": true, "kindb": true},
RejectKinds: map[string]bool{"kindc": true, "kindd": true},
Strict: true,
Summary: true,
Verbose: true,