Skip to content

Commit

Permalink
prowgen: operate on sub-directories
Browse files Browse the repository at this point in the history
  • Loading branch information
bbguimaraes committed Jul 4, 2019
1 parent 0061a12 commit 0fd3498
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
23 changes: 16 additions & 7 deletions cmd/ci-operator-prowgen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -591,8 +591,8 @@ func prune(jobConfig *prowconfig.JobConfig) *prowconfig.JobConfig {
return &pruned
}

func pruneStaleJobs(jobDir string) error {
if err := jc.OperateOnJobConfigDir(jobDir, func(jobConfig *prowconfig.JobConfig, info *jc.Info) error {
func pruneStaleJobs(jobDir, subDir string) error {
if err := jc.OperateOnJobConfigSubdir(jobDir, subDir, func(jobConfig *prowconfig.JobConfig, info *jc.Info) error {
if info.Type == "periodics" {
return nil
}
Expand Down Expand Up @@ -636,13 +636,22 @@ func main() {
label = jc.Generated
}

if err := config.OperateOnCIOperatorConfigDir(opt.fromDir, generateJobsToDir(opt.toDir, label)); err != nil {
fields := logrus.Fields{"target": opt.toDir, "source": opt.fromDir}
logrus.WithError(err).WithFields(fields).Fatal("Failed to generate jobs")
args := flagSet.Args()
if len(args) == 0 {
args = append(args, "")
}
genJobs := generateJobsToDir(opt.toDir, label)
for _, subDir := range args {
if err := config.OperateOnCIOperatorConfigSubdir(opt.fromDir, subDir, genJobs); err != nil {
fields := logrus.Fields{"target": opt.toDir, "source": opt.fromDir, "subdir": subDir}
logrus.WithError(err).WithFields(fields).Fatal("Failed to generate jobs")
}
}
if opt.prune {
if err := pruneStaleJobs(opt.toDir); err != nil {
logrus.WithError(err).Fatal("Failed to prune stale generated jobs")
for _, subDir := range args {
if err := pruneStaleJobs(opt.toDir, subDir); err != nil {
logrus.WithError(err).Fatal("Failed to prune stale generated jobs")
}
}
}
}
6 changes: 5 additions & 1 deletion pkg/config/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,11 @@ func OperateOnCIOperatorConfig(path string, callback func(*cioperatorapi.Release
// OperateOnCIOperatorConfigDir runs the callback on all CI Operator
// configuration files found while walking the directory provided
func OperateOnCIOperatorConfigDir(configDir string, callback func(*cioperatorapi.ReleaseBuildConfiguration, *Info) error) error {
return filepath.Walk(configDir, func(path string, info os.FileInfo, err error) error {
return OperateOnCIOperatorConfigSubdir(configDir, "", callback)
}

func OperateOnCIOperatorConfigSubdir(configDir, subDir string, callback func(*cioperatorapi.ReleaseBuildConfiguration, *Info) error) error {
return filepath.Walk(filepath.Join(configDir, subDir), func(path string, info os.FileInfo, err error) error {
if err != nil {
logrus.WithField("source-file", path).WithError(err).Error("Failed to walk CI Operator configuration dir")
return err
Expand Down
7 changes: 5 additions & 2 deletions pkg/jobconfig/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,16 @@ func extractInfoFromPath(configFilePath string) (*Info, error) {
}

func OperateOnJobConfigDir(configDir string, callback func(*prowconfig.JobConfig, *Info) error) error {
if err := filepath.Walk(configDir, func(path string, info os.FileInfo, err error) error {
return OperateOnJobConfigSubdir(configDir, "", callback)
}

func OperateOnJobConfigSubdir(configDir, subDir string, callback func(*prowconfig.JobConfig, *Info) error) error {
if err := filepath.Walk(filepath.Join(configDir, subDir), func(path string, info os.FileInfo, err error) error {
logger := logrus.WithField("source-file", path)
if err != nil {
logger.WithError(err).Error("Failed to walk file/directory")
return nil
}

if !info.IsDir() && filepath.Ext(path) == ".yaml" {
var configPart *prowconfig.JobConfig
if configPart, err = readFromFile(path); err != nil {
Expand Down

0 comments on commit 0fd3498

Please sign in to comment.