diff --git a/semvalidator/main.go b/semvalidator/main.go index daff3f35..805bcbe5 100644 --- a/semvalidator/main.go +++ b/semvalidator/main.go @@ -28,19 +28,21 @@ import ( ) var ( - dir string - skipDir string - file string - org string - orgURL string - token string - debug bool + dir string + skipDir string + skipFile string + file string + org string + orgURL string + token string + debug bool ) func init() { flag.StringVar(&dir, "dirs", "", "comma separated list of directories to search for Semaphore pipeline files") - flag.StringVar(&skipDir, "skip-dirs", "", "comma separated list of directories to skip when searching for Semaphore pipeline files") + flag.StringVar(&skipDir, "skip-dirs", "", "comma separated list of directories to skip") flag.StringVar(&file, "files", "", "comma separated list of Semaphore pipeline files") + flag.StringVar(&skipFile, "skip-files", "", "comma separated list of files to skip") flag.StringVar(&org, "org", "", "Semaphore organization") flag.StringVar(&orgURL, "org-url", "", "Semaphore organization URL") flag.StringVar(&token, "token", "", "Semaphore API token") @@ -52,14 +54,26 @@ func inSkipDirs(path string, skipDirs []string) bool { return false } for _, skipDir := range skipDirs { - if strings.HasSuffix(path, skipDir) { + if path == skipDir || strings.Contains(path, skipDir+"/") { return true } } return false } -func getPipelineYAMLFiles(dir string, skipDirs []string) ([]string, error) { +func inSkipFiles(path string, skipFiles []string) bool { + if len(skipFiles) == 0 { + return false + } + for _, skipFile := range skipFiles { + if path == skipFile { + return true + } + } + return false +} + +func getPipelineYAMLFiles(dir string, skipDirs, skipFiles []string) ([]string, error) { var files []string err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { if err != nil { @@ -68,10 +82,10 @@ func getPipelineYAMLFiles(dir string, skipDirs []string) ([]string, error) { // Skip the YAML .semaphore/semaphore.yml.d directory // as it contains building blocks which are not full pipeline definitions // The resulting pipeline will be validated as part of semaphore.yml and semaphore-scheduled-builds.yml - if info.IsDir() && !inSkipDirs(path, skipDirs) { + if info.IsDir() && !inSkipDirs(info.Name(), skipDirs) { return filepath.SkipDir } - if !info.IsDir() && (filepath.Ext(path) == ".yml" || filepath.Ext(path) == ".yaml") { + if !info.IsDir() && !inSkipFiles(path, skipFiles) && (filepath.Ext(path) == ".yml" || filepath.Ext(path) == ".yaml") { files = append(files, path) } return nil @@ -141,13 +155,18 @@ func main() { // Get YAML files var yamlFiles []string if file != "" { - yamlFiles = strings.Split(file, ",") + files := strings.Split(file, ",") + for _, f := range files { + if !inSkipDirs(f, strings.Split(skipDir, ",")) && !inSkipFiles(f, strings.Split(skipFile, ",")) { + yamlFiles = append(yamlFiles, f) + } + } } if dir != "" { semaphoreDirs := strings.Split(dir, ",") logrus.WithField("semaphoreDirs", semaphoreDirs).Debug("looking for pipeline YAML files") for _, semaphoreDir := range semaphoreDirs { - files, err := getPipelineYAMLFiles(semaphoreDir, strings.Split(skipDir, ",")) + files, err := getPipelineYAMLFiles(semaphoreDir, strings.Split(skipDir, ","), strings.Split(skipFile, ",")) if err != nil { logrus.WithError(err).Errorf("failed to get YAML files in %s", semaphoreDir) continue