Skip to content

Commit

Permalink
Add support for GET/PAUSE/UNPAUSE/UNLOCK/SCHEDULE GoCD pipelines
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhilsbhat committed Mar 20, 2023
1 parent e724803 commit 69a365d
Show file tree
Hide file tree
Showing 88 changed files with 790 additions and 88 deletions.
1 change: 1 addition & 0 deletions cmd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func getGoCDCliCommands() *cobra.Command {
command.commands = append(command.commands, registerAgentCommand())
command.commands = append(command.commands, registerServerCommand())
command.commands = append(command.commands, registerPipelineGroupsCommand())
command.commands = append(command.commands, registerPipelinesCommand())

return command.prepareCommands()
}
Expand Down
18 changes: 16 additions & 2 deletions cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ func registerConfigRepoPreflightFlags(cmd *cobra.Command) {
}

const (
defaultRetryCount = 30
defaultDelay = 5 * time.Second
defaultRetryCount = 30
defaultDelay = 5 * time.Second
defaultInstanceCount = 0
)

func registerBackupFlags(cmd *cobra.Command) {
Expand All @@ -74,3 +75,16 @@ func registerBackupFlags(cmd *cobra.Command) {
cmd.PersistentFlags().DurationVarP(&delay, "delay", "", defaultDelay,
"time delay between each retries that would be made to get backup stats")
}

func registerPipelineFlags(cmd *cobra.Command) {
cmd.PersistentFlags().IntVarP(&goCDPipelineInstance, "instance", "i", defaultInstanceCount,
"instance number of a pipeline")
cmd.PersistentFlags().StringVarP(&goCDPipelineName, "name", "n", "",
"name of the pipeline present in GoCD")
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")
}
6 changes: 3 additions & 3 deletions cmd/pipeline_groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,13 +228,13 @@ func listPipelineGroupsCommand() *cobra.Command {
return err
}

var environments []string
var pipelineGroups []string

for _, environment := range response {
environments = append(environments, environment.Name)
pipelineGroups = append(pipelineGroups, environment.Name)
}

return cliRenderer.Render(strings.Join(environments, "\n"))
return cliRenderer.Render(strings.Join(pipelineGroups, "\n"))
},
}

Expand Down
277 changes: 276 additions & 1 deletion cmd/pipelines.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,284 @@
package cmd

import (
"encoding/json"
"fmt"
"strings"

"github.com/nikhilsbhat/gocd-cli/pkg/errors"
"github.com/nikhilsbhat/gocd-cli/pkg/render"
"github.com/nikhilsbhat/gocd-sdk-go"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
)

var (
goCDPipelineInstance int
goCDPipelineName string
goCDPipelineMessage string
goCDPipelinePause bool
goCDPipelineUnPause bool
)

