diff --git a/cmd/config_repos.go b/cmd/config_repos.go index b008bff..431a874 100644 --- a/cmd/config_repos.go +++ b/cmd/config_repos.go @@ -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 { @@ -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 @@ -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", diff --git a/cmd/flags.go b/cmd/flags.go index 804f88e..d8cd747 100644 --- a/cmd/flags.go +++ b/cmd/flags.go @@ -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") diff --git a/docs/doc/gocd-cli_configrepo.md b/docs/doc/gocd-cli_configrepo.md index 028ad3e..a1739fc 100644 --- a/docs/doc/gocd-cli_configrepo.md +++ b/docs/doc/gocd-cli_configrepo.md @@ -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 diff --git a/docs/doc/gocd-cli_configrepo_get-definitions.md b/docs/doc/gocd-cli_configrepo_get-definitions.md new file mode 100644 index 0000000..4cd4576 --- /dev/null +++ b/docs/doc/gocd-cli_configrepo_get-definitions.md @@ -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