Skip to content

Commit

Permalink
Add support to GET/CREATE/UPDATE/DELETE/LIST artifact store and artif…
Browse files Browse the repository at this point in the history
…act config
  • Loading branch information
nikhilsbhat committed Jun 12, 2023
1 parent 8793b01 commit 208bc04
Show file tree
Hide file tree
Showing 6 changed files with 340 additions and 19 deletions.
8 changes: 7 additions & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ builds:
flags:
- -trimpath
ldflags:
- '-s -w -X github.com/nikhilsbhat/gocd-cli/cmd.Version={{.Version}} -X github.com/nikhilsbhat/gocd-cli/cmd.Env={{.Env.BUILD_ENVIRONMENT}} -X github.com/nikhilsbhat/gocd-cli/cmd.BuildDate={{.Date}} -X github.com/nikhilsbhat/gocd-cli/cmd.Revision={{.Commit}} -X github.com/nikhilsbhat/gocd-cli/cmd.GoVersion={{.Env.GOVERSION}} -X github.com/nikhilsbhat/gocd-cli/cmd.Platform={{ .Os }}/{{ .Arch }}'
- -s -w
-X github.com/nikhilsbhat/gocd-cli/cmd.Version={{.Version}}
-X github.com/nikhilsbhat/gocd-cli/cmd.Env={{.Env.BUILD_ENVIRONMENT}}
-X github.com/nikhilsbhat/gocd-cli/cmd.BuildDate={{.Date}}
-X github.com/nikhilsbhat/gocd-cli/cmd.Revision={{.Commit}}
-X github.com/nikhilsbhat/gocd-cli/cmd.GoVersion={{.Env.GOVERSION}}
-X github.com/nikhilsbhat/gocd-cli/cmd.Platform={{ .Os }}/{{ .Arch }}
goos:
- freebsd
- windows
Expand Down
313 changes: 313 additions & 0 deletions cmd/artifacts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,313 @@
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"
)

func registerArtifactCommand() *cobra.Command {
agentsCommand := &cobra.Command{
Use: "artifact",
Short: "Command to operate on artifacts store/config present in GoCD",
Long: `Command leverages GoCD agents apis' [https://api.gocd.org/current/#artifacts-config, https://api.gocd.org/current/#artifact-store] to
GET/CREATE/UPDATE/DELETE GoCD artifact`,
RunE: func(cmd *cobra.Command, args []string) error {
if err := cmd.Usage(); err != nil {
return err
}

return nil
},
}

agentsCommand.SetUsageTemplate(getUsageTemplate())

agentsCommand.AddCommand(getArtifactStoreCommand())
agentsCommand.AddCommand(getArtifactStoresCommand())
agentsCommand.AddCommand(getArtifactConfigCommand())
agentsCommand.AddCommand(createArtifactStoreCommand())
agentsCommand.AddCommand(updateArtifactStoreCommand())
agentsCommand.AddCommand(updateArtifactConfigCommand())
agentsCommand.AddCommand(deleteArtifactStoreCommand())
agentsCommand.AddCommand(listArtifactsStoreCommand())
agentsCommand.AddCommand(killTaskCommand())
agentsCommand.AddCommand(getJobRunHistoryCommand())

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

return agentsCommand
}

