Skip to content

Commit

Permalink
WIP - Add support for showing diff before applying the changes
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhilsbhat committed Mar 30, 2024
1 parent a07c474 commit 1f5154d
Show file tree
Hide file tree
Showing 17 changed files with 256 additions and 97 deletions.
4 changes: 2 additions & 2 deletions cmd/agent_profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,11 @@ gocd-cli elastic-agent-profile delete sample_kubernetes -y`,
if !cliCfg.Yes {
contains, option := cliShellReadConfig.Reader()
if !contains {
cliLogger.Fatalln("user input validation failed, cannot proceed further")
cliLogger.Fatalln(inputValidationFailureMessage)
}

if option.Short == "n" {
cliLogger.Warn("not proceeding further since 'no' was opted")
cliLogger.Warn(optingOutMessage)

os.Exit(0)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/agents.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,11 @@ gocd-cli agents delete --id 938d1935-bdca-4728-83d5-e96cbf0a4f8b`,
if !cliCfg.Yes {
contains, option := cliShellReadConfig.Reader()
if !contains {
cliLogger.Fatalln("user input validation failed, cannot proceed further")
cliLogger.Fatalln(inputValidationFailureMessage)
}

if option.Short == "n" {
cliLogger.Warn("not proceeding further since 'no' was opted")
cliLogger.Warn(optingOutMessage)

os.Exit(0)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/artifacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,11 +280,11 @@ func deleteArtifactStoreCommand() *cobra.Command {
if !cliCfg.Yes {
contains, option := cliShellReadConfig.Reader()
if !contains {
cliLogger.Fatalln("user input validation failed, cannot proceed further")
cliLogger.Fatalln(inputValidationFailureMessage)
}

if option.Short == "n" {
cliLogger.Warn("not proceeding further since 'no' was opted")
cliLogger.Warn(optingOutMessage)

os.Exit(0)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,11 @@ func deleteBackupConfig() *cobra.Command {
if !cliCfg.Yes {
contains, option := cliShellReadConfig.Reader()
if !contains {
cliLogger.Fatalln("user input validation failed, cannot proceed further")
cliLogger.Fatalln(inputValidationFailureMessage)
}

if option.Short == "n" {
cliLogger.Warn("not proceeding further since 'no' was opted")
cliLogger.Warn(optingOutMessage)

os.Exit(0)
}
Expand Down
79 changes: 37 additions & 42 deletions cmd/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,75 +2,57 @@ package cmd

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

"github.com/ghodss/yaml"
"github.com/nikhilsbhat/common/renderer"
"github.com/nikhilsbhat/gocd-cli/pkg/diff"
"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 renderer.Config
cliShellReadConfig *utils.ReadConfig
diffCfg diff.Config
supportedOutputFormats = []string{"yaml", "json", "csv", "table"}
)

func setCLIClient(_ *cobra.Command, _ []string) error {
var caContent []byte

SetLogger(cliCfg.LogLevel)

localConfig, localConfigPath, err := checkForConfig()
if err != nil {
if localConfig, localConfigPath, err := checkForConfig(); err != nil {
return err
}

if localConfig && !cliCfg.skipCacheConfig {
cliLogger.Debugf("found authorisation configuration in cache, loading config from %s", localConfigPath)

yamlConfig, err := os.ReadFile(localConfigPath)
if err != nil {
} else if localConfig && !cliCfg.skipCacheConfig {
cliLogger.Debugf("found authorization configuration in cache, loading config from %s", localConfigPath)
if yamlConfig, err := os.ReadFile(localConfigPath); err != nil {
return err
}

if err = yaml.Unmarshal(yamlConfig, &cliCfg); err != nil {
} else if err := yaml.Unmarshal(yamlConfig, &cliCfg); err != nil {
return err
}

cliLogger.Debug("authorisation configuration loaded from cache successfully")
cliLogger.Debug("authorization configuration loaded from cache successfully")
}

if len(cliCfg.CaPath) != 0 {
cliLogger.Debug("CA based auth is enabled, hence reading ca from the path")
cliLogger.Debug("CA based auth is enabled, hence reading CA from the path")

caAbs, err := filepath.Abs(cliCfg.CaPath)
if err != nil {
if caAbs, err := filepath.Abs(cliCfg.CaPath); err != nil {
return err
} else if caContent, err := os.ReadFile(caAbs); err != nil {
return err
} else {
client = gocd.NewClient(cliCfg.URL, cliCfg.Auth, cliCfg.APILogLevel, caContent)
}

caContent, err = os.ReadFile(caAbs)
if err != nil {
log.Fatal(err)
}
} else {
client = gocd.NewClient(cliCfg.URL, cliCfg.Auth, cliCfg.APILogLevel, nil)
}

goCDClient := gocd.NewClient(
cliCfg.URL,
cliCfg.Auth,
cliCfg.APILogLevel,
caContent,
)

client = goCDClient

writer := os.Stdout

if len(cliCfg.ToFile) != 0 {
Expand All @@ -86,17 +68,15 @@ func setCLIClient(_ *cobra.Command, _ []string) error {

if !cliCfg.validateOutputFormats() {
supportedOutputFormatsString := strings.Join(supportedOutputFormats, "|")
cliLogger.Errorf("unsupported output format '%s', the value should be one of %s",
cliCfg.OutputFormat, supportedOutputFormatsString)
errMsg := fmt.Sprintf("unsupported output format '%s', the value should be one of %s", cliCfg.OutputFormat, supportedOutputFormatsString)
cliLogger.Errorf(errMsg)

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

diffCfg = diff.Config{Format: cliCfg.OutputFormat}
diffCfg.SetLogger(cliLogger)
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"}}
Expand Down Expand Up @@ -126,3 +106,18 @@ func (cfg *Config) setOutputFormats() {
default:
}
}

func (cfg *Config) GetOutputFormat() string {
switch {
case cfg.yaml:
return "yaml"
case cfg.json:
return "json"
case cfg.table:
return "table"
case cfg.csv:
return "csv"
default:
return ""
}
}
4 changes: 2 additions & 2 deletions cmd/cluster_profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,11 @@ func deleteClusterProfileCommand() *cobra.Command {
if !cliCfg.Yes {
contains, option := cliShellReadConfig.Reader()
if !contains {
cliLogger.Fatalln("user input validation failed, cannot proceed further")
cliLogger.Fatalln(inputValidationFailureMessage)
}

if option.Short == "n" {
cliLogger.Warn("not proceeding further since 'no' was opted")
cliLogger.Warn(optingOutMessage)

os.Exit(0)
}
Expand Down
22 changes: 20 additions & 2 deletions cmd/config_repos.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,24 @@ func getUpdateConfigRepoCommand() *cobra.Command {
return &errors.UnknownObjectTypeError{Name: objType}
}

configRepoFetched, err := client.GetConfigRepo(configRepo.ID)
if err != nil {
return err
}

cliShellReadConfig.ShellMessage = fmt.Sprintf(updateMessage, "config-repo", configRepoFetched.ID)

existing, err := diffCfg.String(configRepoFetched)
if err != nil {
return err
}

if !cliCfg.Yes {
if err = CheckDiffAndAllow(existing, object.String()); err != nil {
return err
}
}

response, err := client.UpdateConfigRepo(configRepo)
if err != nil {
return err
Expand Down Expand Up @@ -394,11 +412,11 @@ func getDeleteConfigRepoCommand() *cobra.Command {
if !cliCfg.Yes {
contains, option := cliShellReadConfig.Reader()
if !contains {
cliLogger.Fatalln("user input validation failed, cannot proceed further")
cliLogger.Fatalln(inputValidationFailureMessage)
}

if option.Short == "n" {
cliLogger.Warn("not proceeding further since 'no' was opted")
cliLogger.Warn(optingOutMessage)

os.Exit(0)
}
Expand Down
6 changes: 6 additions & 0 deletions cmd/constants.go
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
package cmd

const (
updateMessage = "do you want to update '%s' '%s' [y/n]"
inputValidationFailureMessage = "user input validation failed, cannot proceed further"
optingOutMessage = "not proceeding further since 'no' was opted"
)
33 changes: 33 additions & 0 deletions cmd/diff.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package cmd

import (
"fmt"
"os"
)

func CheckDiffAndAllow(oldData, newData string) error {
hasDiff, diff, err := diffCfg.Diff(oldData, newData)
if err != nil {
return err
}

if !hasDiff {
cliLogger.Info("no changes to the input file, nothing to update, quitting")
os.Exit(0)
}

fmt.Printf("%s\n", diff)

contains, option := cliShellReadConfig.Reader()
if !contains {
cliLogger.Fatalln(inputValidationFailureMessage)
}

if option.Short == "n" {
cliLogger.Warn(optingOutMessage)

os.Exit(0)
}

return nil
}
4 changes: 2 additions & 2 deletions cmd/environments.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,11 +318,11 @@ func deleteEnvironmentCommand() *cobra.Command {
if !cliCfg.Yes {
contains, option := cliShellReadConfig.Reader()
if !contains {
cliLogger.Fatalln("user input validation failed, cannot proceed further")
cliLogger.Fatalln(inputValidationFailureMessage)
}

if option.Short == "n" {
cliLogger.Warn("not proceeding further since 'no' was opted")
cliLogger.Warn(optingOutMessage)

os.Exit(0)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/pipeline_groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,11 @@ func deletePipelineGroupCommand() *cobra.Command {
if !cliCfg.Yes {
contains, option := cliShellReadConfig.Reader()
if !contains {
cliLogger.Fatalln("user input validation failed, cannot proceed further")
cliLogger.Fatalln(inputValidationFailureMessage)
}

if option.Short == "n" {
cliLogger.Warn("not proceeding further since 'no' was opted")
cliLogger.Warn(optingOutMessage)

os.Exit(0)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/pipelines.go
Original file line number Diff line number Diff line change
Expand Up @@ -592,11 +592,11 @@ func deletePipelineCommand() *cobra.Command {
if !cliCfg.Yes {
contains, option := cliShellReadConfig.Reader()
if !contains {
cliLogger.Fatalln("user input validation failed, cannot proceed further")
cliLogger.Fatalln(inputValidationFailureMessage)
}

if option.Short == "n" {
cliLogger.Warn("not proceeding further since 'no' was opted")
cliLogger.Warn(optingOutMessage)

os.Exit(0)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/server_configurations.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,11 +377,11 @@ func deleteMailServerConfigCommand() *cobra.Command {
if !cliCfg.Yes {
contains, option := cliShellReadConfig.Reader()
if !contains {
cliLogger.Fatalln("user input validation failed, cannot proceed further")
cliLogger.Fatalln(inputValidationFailureMessage)
}

if option.Short == "n" {
cliLogger.Warn("not proceeding further since 'no' was opted")
cliLogger.Warn(optingOutMessage)

os.Exit(0)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,11 @@ func userDeleteCommand() *cobra.Command {
if !cliCfg.Yes {
contains, option := cliShellReadConfig.Reader()
if !contains {
cliLogger.Fatalln("user input validation failed, cannot proceed further")
cliLogger.Fatalln(inputValidationFailureMessage)
}

if option.Short == "n" {
cliLogger.Warn("not proceeding further since 'no' was opted")
cliLogger.Warn(optingOutMessage)

os.Exit(0)
}
Expand Down

0 comments on commit 1f5154d

Please sign in to comment.