Skip to content

Commit

Permalink
Add command examples to enrich the cli documents
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhilsbhat committed Jan 8, 2024
1 parent 79a2a9c commit f573d6a
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 20 deletions.
1 change: 1 addition & 0 deletions cmd/agent_profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func getAgentProfilesCommand() *cobra.Command {
getElasticAgentProfilesCmd := &cobra.Command{
Use: "get-all",
Short: "Command to GET all the elastic agent profiles present in GoCD [https://api.gocd.org/current/#get-all-elastic-agent-profiles]",
Example: "gocd-cli elastic-agent-profile get-all",
Args: cobra.NoArgs,
PreRunE: setCLIClient,
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down
49 changes: 35 additions & 14 deletions cmd/config_repos.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func getConfigReposCommand() *cobra.Command {
configGetCommand := &cobra.Command{
Use: "get-all",
Short: "Command to GET all config-repo information present in GoCD [https://api.gocd.org/current/#get-all-config-repos]",
Example: "gocd-cli configrepo get-all",
Args: cobra.NoArgs,
PreRunE: setCLIClient,
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down Expand Up @@ -95,8 +96,8 @@ func getConfigReposCommand() *cobra.Command {

func getFailedConfigReposCommand() *cobra.Command {
var (
configRepoNames bool
configRepoFailed bool
detailed bool
failedConfigRepo bool
getLastModified bool
)

Expand All @@ -110,6 +111,7 @@ func getFailedConfigReposCommand() *cobra.Command {
Use: "get-internal",
Short: `Command to GET all config repo information present in GoCD using internal api [/api/internal/config_repos]
Do not use this command unless you know what you are doing with it`,
Example: "gocd-cli configrepo get-internal --failed --detailed --yaml",
Args: cobra.NoArgs,
PreRunE: setCLIClient,
RunE: func(cmd *cobra.Command, args []string) error {
Expand All @@ -120,20 +122,20 @@ Do not use this command unless you know what you are doing with it`,

var repos interface{}

if configRepoFailed {
if failedConfigRepo {
response = funk.Filter(response, func(configRepo gocd.ConfigRepo) bool {
return len(configRepo.ConfigRepoParseInfo.Error) != 0
}).([]gocd.ConfigRepo)
}

if configRepoNames {
if detailed {
repos = response
} else {
names := make([]string, 0)
for _, configRepo := range response {
names = append(names, configRepo.ID)
}
repos = names
} else {
repos = response
}

if getLastModified {
Expand Down Expand Up @@ -172,13 +174,13 @@ Do not use this command unless you know what you are doing with it`,
},
}

configGetCommand.PersistentFlags().BoolVarP(&configRepoNames, "names", "", false,
"list of config repo name those are failing")
configGetCommand.PersistentFlags().BoolVarP(&configRepoFailed, "failed", "", false,
configGetCommand.PersistentFlags().BoolVarP(&detailed, "detailed", "", false,
"when enabled prints the config-repo information in detailed")
configGetCommand.PersistentFlags().BoolVarP(&failedConfigRepo, "failed", "", false,
"when enabled, fetches only the failed config repositories")
configGetCommand.PersistentFlags().BoolVarP(&getLastModified, "last-modified", "", false,
"list config repo with last modified in number of days")
configGetCommand.MarkFlagsMutuallyExclusive("last-modified", "names")
configGetCommand.MarkFlagsMutuallyExclusive("last-modified", "detailed")

configGetCommand.SetUsageTemplate(getUsageTemplate())

Expand All @@ -187,8 +189,10 @@ Do not use this command unless you know what you are doing with it`,

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]",
Use: "get-definitions",
Short: "Command to GET config-repo definitions present in GoCD [https://api.gocd.org/current/#definitions-defined-in-config-repo]",
Example: `gocd-cli configrepo get-definitions sample-repo --yaml
gocd-cli configrepo get-definitions sample-repo --yaml --pipelines #should print only pipeline names`,
Args: cobra.RangeArgs(1, 1),
PreRunE: setCLIClient,
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down Expand Up @@ -246,6 +250,7 @@ func getConfigRepoCommand() *cobra.Command {
configGetCommand := &cobra.Command{
Use: "get",
Short: "Command to GET the config-repo information with a specified ID present in GoCD [https://api.gocd.org/current/#get-a-config-repo]",
Example: "gocd-cli configrepo get helm-images",
Args: cobra.RangeArgs(1, 1),
PreRunE: setCLIClient,
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down Expand Up @@ -359,6 +364,7 @@ func getDeleteConfigRepoCommand() *cobra.Command {
configUpdateStatusCommand := &cobra.Command{
Use: "delete",
Short: "Command to DELETE the specified config-repo [https://api.gocd.org/current/#delete-a-config-repo]",
Example: "gocd-cli configrepo delete helm-images",
Args: cobra.RangeArgs(1, 1),
PreRunE: setCLIClient,
RunE: func(cmd *cobra.Command, args []string) error {
Expand All @@ -377,6 +383,7 @@ func listConfigReposCommand() *cobra.Command {
listConfigReposCmd := &cobra.Command{
Use: "list",
Short: "Command to LIST all configuration repository present in GoCD [https://api.gocd.org/current/#get-all-config-repos]",
Example: "gocd-cli configrepo list",
Args: cobra.NoArgs,
PreRunE: setCLIClient,
RunE: func(cmd *cobra.Command, args []string) error {
Expand All @@ -402,6 +409,7 @@ func getConfigRepoStatusCommand() *cobra.Command {
configStatusCommand := &cobra.Command{
Use: "status",
Short: "Command to GET the status of config-repo update operation [https://api.gocd.org/current/#status-of-config-repository-update]",
Example: "gocd-cli configrepo status helm-images",
Args: cobra.RangeArgs(1, 1),
PreRunE: setCLIClient,
RunE: func(cmd *cobra.Command, args []string) error {
Expand All @@ -423,6 +431,7 @@ func getConfigRepoTriggerUpdateCommand() *cobra.Command {
configTriggerUpdateCommand := &cobra.Command{
Use: "trigger-update",
Short: "Command to TRIGGER the update for config-repo to get latest revisions [https://api.gocd.org/current/#trigger-update-of-config-repository]",
Example: "gocd-cli configrepo trigger-update helm-images",
Args: cobra.RangeArgs(1, 1),
PreRunE: setCLIClient,
RunE: func(cmd *cobra.Command, args []string) error {
Expand All @@ -441,10 +450,13 @@ func getConfigRepoTriggerUpdateCommand() *cobra.Command {
}

func getConfigRepoPreflightCheckCommand() *cobra.Command {
var goCDConfigRepoName string

configTriggerUpdateCommand := &cobra.Command{
Use: "preflight-check",
Short: "Command to PREFLIGHT check the config repo configurations [https://api.gocd.org/current/#preflight-check-of-config-repo-configurations]",
Args: cobra.RangeArgs(1, 1),
Example: `gocd-cli configrepo preflight-check -f path/to/pipeline1.gocd.yaml -f path/to/pipeline2.gocd.yaml --repo-name helm-images --yaml`,
Args: cobra.NoArgs,
PreRunE: setCLIClient,
RunE: func(cmd *cobra.Command, args []string) error {
var pipelineFiles []gocd.PipelineFiles
Expand All @@ -454,6 +466,8 @@ func getConfigRepoPreflightCheckCommand() *cobra.Command {
for _, pipelineFile := range configRepoPreflightObj.pipelineFiles {
file, err := client.GetPipelineFiles(pipelineFile)
if err != nil {
cliLogger.Errorf("fetching pipeline %s errored with: %v", pipelineFile, err)

return err
}
pipelineFiles = append(pipelineFiles, file...)
Expand All @@ -467,6 +481,8 @@ func getConfigRepoPreflightCheckCommand() *cobra.Command {
pathAndPattern = append(pathAndPattern, configRepoPreflightObj.pipelineExtRegex)
file, err := client.GetPipelineFiles(pathAndPattern[0], pathAndPattern[1])
if err != nil {
cliLogger.Errorf("fetching pipeline using regex errored with: %v", err)

return err
}

Expand All @@ -475,8 +491,10 @@ func getConfigRepoPreflightCheckCommand() *cobra.Command {

pipelineMap := client.SetPipelineFiles(pipelineFiles)

response, err := client.ConfigRepoPreflightCheck(pipelineMap, goCdPluginObj.getPluginID(), args[0])
response, err := client.ConfigRepoPreflightCheck(pipelineMap, goCdPluginObj.getPluginID(), goCDConfigRepoName)
if err != nil {
cliLogger.Errorf("preflight checks errored with: %v", err)

return err
}

Expand All @@ -487,6 +505,9 @@ func getConfigRepoPreflightCheckCommand() *cobra.Command {
configTriggerUpdateCommand.SetUsageTemplate(getUsageTemplate())
registerConfigRepoPreflightFlags(configTriggerUpdateCommand)

configTriggerUpdateCommand.PersistentFlags().StringVarP(&goCDConfigRepoName, "repo-name", "", "",
"name of the config repo present in GoCD against which the pipeline has to be validated")

return configTriggerUpdateCommand
}

Expand Down
7 changes: 5 additions & 2 deletions cmd/encryption.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func getEncryptCommand() *cobra.Command {
encryptionCommand := &cobra.Command{
Use: "encrypt",
Short: "Command to encrypt plain text value [https://api.gocd.org/current/#encryption]",
Example: "gocd-cli encryption encrypt password-to-encrypt",
Args: cobra.MinimumNArgs(1),
PreRunE: setCLIClient,
RunE: func(cmd *cobra.Command, args []string) error {
Expand All @@ -61,8 +62,10 @@ func getEncryptCommand() *cobra.Command {

func getDecryptCommand() *cobra.Command {
decryptionCommand := &cobra.Command{
Use: "decrypt",
Short: "Command to decrypt encrypted value [https://github.com/nikhilsbhat/gocd-sdk-go/blob/master/encryption.go#L49]",
Use: "decrypt",
Short: "Command to decrypt encrypted value [https://github.com/nikhilsbhat/gocd-sdk-go/blob/master/encryption.go#L49]",
Example: `gocd-cli encryption decrypt AES:ldwjekwdjwjwekjkjwefwrfh --cipher-key LKSJDLKDCXMAWLEKFERJSDKBI
gocd-cli encryption decrypt AES:ldwjekwdjwjwekjkjwefwrfh --cipher-key-path path/to/key`,
Args: cobra.MinimumNArgs(1),
PreRunE: setCLIClient,
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down
19 changes: 15 additions & 4 deletions cmd/environments.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,11 @@ GET/CREATE/UPDATE/PATCH/DELETE and list GoCD environments`,

func getEnvironmentsCommand() *cobra.Command {
getEnvironmentsCmd := &cobra.Command{
Use: "get-all",
Short: "Command to GET all the environments present in GoCD [https://api.gocd.org/current/#get-all-environments]",
Use: "get-all",
Short: "Command to GET all the environments present in GoCD [https://api.gocd.org/current/#get-all-environments]",
Example: `gocd-cli environment get-all --yaml
gocd-cli environment get-all --env-var ENVIRONMENT_VAR_1 --env-var ENVIRONMENT_VAR_2 --yaml
gocd-cli environment get-all --pipelines --yaml`,
Args: cobra.NoArgs,
PreRunE: setCLIClient,
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down Expand Up @@ -111,8 +114,11 @@ func getEnvironmentsCommand() *cobra.Command {

func getEnvironmentCommand() *cobra.Command {
getEnvironmentCmd := &cobra.Command{
Use: "get",
Short: "Command to GET a specific environments present in GoCD [https://api.gocd.org/current/#get-environment-config]",
Use: "get",
Short: "Command to GET a specific environments present in GoCD [https://api.gocd.org/current/#get-environment-config]",
Example: `gocd-cli environment get gocd_environment_1
gocd-cli environment get gocd_environment_1 --env-var ENVIRONMENT_VAR_1 --env-var ENVIRONMENT_VAR_2 --yaml
gocd-cli environment get gocd_environment_1 --pipelines --yaml`,
Args: cobra.RangeArgs(1, 1),
PreRunE: setCLIClient,
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down Expand Up @@ -173,6 +179,7 @@ func createEnvironmentCommand() *cobra.Command {
createEnvironmentCmd := &cobra.Command{
Use: "create",
Short: "Command to CREATE the environment with all specified configuration [https://api.gocd.org/current/#create-an-environment]",
Example: `gocd-cli environment create gocd_environment_1 --from-file gocd_environment_1.yaml`,
Args: cobra.NoArgs,
PreRunE: setCLIClient,
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down Expand Up @@ -210,6 +217,7 @@ func updateEnvironmentCommand() *cobra.Command {
updateEnvironmentCmd := &cobra.Command{
Use: "update",
Short: "Command to UPDATE the environment with the latest specified configuration [https://api.gocd.org/current/#update-an-environment]",
Example: `gocd-cli environment update gocd_environment_1 --from-file gocd_environment_1.yaml`,
Args: cobra.NoArgs,
PreRunE: setCLIClient,
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down Expand Up @@ -254,6 +262,7 @@ func patchEnvironmentCommand() *cobra.Command {
patchEnvironmentCmd := &cobra.Command{
Use: "patch",
Short: "Command to PATCH the environment with the latest specified configuration [https://api.gocd.org/current/#patch-an-environment]",
Example: `gocd-cli environment patch gocd_environment_1 --from-file gocd_environment_1.yaml`,
Args: cobra.RangeArgs(1, 1),
PreRunE: setCLIClient,
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down Expand Up @@ -296,6 +305,7 @@ func deleteEnvironmentCommand() *cobra.Command {
deleteEnvironmentCmd := &cobra.Command{
Use: "delete",
Short: "Command to DELETE the specified environment from GoCD [https://api.gocd.org/current/#delete-an-environment]",
Example: `gocd-cli environment delete gocd_environment_1`,
Args: cobra.RangeArgs(1, 1),
PreRunE: setCLIClient,
RunE: func(cmd *cobra.Command, args []string) error {
Expand All @@ -314,6 +324,7 @@ func listEnvironmentsCommand() *cobra.Command {
listEnvironmentsCmd := &cobra.Command{
Use: "list",
Short: "Command to LIST all the environments present in GoCD [https://api.gocd.org/current/#get-all-environments]",
Example: `gocd-cli environment list --yaml`,
Args: cobra.NoArgs,
PreRunE: setCLIClient,
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down

0 comments on commit f573d6a

Please sign in to comment.