Skip to content

Commit

Permalink
Add support for GET/ENABLE/DISABLE maintenance mode in GoC.
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhilsbhat committed Mar 20, 2023
1 parent 69a365d commit 9c95093
Show file tree
Hide file tree
Showing 10 changed files with 256 additions and 5 deletions.
1 change: 1 addition & 0 deletions cmd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func getGoCDCliCommands() *cobra.Command {
command.commands = append(command.commands, registerServerCommand())
command.commands = append(command.commands, registerPipelineGroupsCommand())
command.commands = append(command.commands, registerPipelinesCommand())
command.commands = append(command.commands, registerMaintenanceCommand())

return command.prepareCommands()
}
Expand Down
7 changes: 7 additions & 0 deletions cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,10 @@ func registerPipelineFlags(cmd *cobra.Command) {
cmd.PersistentFlags().BoolVarP(&goCDPipelineUnPause, "un-pause", "", false,
"disable to pause a pipeline")
}

func registerMaintenanceFlags(cmd *cobra.Command) {
cmd.PersistentFlags().BoolVarP(&goCDEnableMaintenance, "enable", "", false,
"set this to enable maintenance mode in GoCD")
cmd.PersistentFlags().BoolVarP(&goCDDisableMaintenance, "disable", "", false,
"set this to disable maintenance mode in GoCD")
}
98 changes: 97 additions & 1 deletion cmd/maintenance.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,105 @@
package cmd

import (
"fmt"

"github.com/nikhilsbhat/gocd-cli/pkg/render"
"github.com/spf13/cobra"
)

var (
goCDEnableMaintenance bool
goCDDisableMaintenance bool
)

func registerMaintenanceCommand() *cobra.Command {
return &cobra.Command{}
maintenanceCommand := &cobra.Command{
Use: "maintenance",
Short: "Command to operate on maintenance modes in GoCD [https://api.gocd.org/current/#maintenance-mode]",
Long: `Command leverages GoCD environments apis' [https://api.gocd.org/current/#maintenance-mode] to
ENABLE/DISABLE/GET maintenance mode information from GoCD`,
RunE: func(cmd *cobra.Command, args []string) error {
if err := cmd.Usage(); err != nil {
return err
}

return nil
},
}

maintenanceCommand.SetUsageTemplate(getUsageTemplate())

registerMaintenanceFlags(maintenanceCommand)

maintenanceCommand.AddCommand(enableOrDisableMaintenanceCommand())
maintenanceCommand.AddCommand(getMaintenanceCommand())

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

return maintenanceCommand
}

