diff --git a/cmd/client.go b/cmd/client.go index 3a27fdd..0748a71 100644 --- a/cmd/client.go +++ b/cmd/client.go @@ -7,8 +7,8 @@ import ( "strings" "github.com/ghodss/yaml" + "github.com/nikhilsbhat/common/diff" "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" @@ -20,7 +20,7 @@ var ( client gocd.GoCd cliRenderer renderer.Config cliShellReadConfig *utils.ReadConfig - diffCfg diff.Config + diffCfg *diff.Config supportedOutputFormats = []string{"yaml", "json", "csv", "table"} ) @@ -74,8 +74,8 @@ func setCLIClient(_ *cobra.Command, _ []string) error { return &errors.CLIError{Message: errMsg} } - diffCfg = diff.Config{Format: cliCfg.OutputFormat, NoColor: cliCfg.NoColor} - diffCfg.SetLogger(cliLogger) + diffCfg = diff.NewDiff(cliCfg.OutputFormat, cliCfg.NoColor, cliLogger) + cliCfg.setOutputFormats() cliRenderer = renderer.GetRenderer(writer, cliLogger, cliCfg.NoColor, cliCfg.yaml, cliCfg.json, cliCfg.csv, cliCfg.table) diff --git a/cmd/diff.go b/cmd/diff.go index 0743e77..63d6642 100644 --- a/cmd/diff.go +++ b/cmd/diff.go @@ -6,7 +6,7 @@ import ( ) func (cfg *Config) CheckDiffAndAllow(oldData, newData string) error { - hasDiff, diff, err := diffCfg.Diff(oldData, newData) + hasDiff, diffIdentified, err := diffCfg.Diff(oldData, newData) if err != nil { return err } @@ -16,7 +16,7 @@ func (cfg *Config) CheckDiffAndAllow(oldData, newData string) error { os.Exit(0) } - fmt.Printf("%s\n", diff) + fmt.Printf("%s\n", diffIdentified) fmt.Printf("%s\n\n", "Above changes would be deployed") if !cfg.Yes { diff --git a/go.mod b/go.mod index 600b6d6..2aa17ba 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( github.com/fatih/color v1.16.0 github.com/ghodss/yaml v1.0.0 github.com/goccy/go-yaml v1.11.3 - github.com/nikhilsbhat/common v0.0.5-0.20240330150338-6dad4cae0c57 + github.com/nikhilsbhat/common v0.0.5-0.20240331064442-d5f43095b010 github.com/nikhilsbhat/gocd-sdk-go v0.1.9 github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 github.com/sirupsen/logrus v1.9.3 diff --git a/go.sum b/go.sum index 4490873..8ae86a5 100644 --- a/go.sum +++ b/go.sum @@ -46,8 +46,8 @@ github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/Qd github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= github.com/neilotoole/jsoncolor v0.7.1 h1:/MoU7KPLcto+ykcy592Y8eX9WFQhoi3IBEbwrP89dgs= github.com/neilotoole/jsoncolor v0.7.1/go.mod h1:KZ9hUYN5xMrvyhqlFQ3QTmu11OcoqFgSnWAcYkN6abg= -github.com/nikhilsbhat/common v0.0.5-0.20240330150338-6dad4cae0c57 h1:6BYXfUsKO7xwzfA5a2xtY14i90xqiHLCJDV0epRHJU8= -github.com/nikhilsbhat/common v0.0.5-0.20240330150338-6dad4cae0c57/go.mod h1:ldoLjRsIaskDbouPaQ0ujVKbW8su3mI1MPQIaD24twQ= +github.com/nikhilsbhat/common v0.0.5-0.20240331064442-d5f43095b010 h1:TuR/lpOOwNt/uqJp/uhxT+K4MNZkHrbo0NMZSSKyX5c= +github.com/nikhilsbhat/common v0.0.5-0.20240331064442-d5f43095b010/go.mod h1:ldoLjRsIaskDbouPaQ0ujVKbW8su3mI1MPQIaD24twQ= github.com/nikhilsbhat/gocd-sdk-go v0.1.9 h1:QG16EwKhxocn0Imza6S+/lE8S0MOw54UDebfJotQx2s= github.com/nikhilsbhat/gocd-sdk-go v0.1.9/go.mod h1:FFVOLXPhn55evuuyfiRJ23R8I5Ks2n31/DXLBQmaNsw= github.com/nwidger/jsoncolor v0.3.2 h1:rVJJlwAWDJShnbTYOQ5RM7yTA20INyKXlJ/fg4JMhHQ= diff --git a/pkg/diff/diff.go b/pkg/diff/diff.go deleted file mode 100644 index 7615f98..0000000 --- a/pkg/diff/diff.go +++ /dev/null @@ -1,108 +0,0 @@ -package diff - -import ( - "encoding/json" - "fmt" - "strings" - - "github.com/fatih/color" - "github.com/ghodss/yaml" - "github.com/nikhilsbhat/gocd-cli/pkg/errors" - "github.com/pmezard/go-difflib/difflib" - "github.com/sirupsen/logrus" -) - -const ( - contextLines = 2000000 -) - -type Config struct { - NoColor bool - Format string - log *logrus.Logger -} - -func (cfg *Config) Diff(oldData, newData string) (bool, string, error) { - switch cfg.Format { - case "yaml": - cfg.log.Debug("loading diff in yaml format") - case "json": - cfg.log.Debug("loading diff in json format") - default: - return false, "", &errors.CLIError{Message: fmt.Sprintf("unknown format, cannot calculate diff for the format '%s'", cfg.Format)} - } - - diffIdentified, err := cfg.diff(oldData, newData) - if err != nil { - return false, "", err - } - - if len(diffIdentified) == 0 { - return false, "", nil - } - - return true, strings.Join(diffIdentified, "\n"), nil -} - -func (cfg *Config) String(input interface{}) (string, error) { - switch strings.ToLower(cfg.Format) { - case "yaml": - out, err := yaml.Marshal(input) - if err != nil { - return "", err - } - - yamlString := strings.Join([]string{"---", string(out)}, "\n") - - return yamlString, nil - case "json": - out, err := json.MarshalIndent(input, "", " ") - if err != nil { - return "", err - } - - return string(out), nil - default: - return "", &errors.CLIError{ - Message: fmt.Sprintf("type '%s' is not supported for loading diff", cfg.Format), - } - } -} - -func (cfg *Config) SetLogger(log *logrus.Logger) { - cfg.log = log -} - -func (cfg *Config) diff(content1, content2 string) ([]string, error) { - lines := make([]string, 0) - diffVal := difflib.UnifiedDiff{ - A: difflib.SplitLines(content1), - B: difflib.SplitLines(content2), - FromFile: "old", - ToFile: "new", - Context: contextLines, - } - - text, err := difflib.GetUnifiedDiffString(diffVal) - if err != nil { - return nil, err - } - - if len(text) == 0 { - return lines, nil - } - - lines = strings.Split(text, "\n") - for index, line := range lines { - if !cfg.NoColor { - switch { - case strings.HasPrefix(line, "-"): - lines[index] = color.RedString(line) - case strings.HasPrefix(line, "+"): - lines[index] = color.GreenString(line) - } - } - } - - return lines, nil -}