Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[semvalidator] update skip dirs usage and add skipping files #596

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 33 additions & 14 deletions semvalidator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down