Skip to content

Commit

Permalink
Merge pull request #4 from semihbkgr/colored-output
Browse files Browse the repository at this point in the history
Colored output
  • Loading branch information
semihbkgr authored Jan 6, 2024
2 parents dac806a + 6fb5824 commit 9460720
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 13 deletions.
4 changes: 3 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func Execute() {
}

var exitOnDifference = false
var plainOutput = false
var diffConfig = diff.DefaultDiffConfig

func run(cmd *cobra.Command, args []string) error {
Expand All @@ -38,7 +39,7 @@ func run(cmd *cobra.Command, args []string) error {
}

diffs := diffCtx.Diffs(diffConfig)
fmt.Fprintf(cmd.OutOrStdout(), "%s", diffs)
fmt.Fprintf(cmd.OutOrStdout(), "%s", diffs.OutputString(!plainOutput))

if exitOnDifference && diffs.HasDifference() {
return errors.New("yaml files have difference(s)")
Expand All @@ -50,6 +51,7 @@ func run(cmd *cobra.Command, args []string) error {
func init() {
rootCmd.Flags().BoolVarP(&exitOnDifference, "exit", "e", false, "returns non-zero exit status if there is a difference between yaml files")
rootCmd.Flags().BoolVarP(&diffConfig.IgnoreIndex, "ignore", "i", diffConfig.IgnoreIndex, "ignore indexes in array")
rootCmd.Flags().BoolVarP(&plainOutput, "plain", "p", plainOutput, "uncolored output")
}

// buildVersion is set by ldflags
Expand Down
34 changes: 26 additions & 8 deletions diff/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"sort"
"strings"

"github.com/fatih/color"
"github.com/goccy/go-yaml/ast"
"github.com/goccy/go-yaml/parser"
)
Expand Down Expand Up @@ -241,24 +242,41 @@ func ignoreIndexes(diffs []*Diff, conf *DiffConfig) []*Diff {
return resultDiffs
}

func (d DocDiffs) String() string {
func (d DocDiffs) OutputString(colored bool) string {
b := strings.Builder{}
for _, diff := range d {
if diff.NodeLeft == nil { // Added
sign := "+"
path := nodePathString(diff.NodeRight)
nodeType := diff.NodeRight.Type()
value := nodeValueString(diff.NodeRight)
b.WriteString(fmt.Sprintf("+ %s: <%s> %s", path, nodeType, value))
if colored {
sign = color.HiGreenString(sign)
path = color.HiGreenString(path)
value = color.HiWhiteString(value)
}
b.WriteString(fmt.Sprintf("%s %s: %s", sign, path, value))
} else if diff.NodeRight == nil { //Deleted
sign := "-"
path := nodePathString(diff.NodeLeft)
nodeType := diff.NodeLeft.Type()
value := nodeValueString(diff.NodeLeft)
b.WriteString(fmt.Sprintf("- %s: <%s> %s", path, nodeType, value))
if colored {
sign = color.HiRedString(sign)
path = color.HiRedString(path)
value = color.HiWhiteString(value)
}
b.WriteString(fmt.Sprintf("%s %s: %s", sign, path, value))
} else { //Modified
sign := "~"
path := nodePathString(diff.NodeLeft)
leftValue := nodeValueString(diff.NodeLeft)
rightValue := nodeValueString(diff.NodeRight)
b.WriteString(fmt.Sprintf("~ %s: <%s> %s -> <%s> %s", path, diff.NodeLeft.Type(), leftValue, diff.NodeRight.Type(), rightValue))
if colored {
sign = color.HiYellowString(sign)
path = color.HiYellowString(path)
leftValue = color.HiWhiteString(leftValue)
rightValue = color.HiWhiteString(rightValue)
}
b.WriteString(fmt.Sprintf("%s %s: %s -> %s", sign, path, leftValue, rightValue))
}
b.WriteRune('\n')
}
Expand Down Expand Up @@ -294,10 +312,10 @@ func nodeValueString(n ast.Node) string {

type FileDiffs []DocDiffs

func (d FileDiffs) String() string {
func (d FileDiffs) OutputString(colored bool) string {
docDiffsStrings := make([]string, 0, len(d))
for _, docDiffs := range d {
docDiffsStrings = append(docDiffsStrings, docDiffs.String())
docDiffsStrings = append(docDiffsStrings, docDiffs.OutputString(colored))
}
return strings.Join(docDiffsStrings, "\n---\n")
}
Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ require (

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fatih/color v1.10.0 // indirect
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/mattn/go-isatty v0.0.18 // indirect
github.com/fatih/color v1.16.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/sys v0.14.0 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Expand Down
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,22 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.10.0 h1:s36xzo75JdqLaaWoiEHk767eHiwo0598uUxyfiPkDsg=
github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM=
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
github.com/goccy/go-yaml v1.11.2 h1:joq77SxuyIs9zzxEjgyLBugMQ9NEgTWxXfz2wVqwAaQ=
github.com/goccy/go-yaml v1.11.2/go.mod h1:wKnAMd44+9JAAnGQpWVEgBzGt3YuTaQ4uXoHvE4m7WU=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8=
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98=
github.com/mattn/go-isatty v0.0.18/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
Expand All @@ -23,9 +30,12 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
Expand Down

0 comments on commit 9460720

Please sign in to comment.