func registerPipelinesCommand() *cobra.Command {
return &cobra.Command{}
pipelineCommand := &cobra.Command{
Use: "pipeline",
Short: "Command to operate on pipelines present in GoCD ",
Long: `Command leverages GoCD pipeline group config 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 {
if err := cmd.Usage(); err != nil {
return err
}

return nil
},
}

pipelineCommand.SetUsageTemplate(getUsageTemplate())

registerPipelineFlags(pipelineCommand)

pipelineCommand.AddCommand(getPipelinesCommand())
pipelineCommand.AddCommand(getPipelineStateCommand())
pipelineCommand.AddCommand(getPipelineInstanceCommand())
pipelineCommand.AddCommand(pauseUnpausePipelineCommand())
pipelineCommand.AddCommand(schedulePipelineCommand())
pipelineCommand.AddCommand(commentPipelineCommand())
pipelineCommand.AddCommand(listPipelinesCommand())

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

return pipelineCommand
}

func getPipelinesCommand() *cobra.Command {
getPipelinesCmd := &cobra.Command{
Use: "get-all",
Short: "Command to GET all pipelines present in GoCD [https://api.gocd.org/current/#get-feed-of-all-stages-in-a-pipeline]",
Args: cobra.NoArgs,
PreRunE: setCLIClient,
Example: `gocd-cli pipeline get-all --query "[*] | name eq sample-group"`,
RunE: func(cmd *cobra.Command, args []string) error {
response, err := client.GetPipelines()
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 getPipelinesCmd
}

func getPipelineStateCommand() *cobra.Command {
getPipelineStateCmd := &cobra.Command{
Use: "status",
Short: "Command to GET status of a specific pipeline present in GoCD [https://api.gocd.org/current/#get-pipeline-status]",
Args: cobra.RangeArgs(1, 1),
PreRunE: setCLIClient,
Example: `gocd-cli pipeline status sample-pipeline`,
RunE: func(cmd *cobra.Command, args []string) error {
response, err := client.GetPipelineState(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 getPipelineStateCmd
}

func getPipelineInstanceCommand() *cobra.Command {
getPipelineInstanceCmd := &cobra.Command{
Use: "instance",
Short: "Command to GET instance of a specific pipeline present in GoCD [https://api.gocd.org/current/#get-pipeline-instance]",
Args: cobra.RangeArgs(1, 1),
PreRunE: setCLIClient,
Example: `gocd-cli pipeline instance sample-pipeline --instance 10`,
RunE: func(cmd *cobra.Command, args []string) error {
pipelineObject := gocd.PipelineObject{
Name: args[0],
Counter: goCDPipelineInstance,
}

response, err := client.GetPipelineInstance(pipelineObject)
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 getPipelineInstanceCmd
}

func pauseUnpausePipelineCommand() *cobra.Command {
pauseUnpausePipelineCmd := &cobra.Command{
Use: "action",
Short: "Command to PAUSE/UNPAUSE a specific pipeline present in GoCD " +
"[https://api.gocd.org/current/#pause-a-pipeline,https://api.gocd.org/current/#unpause-a-pipeline]",
Args: cobra.RangeArgs(1, 1),
PreRunE: setCLIClient,
Example: `gocd-cli pipeline action sample-pipeline --pause/--un-pause`,
RunE: func(cmd *cobra.Command, args []string) error {
var action string
if goCDPipelinePause {
action = "pausing"
if err := client.PipelinePause(args[0], goCDPipelineMessage); err != nil {
return err
}
}
if goCDPipelineUnPause {
action = "unpausing"
if err := client.PipelineUnPause(args[0]); err != nil {
return err
}
}

return cliRenderer.Render(fmt.Sprintf("%s pipeline '%s' was successful", action, args[0]))
},
}

return pauseUnpausePipelineCmd
}

func schedulePipelineCommand() *cobra.Command {
schedulePipelineCmd := &cobra.Command{
Use: "schedule",
Short: "Command to SCHEDULE a specific pipeline present in GoCD [https://api.gocd.org/current/#scheduling-pipelines]",
Args: cobra.NoArgs,
PreRunE: setCLIClient,
Example: `gocd-cli pipeline schedule --name sample --from-file schedule-config.yaml`,
RunE: func(cmd *cobra.Command, args []string) error {
var schedule gocd.Schedule
object, err := readObject(cmd)
if err != nil {
return err
}

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

if err = client.SchedulePipeline(goCDPipelineName, schedule); err != nil {
return err
}

return cliRenderer.Render(fmt.Sprintf("pipeline '%s' scheduled successfully", goCDPipelineName))
},
}

return schedulePipelineCmd
}

func commentPipelineCommand() *cobra.Command {
commentOnPipelineCmd := &cobra.Command{
Use: "comment",
Short: "Command to COMMENT on a specific pipeline instance present in GoCD [https://api.gocd.org/current/#comment-on-pipeline-instance]",
Args: cobra.NoArgs,
PreRunE: setCLIClient,
Example: `gocd-cli pipeline comment --message "message to be commented"`,
RunE: func(cmd *cobra.Command, args []string) error {
pipelineObject := gocd.PipelineObject{
Name: goCDPipelineName,
Counter: goCDPipelineInstance,
Message: goCDPipelineMessage,
}

if err := client.CommentOnPipeline(pipelineObject); err != nil {
return err
}

return cliRenderer.Render(fmt.Sprintf("commented on pipeline '%s' successfully", goCDPipelineName))
},
}

return commentOnPipelineCmd
}

func listPipelinesCommand() *cobra.Command {
listPipelinesCmd := &cobra.Command{
Use: "list",
Short: "Command to LIST all the pipelines present in GoCD [https://api.gocd.org/current/#get-feed-of-all-stages-in-a-pipeline]",
Args: cobra.NoArgs,
PreRunE: setCLIClient,
Example: `gocd-cli pipeline list`,
RunE: func(cmd *cobra.Command, args []string) error {
response, err := client.GetPipelines()
if err != nil {
return err
}

var pipelines []string

for _, pipeline := range response.Pipeline {
pipelineName, err := gocd.GetPipelineName(pipeline.Href)
if err != nil {
cliLogger.Errorf("fetching pipeline name from pipline url erored with:, %v", err)
} else {
pipelines = append(pipelines, pipelineName)
}
}

return cliRenderer.Render(strings.Join(pipelines, "\n"))
},
}

return listPipelinesCmd
}
3 changes: 2 additions & 1 deletion docs/doc/gocd-cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@ 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 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 17-Mar-2023
###### Auto generated by spf13/cobra on 20-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 17-Mar-2023
###### Auto generated by spf13/cobra on 20-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 17-Mar-2023
###### Auto generated by spf13/cobra on 20-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 17-Mar-2023
###### Auto generated by spf13/cobra on 20-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 17-Mar-2023
###### Auto generated by spf13/cobra on 20-Mar-2023
2 changes: 1 addition & 1 deletion docs/doc/gocd-cli_agents_job-history.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ gocd-cli agents job-history [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 17-Mar-2023
###### Auto generated by spf13/cobra on 20-Mar-2023
2 changes: 1 addition & 1 deletion docs/doc/gocd-cli_agents_kill-task.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ gocd-cli agents kill-task [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 17-Mar-2023
###### Auto generated by spf13/cobra on 20-Mar-2023
2 changes: 1 addition & 1 deletion docs/doc/gocd-cli_agents_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ gocd-cli agents list [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 17-Mar-2023
###### Auto generated by spf13/cobra on 20-Mar-2023
Loading

0 comments on commit 69a365d

Please sign in to comment.