func getArtifactStoreCommand() *cobra.Command {
getArtifactStoreCmd := &cobra.Command{
Use: "get-store",
Short: "Command to GET an specific artifact store in GoCD [https://api.gocd.org/current/#get-an-artifact-store]",
Args: cobra.RangeArgs(1, 1),
PreRunE: setCLIClient,
RunE: func(cmd *cobra.Command, args []string) error {
response, err := client.GetArtifactStore(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 getArtifactStoreCmd
}

func getArtifactStoresCommand() *cobra.Command {
getArtifactStoresCmd := &cobra.Command{
Use: "get-all-stores",
Short: "Command to GET all the artifact stores present in GoCD [https://api.gocd.org/current/#get-all-artifact-stores]",
Args: cobra.NoArgs,
PreRunE: setCLIClient,
RunE: func(cmd *cobra.Command, args []string) error {
response, err := client.GetArtifactStores()
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.CommonConfigs)
},
}

return getArtifactStoresCmd
}

func getArtifactConfigCommand() *cobra.Command {
getArtifactsConfigCmd := &cobra.Command{
Use: "get-config",
Short: "Command to GET a configured artifact configuration GoCD [https://api.gocd.org/current/#get-artifacts-config]",
Args: cobra.NoArgs,
PreRunE: setCLIClient,
RunE: func(cmd *cobra.Command, args []string) error {
response, err := client.GetArtifactConfig()
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 getArtifactsConfigCmd
}

func createArtifactStoreCommand() *cobra.Command {
getArtifactsStoreCmd := &cobra.Command{
Use: "create-store",
Short: "Command to CREATE an artifact store with all specified configurations in GoCD [https://api.gocd.org/current/#create-an-artifact-store]",
Args: cobra.RangeArgs(1, 1),
PreRunE: setCLIClient,
RunE: func(cmd *cobra.Command, args []string) error {
var commonCfg gocd.CommonConfig
object, err := readObject(cmd)
if err != nil {
return err
}

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

response, err := client.CreateArtifactStore(commonCfg)
if err != nil {
return err
}

if err = cliRenderer.Render(fmt.Sprintf("artifact store %s created successfully", commonCfg.Name)); err != nil {
return err
}

return cliRenderer.Render(response)
},
}

return getArtifactsStoreCmd
}

func updateArtifactStoreCommand() *cobra.Command {
updateArtifactsStoreCmd := &cobra.Command{
Use: "update-store",
Short: "Command to UPDATE an artifact store with all specified configurations in GoCD [https://api.gocd.org/current/#update-an-artifact-store]",
Args: cobra.RangeArgs(1, 1),
PreRunE: setCLIClient,
RunE: func(cmd *cobra.Command, args []string) error {
var commonCfg gocd.CommonConfig
object, err := readObject(cmd)
if err != nil {
return err
}

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

response, err := client.UpdateArtifactStore(commonCfg)
if err != nil {
return err
}

if err = cliRenderer.Render(fmt.Sprintf("artifact store %s updated successfully", commonCfg.Name)); err != nil {
return err
}

return cliRenderer.Render(response)
},
}

return updateArtifactsStoreCmd
}

func updateArtifactConfigCommand() *cobra.Command {
updateArtifactsConfigCmd := &cobra.Command{
Use: "update-config",
Short: "Command to UPDATE artifact config specified configurations in GoCD [https://api.gocd.org/current/#update-artifacts-config]",
Args: cobra.NoArgs,
PreRunE: setCLIClient,
RunE: func(cmd *cobra.Command, args []string) error {
var artifactInfo gocd.ArtifactInfo
object, err := readObject(cmd)
if err != nil {
return err
}

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

response, err := client.UpdateArtifactConfig(artifactInfo)
if err != nil {
return err
}

if err = cliRenderer.Render("artifact config updated successfully"); err != nil {
return err
}

return cliRenderer.Render(response)
},
}

return updateArtifactsConfigCmd
}

func deleteArtifactStoreCommand() *cobra.Command {
deleteArtifactsStoreCmd := &cobra.Command{
Use: "delete-store",
Short: "Command to DELETE a specific artifact store present in GoCD [https://api.gocd.org/current/#delete-an-artifact-store]",
Args: cobra.RangeArgs(1, 1),
PreRunE: setCLIClient,
RunE: func(cmd *cobra.Command, args []string) error {
if err := client.DeleteArtifactStore(args[0]); err != nil {
return err
}

return cliRenderer.Render(fmt.Sprintf("artifact store '%s' deleted successfully", args[0]))
},
}

return deleteArtifactsStoreCmd
}

func listArtifactsStoreCommand() *cobra.Command {
listArtifactsStoreCmd := &cobra.Command{
Use: "list-store",
Short: "Command to LIST all artifact stores present in GoCD [https://api.gocd.org/current/#get-all-artifact-stores]",
Args: cobra.NoArgs,
PreRunE: setCLIClient,
RunE: func(cmd *cobra.Command, args []string) error {
response, err := client.GetArtifactStores()
if err != nil {
return err
}

var artifactStores []string

for _, commonConfig := range response.CommonConfigs {
artifactStores = append(artifactStores, commonConfig.ID)
}

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

return listArtifactsStoreCmd
}
1 change: 1 addition & 0 deletions cmd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func getGoCDCliCommands() *cobra.Command {
command.commands = append(command.commands, registerPipelinesCommand())
command.commands = append(command.commands, registerMaintenanceCommand())
command.commands = append(command.commands, registerJobsCommand())
command.commands = append(command.commands, registerArtifactCommand())

return command.prepareCommands()
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/pipelines.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func createPipelineCommand() *cobra.Command {
pipelineConfig.PauseReason = goCDPipelineMessage
}

if err = client.CreatePipeline(pipelineConfig); err != nil {
if _, err = client.CreatePipeline(pipelineConfig); err != nil {
return err
}

Expand Down
13 changes: 7 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ go 1.19

require (
github.com/ghodss/yaml v1.0.0
github.com/nikhilsbhat/gocd-sdk-go v0.1.1
github.com/sirupsen/logrus v1.9.0
github.com/nikhilsbhat/gocd-sdk-go v0.1.3-0.20230612172954-c5369e830502
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.6.1
github.com/stretchr/testify v1.8.0
github.com/stretchr/testify v1.8.2
github.com/thedevsaddam/gojsonq/v2 v2.5.2
gopkg.in/yaml.v3 v3.0.1
)
Expand All @@ -18,11 +18,12 @@ require (
github.com/go-resty/resty/v2 v2.7.0 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/jinzhu/copier v0.3.5 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/rogpeppe/go-internal v1.9.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sys v0.9.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
Loading

0 comments on commit 208bc04

Please sign in to comment.