From 77184c2ab105293a99fe13ac120050636d7dfd6f Mon Sep 17 00:00:00 2001 From: tuti Date: Wed, 16 Oct 2024 16:07:58 -0700 Subject: [PATCH 1/2] use skip dir to filter semaphore files --- semvalidator/main.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/semvalidator/main.go b/semvalidator/main.go index daff3f3..0f7ee4f 100644 --- a/semvalidator/main.go +++ b/semvalidator/main.go @@ -39,7 +39,7 @@ var ( 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(&org, "org", "", "Semaphore organization") flag.StringVar(&orgURL, "org-url", "", "Semaphore organization URL") @@ -52,7 +52,7 @@ 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 } } @@ -68,7 +68,7 @@ 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") { @@ -141,7 +141,12 @@ 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, ",")) { + yamlFiles = append(yamlFiles, f) + } + } } if dir != "" { semaphoreDirs := strings.Split(dir, ",") From aa7b79a627a5b02c5b1d772cdd6b5e7a9688d288 Mon Sep 17 00:00:00 2001 From: tuti Date: Wed, 16 Oct 2024 17:00:41 -0700 Subject: [PATCH 2/2] add skip-files flag to allow skipping specific files --- semvalidator/main.go | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/semvalidator/main.go b/semvalidator/main.go index 0f7ee4f..805bcbe 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") 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") @@ -59,7 +61,19 @@ func inSkipDirs(path string, skipDirs []string) bool { 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 { @@ -71,7 +85,7 @@ func getPipelineYAMLFiles(dir string, skipDirs []string) ([]string, error) { 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 @@ -143,7 +157,7 @@ func main() { if file != "" { files := strings.Split(file, ",") for _, f := range files { - if !inSkipDirs(f, strings.Split(skipDir, ",")) { + if !inSkipDirs(f, strings.Split(skipDir, ",")) && !inSkipFiles(f, strings.Split(skipFile, ",")) { yamlFiles = append(yamlFiles, f) } } @@ -152,7 +166,7 @@ func main() { 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