From a7c49502ac423ee322de6501668b2374efb56935 Mon Sep 17 00:00:00 2001 From: nikhilsbhat Date: Wed, 22 Nov 2023 10:18:36 +0530 Subject: [PATCH] Add support for command 'pipeline find' to identify pipeline files in a given directory --- cmd/pipelines.go | 56 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/cmd/pipelines.go b/cmd/pipelines.go index 57ccdc6..47430c3 100644 --- a/cmd/pipelines.go +++ b/cmd/pipelines.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "os" + "path/filepath" "strconv" "strings" "time" @@ -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"` @@ -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 @@ -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}