From 717a9ce7359ba1c51441157ea89b5f364953d249 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bruno=20Miguel=20Cust=C3=B3dio?= Date: Mon, 21 Oct 2019 21:17:46 +0100 Subject: [PATCH] Add the '--ignored-filename-patterns' flag. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Bruno Miguel Custódio --- main.go | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index 442576e..04447d7 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ import ( "io/ioutil" "os" "path/filepath" + "regexp" "runtime" "strings" @@ -21,10 +22,11 @@ import ( ) var ( - version = "dev" - commit = "none" - date = "unknown" - directories = []string{} + version = "dev" + commit = "none" + date = "unknown" + directories = []string{} + ignoredFilenamePatterns = []string{} // forceColor tells kubeval to use colored output even if // stdout is not a TTY @@ -157,6 +159,20 @@ func hasErrors(res []kubeval.ValidationResult) bool { return false } +// isIgnored returns whether the specified filename should be ignored. +func isIgnored(filename string) (bool, error) { + for _, p := range ignoredFilenamePatterns { + m, err := regexp.MatchString(p, filename) + if err != nil { + return false, err + } + if m { + return true, nil + } + } + return false, nil +} + func aggregateFiles(args []string) ([]string, error) { files := make([]string, len(args)) copy(files, args) @@ -167,7 +183,11 @@ func aggregateFiles(args []string) ([]string, error) { if err != nil { return err } - if !info.IsDir() && (strings.HasSuffix(info.Name(), ".yaml") || strings.HasSuffix(info.Name(), ".yml")) { + ignored, err := isIgnored(info.Name()) + if err != nil { + return err + } + if !info.IsDir() && (strings.HasSuffix(info.Name(), ".yaml") || strings.HasSuffix(info.Name(), ".yml")) && !ignored { files = append(files, path) } return nil @@ -200,6 +220,7 @@ func init() { RootCmd.Flags().BoolVarP(&forceColor, "force-color", "", false, "Force colored output even if stdout is not a TTY") RootCmd.SetVersionTemplate(`{{.Version}}`) RootCmd.Flags().StringSliceVarP(&directories, "directories", "d", []string{}, "A comma-separated list of directories to recursively search for YAML documents") + RootCmd.Flags().StringSliceVarP(&ignoredFilenamePatterns, "ignored-filename-patterns", "i", []string{}, "A comma-separated list of regular expressions specifying filenames to ignore") viper.SetEnvPrefix("KUBEVAL") viper.AutomaticEnv()