Skip to content

Commit

Permalink
Add support for sub-command get-definitions for command configrepo
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhilsbhat committed Jun 27, 2023
1 parent 8cb7099 commit 811b39a
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 3 deletions.
63 changes: 61 additions & 2 deletions cmd/config_repos.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ type configRepoPreflight struct {
}

var (
configRepoPreflightObj configRepoPreflight
queryEnabledMessage = "since --query is passed, applying query '%v' to the output"
pipelines, environments bool
configRepoPreflightObj configRepoPreflight
queryEnabledMessage = "since --query is passed, applying query '%v' to the output"
)

func registerConfigRepoCommand() *cobra.Command {
Expand Down Expand Up @@ -50,6 +51,7 @@ GET/CREATE/UPDATE/DELETE and trigger update on the same`,
configRepoCommand.AddCommand(getDeleteConfigRepoCommand())
configRepoCommand.AddCommand(listConfigReposCommand())
configRepoCommand.AddCommand(getConfigRepoPreflightCheckCommand())
configRepoCommand.AddCommand(getConfigReposDefinitionsCommand())

for _, command := range configRepoCommand.Commands() {
command.SilenceUsage = true
Expand Down Expand Up @@ -92,6 +94,63 @@ func getConfigReposCommand() *cobra.Command {
return configGetCommand
}

func getConfigReposDefinitionsCommand() *cobra.Command {
getConfigReposDefinitionsCmd := &cobra.Command{
Use: "get-definitions",
Short: "Command to GET config-repo definitions present in GoCD [https://api.gocd.org/current/#definitions-defined-in-config-repo]",
Args: cobra.RangeArgs(1, 1),
PreRunE: setCLIClient,
RunE: func(cmd *cobra.Command, args []string) error {
response, err := client.GetConfigRepoDefinitions(args[0])
if err != nil {
return err
}

var output interface{}
output = response

envsNPipelines := make([]string, 0)

if environments {
for _, env := range response.Environments {
envsNPipelines = append(envsNPipelines, env.Name)
}
output = envsNPipelines
}

if pipelines {
for _, group := range response.Groups {
for _, pipeline := range group.Pipelines {
envsNPipelines = append(envsNPipelines, pipeline.Name)
}
}
output = envsNPipelines
}

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

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

cliLogger.Debugf(baseQuery.Print())

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

return cliRenderer.Render(output)
},
}

registerConfigRepoDefinitionsFlags(getConfigReposDefinitionsCmd)

getConfigReposDefinitionsCmd.SetUsageTemplate(getUsageTemplate())

return getConfigReposDefinitionsCmd
}

func getConfigRepoCommand() *cobra.Command {
configGetCommand := &cobra.Command{
Use: "get",
Expand Down
7 changes: 7 additions & 0 deletions cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ func registerEncryptionFlags(cmd *cobra.Command) {
"cipher key value used for decryption, the key should same which is used by GoCD server for encryption")
}

func registerConfigRepoDefinitionsFlags(cmd *cobra.Command) {
cmd.PersistentFlags().BoolVarP(&pipelines, "pipelines", "", false,
"set this flag to get only the pipelines from the config-repo")
cmd.PersistentFlags().BoolVarP(&environments, "environments", "", false,
"set this flag to get only the environments from the config-repo")
}

func registerConfigRepoPreflightFlags(cmd *cobra.Command) {
cmd.PersistentFlags().StringVarP(&configRepoPreflightObj.pluginID, "plugin-id", "i", "",
"GoCD's config-repo plugin ID against which the pipelines has to be validated")
Expand Down
3 changes: 2 additions & 1 deletion docs/doc/gocd-cli_configrepo.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ gocd-cli configrepo [flags]
* [gocd-cli configrepo delete](gocd-cli_configrepo_delete.md) - Command to DELETE the specified config-repo [https://api.gocd.org/current/#delete-a-config-repo]
* [gocd-cli configrepo get](gocd-cli_configrepo_get.md) - Command to GET the config-repo information with a specified ID present in GoCD [https://api.gocd.org/current/#get-a-config-repo]
* [gocd-cli configrepo get-all](gocd-cli_configrepo_get-all.md) - Command to GET all config-repo information present in GoCD [https://api.gocd.org/current/#get-all-config-repos]
* [gocd-cli configrepo get-definitions](gocd-cli_configrepo_get-definitions.md) - Command to GET config-repo definitions present in GoCD [https://api.gocd.org/current/#definitions-defined-in-config-repo]
* [gocd-cli configrepo list](gocd-cli_configrepo_list.md) - Command to LIST all configuration repository present in GoCD [https://api.gocd.org/current/#get-all-config-repos]
* [gocd-cli configrepo preflight-check](gocd-cli_configrepo_preflight-check.md) - Command to PREFLIGHT check the config repo configurations [https://api.gocd.org/current/#preflight-check-of-config-repo-configurations]
* [gocd-cli configrepo status](gocd-cli_configrepo_status.md) - Command to GET the status of config-repo update operation [https://api.gocd.org/current/#status-of-config-repository-update]
* [gocd-cli configrepo trigger-update](gocd-cli_configrepo_trigger-update.md) - Command to TRIGGER the update for config-repo to get latest revisions [https://api.gocd.org/current/#trigger-update-of-config-repository]
* [gocd-cli configrepo update](gocd-cli_configrepo_update.md) - Command to UPDATE the config-repo present in GoCD [https://api.gocd.org/current/#update-config-repo]

###### Auto generated by spf13/cobra on 18-Jun-2023
###### Auto generated by spf13/cobra on 27-Jun-2023
40 changes: 40 additions & 0 deletions docs/doc/gocd-cli_configrepo_get-definitions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
## gocd-cli configrepo get-definitions

Command to GET config-repo definitions present in GoCD [https://api.gocd.org/current/#definitions-defined-in-config-repo]

```
gocd-cli configrepo get-definitions [flags]
```

### Options

```
--environments set this flag to get only the environments from the config-repo
-h, --help help for get-definitions
--pipelines set this flag to get only the pipelines from the config-repo
```

### 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
--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 configrepo](gocd-cli_configrepo.md) - Command to operate on configrepo present in GoCD [https://api.gocd.org/current/#config-repo]

###### Auto generated by spf13/cobra on 27-Jun-2023

0 comments on commit 811b39a

Please sign in to comment.