Skip to content

Commit

Permalink
Use package github.com/nikhilsbhat/common
Browse files Browse the repository at this point in the history
  - This helps in rendering the output to multiple formats
  - Identify the input content type
  - CLI shell prompts
  • Loading branch information
nikhilsbhat committed Jan 27, 2024
1 parent 62ad13a commit d8c5eca
Show file tree
Hide file tree
Showing 21 changed files with 251 additions and 145 deletions.
16 changes: 9 additions & 7 deletions cmd/agent_profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"strings"

"github.com/nikhilsbhat/common/content"
"github.com/nikhilsbhat/gocd-cli/pkg/errors"
"github.com/nikhilsbhat/gocd-cli/pkg/render"
"github.com/nikhilsbhat/gocd-sdk-go"
Expand Down Expand Up @@ -123,11 +124,11 @@ func createAgentProfileCommand() *cobra.Command {
}

switch objType := object.CheckFileType(cliLogger); objType {
case render.FileTypeYAML:
case content.FileTypeYAML:
if err = yaml.Unmarshal([]byte(object), &commonCfg); err != nil {
return err
}
case render.FileTypeJSON:
case content.FileTypeJSON:
if err = json.Unmarshal([]byte(object), &commonCfg); err != nil {
return err
}
Expand Down Expand Up @@ -166,11 +167,11 @@ func updateAgentProfileCommand() *cobra.Command {
}

switch objType := object.CheckFileType(cliLogger); objType {
case render.FileTypeYAML:
case content.FileTypeYAML:
if err = yaml.Unmarshal([]byte(object), &commonCfg); err != nil {
return err
}
case render.FileTypeJSON:
case content.FileTypeJSON:
if err = json.Unmarshal([]byte(object), &commonCfg); err != nil {
return err
}
Expand All @@ -196,9 +197,10 @@ func updateAgentProfileCommand() *cobra.Command {

func deleteAgentProfileCommand() *cobra.Command {
deleteElasticAgentProfileCmd := &cobra.Command{
Use: "delete",
Short: "Command to DELETE a specific elastic agent profile present in GoCD [https://api.gocd.org/current/#delete-an-elastic-agent-profile]",
Example: "gocd-cli elastic-agent-profile delete sample_kubernetes",
Use: "delete",
Short: "Command to DELETE a specific elastic agent profile present in GoCD [https://api.gocd.org/current/#delete-an-elastic-agent-profile]",
Example: `gocd-cli elastic-agent-profile delete sample_kubernetes
gocd-cli elastic-agent-profile delete sample_kubernetes -y`,
Args: cobra.RangeArgs(1, 1),
PreRunE: setCLIClient,
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down
9 changes: 5 additions & 4 deletions cmd/agents.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"reflect"

"github.com/nikhilsbhat/common/content"
"github.com/nikhilsbhat/gocd-cli/pkg/errors"
"github.com/nikhilsbhat/gocd-cli/pkg/render"
"github.com/nikhilsbhat/gocd-sdk-go"
Expand Down Expand Up @@ -159,11 +160,11 @@ func updateAgentCommand() *cobra.Command {
}

switch objType := object.CheckFileType(cliLogger); objType {
case render.FileTypeYAML:
case content.FileTypeYAML:
if err = yaml.Unmarshal([]byte(object), &agent); err != nil {
return err
}
case render.FileTypeJSON:
case content.FileTypeJSON:
if err = json.Unmarshal([]byte(object), &agent); err != nil {
return err
}
Expand Down Expand Up @@ -417,8 +418,8 @@ func filterAgentsResponse(response []gocd.Agent) []gocd.Agent {

if len(agentOS) != 0 {
response = funk.Filter(response, func(agent gocd.Agent) bool {
for _, os := range agentOS {
return funk.Contains(agent.OS, os)
for _, agOS := range agentOS {
return funk.Contains(agent.OS, agOS)
}

return false
Expand Down
13 changes: 7 additions & 6 deletions cmd/artifacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"strings"

"github.com/nikhilsbhat/common/content"
"github.com/nikhilsbhat/gocd-cli/pkg/errors"
"github.com/nikhilsbhat/gocd-cli/pkg/render"
"github.com/nikhilsbhat/gocd-sdk-go"
Expand Down Expand Up @@ -154,11 +155,11 @@ func createArtifactStoreCommand() *cobra.Command {
}

switch objType := object.CheckFileType(cliLogger); objType {
case render.FileTypeYAML:
case content.FileTypeYAML:
if err = yaml.Unmarshal([]byte(object), &commonCfg); err != nil {
return err
}
case render.FileTypeJSON:
case content.FileTypeJSON:
if err = json.Unmarshal([]byte(object), &commonCfg); err != nil {
return err
}
Expand Down Expand Up @@ -196,11 +197,11 @@ func updateArtifactStoreCommand() *cobra.Command {
}

switch objType := object.CheckFileType(cliLogger); objType {
case render.FileTypeYAML:
case content.FileTypeYAML:
if err = yaml.Unmarshal([]byte(object), &commonCfg); err != nil {
return err
}
case render.FileTypeJSON:
case content.FileTypeJSON:
if err = json.Unmarshal([]byte(object), &commonCfg); err != nil {
return err
}
Expand Down Expand Up @@ -238,11 +239,11 @@ func updateArtifactConfigCommand() *cobra.Command {
}

switch objType := object.CheckFileType(cliLogger); objType {
case render.FileTypeYAML:
case content.FileTypeYAML:
if err = yaml.Unmarshal([]byte(object), &artifactInfo); err != nil {
return err
}
case render.FileTypeJSON:
case content.FileTypeJSON:
if err = json.Unmarshal([]byte(object), &artifactInfo); err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"strconv"
"time"

"github.com/nikhilsbhat/common/content"
"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"
Expand Down Expand Up @@ -79,11 +79,11 @@ func createOrUpdateBackupConfig() *cobra.Command {
}

switch objType := object.CheckFileType(cliLogger); objType {
case render.FileTypeYAML:
case content.FileTypeYAML:
if err = yaml.Unmarshal([]byte(object), &backupConfig); err != nil {
return err
}
case render.FileTypeJSON:
case content.FileTypeJSON:
if err = json.Unmarshal([]byte(object), &backupConfig); err != nil {
return err
}
Expand Down
50 changes: 45 additions & 5 deletions cmd/client.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
package cmd

import (
"fmt"
"log"
"os"
"path/filepath"
"strings"

"github.com/nikhilsbhat/gocd-cli/pkg/render"
"github.com/nikhilsbhat/common/renderer"
"github.com/nikhilsbhat/gocd-cli/pkg/errors"
"github.com/nikhilsbhat/gocd-cli/pkg/utils"
"github.com/nikhilsbhat/gocd-sdk-go"
"github.com/spf13/cobra"
"github.com/thoas/go-funk"
"gopkg.in/yaml.v3"
)

var (
client gocd.GoCd
cliRenderer render.Renderer
cliShellReadConfig *utils.ReadConfig
client gocd.GoCd
cliRenderer renderer.Config
cliShellReadConfig *utils.ReadConfig
supportedOutputFormats = []string{"yaml", "json", "csv", "table"}
)

func setCLIClient(_ *cobra.Command, _ []string) error {
Expand Down Expand Up @@ -79,10 +84,45 @@ func setCLIClient(_ *cobra.Command, _ []string) error {
writer = filePTR
}

cliRenderer = render.GetRenderer(writer, cliLogger, cliCfg.YAML, cliCfg.JSON)
if !cliCfg.validateOutputFormats() {
supportedOutputFormatsString := strings.Join(supportedOutputFormats, "|")
cliLogger.Errorf("unsupported output format '%s', the value should be one of %s",
cliCfg.OutputFormat, supportedOutputFormatsString)

return &errors.CLIError{
Message: fmt.Sprintf("unsupported output format '%s', the value should be one of %s",
cliCfg.OutputFormat, supportedOutputFormatsString),
}
}

cliCfg.setOutputFormats()

cliRenderer = renderer.GetRenderer(writer, cliLogger, cliCfg.NoColor, cliCfg.yaml, cliCfg.json, cliCfg.csv, cliCfg.table)

inputOptions := []utils.Options{{Name: "yes", Short: "y"}, {Name: "no", Short: "n"}}
cliShellReadConfig = utils.NewReadConfig("gocd-cli", "", inputOptions, cliLogger)

return nil
}

func (cfg *Config) validateOutputFormats() bool {
if len(cfg.OutputFormat) == 0 {
return true
}

return funk.Contains(supportedOutputFormats, strings.ToLower(cfg.OutputFormat))
}

func (cfg *Config) setOutputFormats() {
switch strings.ToLower(cfg.OutputFormat) {
case "yaml":
cfg.yaml = true
case "json":
cfg.json = true
case "csv":
cfg.csv = true
case "table":
cfg.table = true
default:
}
}
9 changes: 5 additions & 4 deletions cmd/cluster_profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"strings"

"github.com/nikhilsbhat/common/content"
"github.com/nikhilsbhat/gocd-cli/pkg/errors"
"github.com/nikhilsbhat/gocd-cli/pkg/render"
"github.com/nikhilsbhat/gocd-sdk-go"
Expand Down Expand Up @@ -118,11 +119,11 @@ func createClusterProfileCommand() *cobra.Command {
}

switch objType := object.CheckFileType(cliLogger); objType {
case render.FileTypeYAML:
case content.FileTypeYAML:
if err = yaml.Unmarshal([]byte(object), &commonCfg); err != nil {
return err
}
case render.FileTypeJSON:
case content.FileTypeJSON:
if err = json.Unmarshal([]byte(object), &commonCfg); err != nil {
return err
}
Expand Down Expand Up @@ -160,11 +161,11 @@ func updateClusterProfileCommand() *cobra.Command {
}

switch objType := object.CheckFileType(cliLogger); objType {
case render.FileTypeYAML:
case content.FileTypeYAML:
if err = yaml.Unmarshal([]byte(object), &commonCfg); err != nil {
return err
}
case render.FileTypeJSON:
case content.FileTypeJSON:
if err = json.Unmarshal([]byte(object), &commonCfg); err != nil {
return err
}
Expand Down
26 changes: 15 additions & 11 deletions cmd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,21 @@ type cliCommands struct {

// Config holds the information of the cli config.
type Config struct {
URL string `yaml:"url,omitempty"`
CaPath string `yaml:"ca_path,omitempty"`
Auth gocd.Auth `yaml:"auth,omitempty"`
Yes bool `yaml:"-"`
JSON bool `yaml:"-"`
YAML bool `yaml:"-"`
NoColor bool `yaml:"-"`
LogLevel string `yaml:"-"`
APILogLevel string `yaml:"-"`
FromFile string `yaml:"-"`
ToFile string `yaml:"-"`
URL string `yaml:"url,omitempty"`
CaPath string `yaml:"ca_path,omitempty"`
Auth gocd.Auth `yaml:"auth,omitempty"`
Yes bool `yaml:"-"`
OutputFormat string `yaml:"-"`
NoColor bool `yaml:"-"`
LogLevel string `yaml:"-"`
APILogLevel string `yaml:"-"`
FromFile string `yaml:"-"`
ToFile string `yaml:"-"`
TableData [][]string `yaml:"-"`
json bool `yaml:"-"`
yaml bool `yaml:"-"`
csv bool `yaml:"-"`
table bool `yaml:"-"`
skipCacheConfig bool
}

Expand Down
27 changes: 23 additions & 4 deletions cmd/config_repos.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"
"time"

"github.com/nikhilsbhat/common/content"
"github.com/nikhilsbhat/gocd-cli/pkg/errors"
"github.com/nikhilsbhat/gocd-cli/pkg/render"
"github.com/nikhilsbhat/gocd-sdk-go"
Expand Down Expand Up @@ -86,6 +87,15 @@ func getConfigReposCommand() *cobra.Command {
return cliRenderer.Render(baseQuery.RunQuery())
}

if cliRenderer.Table {
cliCfg.TableData = append(cliCfg.TableData, []string{"ID", "Data"})
for _, res := range response {
cliCfg.TableData = append(cliCfg.TableData, []string{res.ID, fmt.Sprintf("%v", res)})
}

return cliRenderer.Render(cliCfg.TableData)
}

return cliRenderer.Render(response)
},
}
Expand Down Expand Up @@ -273,6 +283,13 @@ func getConfigRepoCommand() *cobra.Command {
return cliRenderer.Render(baseQuery.RunQuery())
}

if cliRenderer.Table {
cliCfg.TableData = append(cliCfg.TableData, []string{"ID", "Data"})
cliCfg.TableData = append(cliCfg.TableData, []string{response.ID, fmt.Sprintf("%v", response)})

return cliRenderer.Render(cliCfg.TableData)
}

return cliRenderer.Render(response)
},
}
Expand All @@ -295,12 +312,14 @@ func getCreateConfigRepoCommand() *cobra.Command {
return err
}

fmt.Println(object.String())

switch objType := object.CheckFileType(cliLogger); objType {
case render.FileTypeYAML:
case content.FileTypeYAML:
if err = yaml.Unmarshal([]byte(object), &configRepo); err != nil {
return err
}
case render.FileTypeJSON:
case content.FileTypeJSON:
if err = json.Unmarshal([]byte(object), &configRepo); err != nil {
return err
}
Expand Down Expand Up @@ -335,11 +354,11 @@ func getUpdateConfigRepoCommand() *cobra.Command {
}

switch objType := object.CheckFileType(cliLogger); objType {
case render.FileTypeYAML:
case content.FileTypeYAML:
if err = yaml.Unmarshal([]byte(object), &configRepo); err != nil {
return err
}
case render.FileTypeJSON:
case content.FileTypeJSON:
if err = json.Unmarshal([]byte(object), &configRepo); err != nil {
return err
}
Expand Down
Loading

0 comments on commit d8c5eca

Please sign in to comment.