func getMaintenanceCommand() *cobra.Command {
getMaintenanceCmd := &cobra.Command{
Use: "get",
Short: "Command to GET a maintenance mode information from GoCD [https://api.gocd.org/current/#get-maintenance-mode-info]",
Args: cobra.NoArgs,
PreRunE: setCLIClient,
Example: `gocd-cli maintenance get`,
RunE: func(cmd *cobra.Command, args []string) error {
response, err := client.GetMaintenanceModeInfo()
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 getMaintenanceCmd
}

func enableOrDisableMaintenanceCommand() *cobra.Command {
enableDisableMaintenanceModeCmd := &cobra.Command{
Use: "action",
Short: `Command to ENABLE/DISABLE maintenance mode in GoCD,
[https://api.gocd.org/current/#enable-maintenance-mode,https://api.gocd.org/current/#disable-maintenance-mode]`,
Args: cobra.NoArgs,
PreRunE: setCLIClient,
Example: `gocd-cli maintenance --enable/--disable`,
RunE: func(cmd *cobra.Command, args []string) error {
var action string
if goCDEnableMaintenance {
action = "enabling"
if err := client.EnableMaintenanceMode(); err != nil {
return err
}
}
if goCDDisableMaintenance {
action = "disabling"
if err := client.DisableMaintenanceMode(); err != nil {
return err
}
}

return cliRenderer.Render(fmt.Sprintf("%s maintenance mode was successful", action))
},
}

return enableDisableMaintenanceModeCmd
}
4 changes: 2 additions & 2 deletions cmd/pipelines.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ func getPipelineInstanceCommand() *cobra.Command {
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]",
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`,
Expand Down
1 change: 1 addition & 0 deletions docs/doc/gocd-cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ 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 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-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
Expand Down
49 changes: 49 additions & 0 deletions docs/doc/gocd-cli_maintenance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
## gocd-cli maintenance

Command to operate on maintenance modes in GoCD [https://api.gocd.org/current/#maintenance-mode]

### Synopsis

Command leverages GoCD environments apis' [https://api.gocd.org/current/#maintenance-mode] to
ENABLE/DISABLE/GET maintenance mode information from GoCD

```
gocd-cli maintenance [flags]
```

### Options

```
--disable set this to disable maintenance mode in GoCD
--enable set this to enable maintenance mode in GoCD
-h, --help help for maintenance
```

### Options inherited from parent commands

```
-t, --auth-token string token to authenticate with GoCD server, should not be co-used with basic auth (username/password)
--ca-file-path string path to file containing CA cert used to authenticate GoCD server, if you have one
--from-file string file containing configurations of objects that needs to be created in GoCD, config-repo/pipeline-group/environment and etc.
--json enable this to Render output in JSON format
-l, --log-level string log level for gocd cli, log levels supported by [https://github.com/sirupsen/logrus] will work (default "info")
--no-color enable this to Render output in YAML format
-p, --password string password to authenticate with GoCD server
-q, --query string query to filter the results, ex: '.material.attributes.type | id eq git'. this uses library gojsonq beneath
more queries can be found here https://github.com/thedevsaddam/gojsonq/wiki/Queries
--save-config enable this to locally save auth configs used to connect GoCD server (path: $HOME/.gocd/auth_config.yaml)
--server-url string GoCD server URL base path (default "http://localhost:8153/go")
--skip-cache-config if enabled locally save auth configs would not be used to authenticate GoCD server (path: $HOME/.gocd/auth_config.yaml)
--to-file string file to which the output needs to be written to (this works only if --yaml or --json is enabled)
-u, --username string username to authenticate with GoCD server
--yaml enable this to Render output in YAML format
```

### SEE ALSO

* [gocd-cli](gocd-cli.md) - Command line interface for GoCD
* [gocd-cli maintenance action](gocd-cli_maintenance_action.md) - Command to ENABLE/DISABLE maintenance mode in GoCD,
[https://api.gocd.org/current/#enable-maintenance-mode,https://api.gocd.org/current/#disable-maintenance-mode]
* [gocd-cli maintenance get](gocd-cli_maintenance_get.md) - Command to GET a maintenance mode information from GoCD [https://api.gocd.org/current/#get-maintenance-mode-info]

###### Auto generated by spf13/cobra on 20-Mar-2023
48 changes: 48 additions & 0 deletions docs/doc/gocd-cli_maintenance_action.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
## gocd-cli maintenance action

Command to ENABLE/DISABLE maintenance mode in GoCD,
[https://api.gocd.org/current/#enable-maintenance-mode,https://api.gocd.org/current/#disable-maintenance-mode]

```
gocd-cli maintenance action [flags]
```

### Examples

```
gocd-cli maintenance --enable/--disable
```

### Options

```
-h, --help help for action
```

### Options inherited from parent commands

```
-t, --auth-token string token to authenticate with GoCD server, should not be co-used with basic auth (username/password)
--ca-file-path string path to file containing CA cert used to authenticate GoCD server, if you have one
--disable set this to disable maintenance mode in GoCD
--enable set this to enable maintenance mode in GoCD
--from-file string file containing configurations of objects that needs to be created in GoCD, config-repo/pipeline-group/environment and etc.
--json enable this to Render output in JSON format
-l, --log-level string log level for gocd cli, log levels supported by [https://github.com/sirupsen/logrus] will work (default "info")
--no-color enable this to Render output in YAML format
-p, --password string password to authenticate with GoCD server
-q, --query string query to filter the results, ex: '.material.attributes.type | id eq git'. this uses library gojsonq beneath
more queries can be found here https://github.com/thedevsaddam/gojsonq/wiki/Queries
--save-config enable this to locally save auth configs used to connect GoCD server (path: $HOME/.gocd/auth_config.yaml)
--server-url string GoCD server URL base path (default "http://localhost:8153/go")
--skip-cache-config if enabled locally save auth configs would not be used to authenticate GoCD server (path: $HOME/.gocd/auth_config.yaml)
--to-file string file to which the output needs to be written to (this works only if --yaml or --json is enabled)
-u, --username string username to authenticate with GoCD server
--yaml enable this to Render output in YAML format
```

### SEE ALSO

* [gocd-cli maintenance](gocd-cli_maintenance.md) - Command to operate on maintenance modes in GoCD [https://api.gocd.org/current/#maintenance-mode]

###### Auto generated by spf13/cobra on 20-Mar-2023
47 changes: 47 additions & 0 deletions docs/doc/gocd-cli_maintenance_get.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
## gocd-cli maintenance get

Command to GET a maintenance mode information from GoCD [https://api.gocd.org/current/#get-maintenance-mode-info]

```
gocd-cli maintenance get [flags]
```

### Examples

```
gocd-cli maintenance get
```

### Options

```
-h, --help help for get
```

### Options inherited from parent commands

```
-t, --auth-token string token to authenticate with GoCD server, should not be co-used with basic auth (username/password)
--ca-file-path string path to file containing CA cert used to authenticate GoCD server, if you have one
--disable set this to disable maintenance mode in GoCD
--enable set this to enable maintenance mode in GoCD
--from-file string file containing configurations of objects that needs to be created in GoCD, config-repo/pipeline-group/environment and etc.
--json enable this to Render output in JSON format
-l, --log-level string log level for gocd cli, log levels supported by [https://github.com/sirupsen/logrus] will work (default "info")
--no-color enable this to Render output in YAML format
-p, --password string password to authenticate with GoCD server
-q, --query string query to filter the results, ex: '.material.attributes.type | id eq git'. this uses library gojsonq beneath
more queries can be found here https://github.com/thedevsaddam/gojsonq/wiki/Queries
--save-config enable this to locally save auth configs used to connect GoCD server (path: $HOME/.gocd/auth_config.yaml)
--server-url string GoCD server URL base path (default "http://localhost:8153/go")
--skip-cache-config if enabled locally save auth configs would not be used to authenticate GoCD server (path: $HOME/.gocd/auth_config.yaml)
--to-file string file to which the output needs to be written to (this works only if --yaml or --json is enabled)
-u, --username string username to authenticate with GoCD server
--yaml enable this to Render output in YAML format
```

### SEE ALSO

* [gocd-cli maintenance](gocd-cli_maintenance.md) - Command to operate on maintenance modes in GoCD [https://api.gocd.org/current/#maintenance-mode]

###### Auto generated by spf13/cobra on 20-Mar-2023
3 changes: 2 additions & 1 deletion docs/doc/gocd-cli_pipeline.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ gocd-cli pipeline [flags]
### SEE ALSO

* [gocd-cli](gocd-cli.md) - Command line interface for GoCD
* [gocd-cli pipeline action](gocd-cli_pipeline_action.md) - 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]
* [gocd-cli pipeline action](gocd-cli_pipeline_action.md) - 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]
* [gocd-cli pipeline comment](gocd-cli_pipeline_comment.md) - Command to COMMENT on a specific pipeline instance present in GoCD [https://api.gocd.org/current/#comment-on-pipeline-instance]
* [gocd-cli pipeline get-all](gocd-cli_pipeline_get-all.md) - Command to GET all pipelines present in GoCD [https://api.gocd.org/current/#get-feed-of-all-stages-in-a-pipeline]
* [gocd-cli pipeline instance](gocd-cli_pipeline_instance.md) - Command to GET instance of a specific pipeline present in GoCD [https://api.gocd.org/current/#get-pipeline-instance]
Expand Down
3 changes: 2 additions & 1 deletion docs/doc/gocd-cli_pipeline_action.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## gocd-cli pipeline action

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]
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]

```
gocd-cli pipeline action [flags]
Expand Down

0 comments on commit 9c95093

Please sign in to comment.