Skip to content

Commit

Permalink
Add support to GET/CREATE/UPDATE/DELETE pipeline config
Browse files Browse the repository at this point in the history
Add support to get list of scheduled jobs

Reorganize the flags for subcommand pipeline
  • Loading branch information
nikhilsbhat committed Mar 21, 2023
1 parent 9c95093 commit 8793b01
Show file tree
Hide file tree
Showing 99 changed files with 754 additions and 153 deletions.
1 change: 1 addition & 0 deletions cmd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func getGoCDCliCommands() *cobra.Command {
command.commands = append(command.commands, registerPipelineGroupsCommand())
command.commands = append(command.commands, registerPipelinesCommand())
command.commands = append(command.commands, registerMaintenanceCommand())
command.commands = append(command.commands, registerJobsCommand())

return command.prepareCommands()
}
Expand Down
6 changes: 6 additions & 0 deletions cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,18 @@ func registerPipelineFlags(cmd *cobra.Command) {
"instance number of a pipeline")
cmd.PersistentFlags().StringVarP(&goCDPipelineName, "name", "n", "",
"name of the pipeline present in GoCD")
cmd.PersistentFlags().StringVarP(&goCDPipelineETAG, "etag", "", "",
"etag used to identify the pipeline config. If you don't have one get it by using pipeline get command")
cmd.PersistentFlags().StringVarP(&goCDPipelineMessage, "message", "m", "",
"message to be passed while pausing/unpausing or commenting on pipeline present in GoCD")
cmd.PersistentFlags().BoolVarP(&goCDPipelinePause, "pause", "", false,
"enable to pause a pipeline")
cmd.PersistentFlags().BoolVarP(&goCDPipelineUnPause, "un-pause", "", false,
"disable to pause a pipeline")
cmd.PersistentFlags().BoolVarP(&goCDPausePipelineAtStart, "pause-at-start", "", false,
"enabling this will create the pipeline in the paused state")
cmd.PersistentFlags().StringVarP(&goCDPipelineTemplateName, "template-name", "", "",
"name of the template to which the pipeline has to be extracted")
}

