Skip to content

Commit

Permalink
Add support for command 'pipeline find' to identify pipeline files in…
Browse files Browse the repository at this point in the history
… a given directory
  • Loading branch information
nikhilsbhat committed Nov 22, 2023
1 parent 11defd5 commit a7c4950
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions cmd/pipelines.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -35,6 +36,8 @@ var (
numberOfDays time.Duration
)

var defaultGoCDPipelinePatterns = []string{"*.gocd.yaml", "*.gocd.json", "*.gocd.groovy"}

type PipelineVSM struct {
Pipeline string `json:"pipeline,omitempty" yaml:"pipeline,omitempty"`
DownstreamPipelines []string `json:"downstream_pipelines,omitempty" yaml:"downstream_pipelines,omitempty"`
Expand Down Expand Up @@ -74,6 +77,7 @@ GET/PAUSE/UNPAUSE/UNLOCK/SCHEDULE and comment on a GoCD pipeline`,
pipelineCommand.AddCommand(exportPipelineToConfigRepoFormatCommand())
pipelineCommand.AddCommand(getPipelineVSMCommand())
pipelineCommand.AddCommand(getPipelineMapping())
pipelineCommand.AddCommand(getPipelineFilesCommand())

for _, command := range pipelineCommand.Commands() {
command.SilenceUsage = true
Expand Down Expand Up @@ -1032,6 +1036,58 @@ func getPipelineMapping() *cobra.Command {
return getPipelineMappingCmd
}

func getPipelineFilesCommand() *cobra.Command {
var (
goCDPipelinesPath string
goCDPipelinesPatterns []string
absPath bool
)

findPipelineCmd := &cobra.Command{
Use: "find",
Short: "Command to find all GoCD pipeline files present in a directory (it recursively finds for pipeline files in all sub-directory)",
Args: cobra.NoArgs,
PreRunE: setCLIClient,
Example: `gocd-cli pipeline find --path /path/to/pipelines --pattern *.gocd.yaml --pattern *.gocd.json`,
RunE: func(cmd *cobra.Command, args []string) error {
cliLogger.Debugf("searching GoCD pipelines under '%s'", goCDPipelinesPath)

return filepath.Walk(goCDPipelinesPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
for _, goCDPipelinesPattern := range goCDPipelinesPatterns {
match, err := filepath.Match(goCDPipelinesPattern, info.Name())
if err != nil {
cliLogger.Errorf("matching GoCD pipeline file errored with '%s'", err)
}

if match {
if !absPath {
fmt.Printf("%s\n", info.Name())

continue
}

fmt.Printf("%s\n", path)
}
}

return nil
})
},
}

findPipelineCmd.PersistentFlags().StringVarP(&goCDPipelinesPath, "path", "f", "",
"path to search for all GoCD pipeline files")
findPipelineCmd.PersistentFlags().StringSliceVarP(&goCDPipelinesPatterns, "pattern", "", defaultGoCDPipelinePatterns,
"list of patterns to match while searching for all GoCD pipeline files")
findPipelineCmd.PersistentFlags().BoolVarP(&absPath, "absolute-path", "a", false,
"when enabled prints absolute path of the pipelines")

return findPipelineCmd
}

func findDownStreamPipelines(pipelineName string, resp gocd.VSM) []string {
newParents := []string{pipelineName}

Expand Down

0 comments on commit a7c4950

Please sign in to comment.