func registerMaintenanceFlags(cmd *cobra.Command) {
Expand Down
54 changes: 54 additions & 0 deletions cmd/jobs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package cmd

import (
"github.com/spf13/cobra"
)

func registerJobsCommand() *cobra.Command {
jobsCommand := &cobra.Command{
Use: "job",
Short: "Command to operate on jobs present in GoCD",
Long: `Command leverages GoCD job apis'
[https://api.gocd.org/current/#scheduled-jobs] to
GET/SCHEDULE jobs of specific pipelines present GoCD`,
RunE: func(cmd *cobra.Command, args []string) error {
if err := cmd.Usage(); err != nil {
return err
}

return nil
},
}

jobsCommand.SetUsageTemplate(getUsageTemplate())

jobsCommand.AddCommand(getScheduledJobsCommand())

for _, command := range jobsCommand.Commands() {
command.SilenceUsage = true
}

return jobsCommand
}

func getScheduledJobsCommand() *cobra.Command {
getScheduledJobsCmd := &cobra.Command{
Use: "scheduled",
Short: "Command to GET a list of scheduled jobs in GoCD [https://api.gocd.org/current/#scheduled-jobs]",
Args: cobra.NoArgs,
PreRunE: setCLIClient,
Example: `gocd-cli job scheduled"`,
RunE: func(cmd *cobra.Command, args []string) error {
response, err := client.GetScheduledJobs()
if err != nil {
return err
}

return cliRenderer.Render(response)
},
}

registerPipelineFlags(getScheduledJobsCmd)

return getScheduledJobsCmd
}
210 changes: 201 additions & 9 deletions cmd/pipelines.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,21 @@ import (
)

var (
goCDPipelineInstance int
goCDPipelineName string
goCDPipelineMessage string
goCDPipelinePause bool
goCDPipelineUnPause bool
goCDPipelineInstance int
goCDPipelineName string
goCDPipelineMessage string
goCDPipelineETAG string
goCDPipelineTemplateName string
goCDPausePipelineAtStart bool
goCDPipelinePause bool
goCDPipelineUnPause bool
)

func registerPipelinesCommand() *cobra.Command {
pipelineCommand := &cobra.Command{
Use: "pipeline",
Short: "Command to operate on pipelines present in GoCD ",
Long: `Command leverages GoCD pipeline group config apis'
Short: "Command to operate on pipelines present in GoCD",
Long: `Command leverages GoCD pipeline apis'
[https://api.gocd.org/current/#pipeline-instances, https://api.gocd.org/current/#pipeline-config, https://api.gocd.org/current/#pipelines] to
GET/PAUSE/UNPAUSE/UNLOCK/SCHEDULE and comment on a GoCD pipeline`,
RunE: func(cmd *cobra.Command, args []string) error {
Expand All @@ -38,14 +41,17 @@ GET/PAUSE/UNPAUSE/UNLOCK/SCHEDULE and comment on a GoCD pipeline`,

pipelineCommand.SetUsageTemplate(getUsageTemplate())

registerPipelineFlags(pipelineCommand)

pipelineCommand.AddCommand(getPipelinesCommand())
pipelineCommand.AddCommand(getPipelineCommand())
pipelineCommand.AddCommand(createPipelineCommand())
pipelineCommand.AddCommand(updatePipelineCommand())
pipelineCommand.AddCommand(deletePipelineCommand())
pipelineCommand.AddCommand(getPipelineStateCommand())
pipelineCommand.AddCommand(getPipelineInstanceCommand())
pipelineCommand.AddCommand(pauseUnpausePipelineCommand())
pipelineCommand.AddCommand(schedulePipelineCommand())
pipelineCommand.AddCommand(commentPipelineCommand())
pipelineCommand.AddCommand(pipelineExtractTemplateCommand())
pipelineCommand.AddCommand(listPipelinesCommand())

for _, command := range pipelineCommand.Commands() {
Expand Down Expand Up @@ -88,6 +94,162 @@ func getPipelinesCommand() *cobra.Command {
return getPipelinesCmd
}

func getPipelineCommand() *cobra.Command {
getPipelineCmd := &cobra.Command{
Use: "get",
Short: "Command to GET pipeline config of a specified pipeline present in GoCD [https://api.gocd.org/current/#get-pipeline-config",
Args: cobra.RangeArgs(1, 1),
PreRunE: setCLIClient,
Example: `gocd-cli pipeline get sample-pipeline --query "[*] | name eq sample-group"`,
RunE: func(cmd *cobra.Command, args []string) error {
response, err := client.GetPipelineConfig(args[0])
if err != nil {
return err
}

if len(jsonQuery) != 0 {
cliLogger.Debugf(queryEnabledMessage, jsonQuery)

baseQuery, err := render.SetQuery(response, jsonQuery)
if err != nil {
return err
}

cliLogger.Debugf(baseQuery.Print())

return cliRenderer.Render(baseQuery.RunQuery())
}

return cliRenderer.Render(response)
},
}

return getPipelineCmd
}

func createPipelineCommand() *cobra.Command {
createPipelineGroupCmd := &cobra.Command{
Use: "create",
Short: "Command to CREATE the pipeline with all specified configuration [https://api.gocd.org/current/#create-a-pipeline]",
Args: cobra.NoArgs,
PreRunE: setCLIClient,
Example: `gocd-cli pipeline create sample-pipeline --from-file sample-pipeline.yaml --log-level debug
// the inputs can be passed either from file using '--from-file' flag or entire content as argument to command`,
RunE: func(cmd *cobra.Command, args []string) error {
var pipeline map[string]interface{}
object, err := readObject(cmd)
if err != nil {
return err
}

switch objType := object.CheckFileType(cliLogger); objType {
case render.FileTypeYAML:
if err = yaml.Unmarshal([]byte(object), &pipeline); err != nil {
return err
}
case render.FileTypeJSON:
if err = json.Unmarshal([]byte(object), &pipeline); err != nil {
return err
}
default:
return &errors.UnknownObjectTypeError{Name: objType}
}

pipelineConfig := gocd.PipelineConfig{
Config: pipeline,
}

if goCDPausePipelineAtStart {
pipelineConfig.PausePipeline = true
}

if len(goCDPipelineMessage) != 0 {
pipelineConfig.PauseReason = goCDPipelineMessage
}

if err = client.CreatePipeline(pipelineConfig); err != nil {
return err
}

return cliRenderer.Render(fmt.Sprintf("pipeline %s created successfully", pipeline["name"]))
},
}

registerPipelineFlags(createPipelineGroupCmd)

return createPipelineGroupCmd
}

func updatePipelineCommand() *cobra.Command {
updatePipelineGroupCmd := &cobra.Command{
Use: "update",
Short: "Command to UPDATE the pipeline config with the latest specified configuration [https://api.gocd.org/current/#edit-pipeline-config]",
Args: cobra.NoArgs,
PreRunE: setCLIClient,
Example: `gocd-cli pipeline update sample-movies --from-file sample-movies.yaml --log-level debug
// the inputs can be passed either from file using '--from-file' flag or entire content as argument to command`,
RunE: func(cmd *cobra.Command, args []string) error {
var pipeline map[string]interface{}
object, err := readObject(cmd)
if err != nil {
return err
}

switch objType := object.CheckFileType(cliLogger); objType {
case render.FileTypeYAML:
if err = yaml.Unmarshal([]byte(object), &pipeline); err != nil {
return err
}
case render.FileTypeJSON:
if err = json.Unmarshal([]byte(object), &pipeline); err != nil {
return err
}
default:
return &errors.UnknownObjectTypeError{Name: objType}
}

pipelineConfig := gocd.PipelineConfig{
ETAG: goCDPipelineETAG,
Config: pipeline,
}

response, err := client.UpdatePipelineConfig(pipelineConfig)
if err != nil {
return err
}

if err = cliRenderer.Render(fmt.Sprintf("pipeline %s updated successfully", pipeline["name"])); err != nil {
return err
}

return cliRenderer.Render(response)
},
}

registerPipelineFlags(updatePipelineGroupCmd)

return updatePipelineGroupCmd
}

func deletePipelineCommand() *cobra.Command {
deletePipelineCmd := &cobra.Command{
Use: "delete",
Short: "Command to DELETE the specified pipeline from GoCD [https://api.gocd.org/current/#delete-a-pipeline]",
Args: cobra.RangeArgs(1, 1),
PreRunE: setCLIClient,
Example: `gocd-cli pipeline delete movies`,
RunE: func(cmd *cobra.Command, args []string) error {
if err := client.DeletePipeline(args[0]); err != nil {
return err
}

return cliRenderer.Render(fmt.Sprintf("pipeline deleted: %s", args[0]))
},
}

return deletePipelineCmd
}

func getPipelineStateCommand() *cobra.Command {
getPipelineStateCmd := &cobra.Command{
Use: "status",
Expand Down Expand Up @@ -156,6 +318,8 @@ func getPipelineInstanceCommand() *cobra.Command {
},
}

registerPipelineFlags(getPipelineInstanceCmd)

return getPipelineInstanceCmd
}

Expand Down Expand Up @@ -186,6 +350,8 @@ func pauseUnpausePipelineCommand() *cobra.Command {
},
}

registerPipelineFlags(pauseUnpausePipelineCmd)

return pauseUnpausePipelineCmd
}

Expand Down Expand Up @@ -224,6 +390,8 @@ func schedulePipelineCommand() *cobra.Command {
},
}

registerPipelineFlags(schedulePipelineCmd)

return schedulePipelineCmd
}

Expand All @@ -249,9 +417,33 @@ func commentPipelineCommand() *cobra.Command {
},
}

registerPipelineFlags(commentOnPipelineCmd)

return commentOnPipelineCmd
}

func pipelineExtractTemplateCommand() *cobra.Command {
extractTemplatePipelineCmd := &cobra.Command{
Use: "template",
Short: "Command to EXTRACT template from specific pipeline instance present in GoCD [https://api.gocd.org/current/#extract-template-from-pipeline]",
Args: cobra.RangeArgs(1, 1),
PreRunE: setCLIClient,
Example: `gocd-cli pipeline template --name sample-pipeline --template-name sample-template`,
RunE: func(cmd *cobra.Command, args []string) error {
response, err := client.ExtractTemplatePipeline(args[0], goCDPipelineTemplateName)
if err != nil {
return err
}

return cliRenderer.Render(response)
},
}

registerPipelineFlags(extractTemplatePipelineCmd)

return extractTemplatePipelineCmd
}

func listPipelinesCommand() *cobra.Command {
listPipelinesCmd := &cobra.Command{
Use: "list",
Expand Down
5 changes: 3 additions & 2 deletions docs/doc/gocd-cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,13 @@ gocd-cli [flags]
* [gocd-cli elastic-agent-profile](gocd-cli_elastic-agent-profile.md) - Command to operate on elastic-agent-profile in GoCD [https://api.gocd.org/current/#elastic-agent-profiles]
* [gocd-cli encryption](gocd-cli_encryption.md) - Command to encrypt/decrypt plain text value [https://api.gocd.org/current/#encryption]
* [gocd-cli environment](gocd-cli_environment.md) - Command to operate on environments present in GoCD [https://api.gocd.org/current/#environment-config]
* [gocd-cli job](gocd-cli_job.md) - Command to operate on jobs present in GoCD
* [gocd-cli maintenance](gocd-cli_maintenance.md) - Command to operate on maintenance modes in GoCD [https://api.gocd.org/current/#maintenance-mode]
* [gocd-cli pipeline](gocd-cli_pipeline.md) - Command to operate on pipelines present in GoCD
* [gocd-cli pipeline](gocd-cli_pipeline.md) - Command to operate on pipelines present in GoCD
* [gocd-cli pipeline-group](gocd-cli_pipeline-group.md) - Command to operate on pipeline groups present in GoCD [https://api.gocd.org/current/#pipeline-group-config]
* [gocd-cli plugin](gocd-cli_plugin.md) - Command to operate on plugins present in GoCD
* [gocd-cli server](gocd-cli_server.md) - Command to operate on GoCD server health status
* [gocd-cli user](gocd-cli_user.md) - Command to operate on users in GoCD [https://api.gocd.org/current/#users]
* [gocd-cli version](gocd-cli_version.md) - Command to fetch the version of gocd-cli installed

###### Auto generated by spf13/cobra on 20-Mar-2023
###### Auto generated by spf13/cobra on 21-Mar-2023
2 changes: 1 addition & 1 deletion docs/doc/gocd-cli_agents.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@ gocd-cli agents [flags]
* [gocd-cli agents list](gocd-cli_agents_list.md) - Command to LIST all the agents present in GoCD [https://api.gocd.org/current/#get-all-agents]
* [gocd-cli agents update](gocd-cli_agents_update.md) - Command to UPDATE an agent with all specified configuration [https://api.gocd.org/current/#update-an-agent]

###### Auto generated by spf13/cobra on 20-Mar-2023
###### Auto generated by spf13/cobra on 21-Mar-2023
2 changes: 1 addition & 1 deletion docs/doc/gocd-cli_agents_delete.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ gocd-cli agents delete [flags]

* [gocd-cli agents](gocd-cli_agents.md) - Command to operate on agents present in GoCD [https://api.gocd.org/current/#agents]

###### Auto generated by spf13/cobra on 20-Mar-2023
###### Auto generated by spf13/cobra on 21-Mar-2023
2 changes: 1 addition & 1 deletion docs/doc/gocd-cli_agents_get-all.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ gocd-cli agents get-all [flags]

* [gocd-cli agents](gocd-cli_agents.md) - Command to operate on agents present in GoCD [https://api.gocd.org/current/#agents]

###### Auto generated by spf13/cobra on 20-Mar-2023
###### Auto generated by spf13/cobra on 21-Mar-2023
2 changes: 1 addition & 1 deletion docs/doc/gocd-cli_agents_get.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ gocd-cli agents get [flags]

* [gocd-cli agents](gocd-cli_agents.md) - Command to operate on agents present in GoCD [https://api.gocd.org/current/#agents]

###### Auto generated by spf13/cobra on 20-Mar-2023
###### Auto generated by spf13/cobra on 21-Mar-2023
Loading

0 comments on commit 8793b01

Please sign in to comment.