Skip to content

Commit

Permalink
Add diff and word-diff format
Browse files Browse the repository at this point in the history
  • Loading branch information
aswinkarthik committed Feb 28, 2019
1 parent d78e953 commit 2e4d4e6
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 4 deletions.
1 change: 0 additions & 1 deletion cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ func (c *Config) GetIncludeColumnPositions() digest.Positions {
// Validate validates the config object
// and returns error if not valid.
func (c *Config) Validate() error {
allFormats := []string{rowmark, jsonFormat, diffFormat}

for _, format := range allFormats {
if strings.ToLower(c.Format) == format {
Expand Down
63 changes: 62 additions & 1 deletion cmd/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,21 @@ import (
"encoding/json"
"fmt"
"io"
"strings"

"github.com/aswinkarthik/csvdiff/pkg/digest"
"github.com/fatih/color"
)

const (
rowmark = "rowmark"
jsonFormat = "json"
diffFormat = "diff"
lineDiff = "diff"
wordDiff = "word-diff"
)

var allFormats = []string{rowmark, jsonFormat, lineDiff, wordDiff}

// Formatter can print the differences to stdout
// and accompanying metadata to stderr
type Formatter struct {
Expand All @@ -35,6 +40,10 @@ func (f *Formatter) Format(diff digest.Differences) error {
return f.json(diff)
case rowmark:
return f.rowMark(diff)
case lineDiff:
return f.lineDiff(diff)
case wordDiff:
return f.wordDiff(diff)
default:
return fmt.Errorf("formatter not found")
}
Expand Down Expand Up @@ -116,3 +125,55 @@ func (f *Formatter) rowMark(diff digest.Differences) error {

return nil
}

// lineDiff is git-style line diff
func (f *Formatter) lineDiff(diff digest.Differences) error {
includes := config.GetIncludeColumnPositions()

white := color.New(color.FgWhite).FprintfFunc()
red := color.New(color.FgRed).FprintfFunc()
green := color.New(color.FgGreen).FprintfFunc()

white(f.stderr, "# Additions (%d)\n", len(diff.Additions))
for _, addition := range diff.Additions {
green(f.stdout, "+ %s\n", includes.MapToValue(addition))
}
white(f.stderr, "# Modifications (%d)\n", len(diff.Modifications))
for _, modification := range diff.Modifications {
red(f.stdout, "- %s\n", includes.MapToValue(modification.Original))
green(f.stdout, "+ %s\n", includes.MapToValue(modification.Current))
}

return nil
}

// wordDiff is git-style word diff
func (f *Formatter) wordDiff(diff digest.Differences) error {
includes := config.GetIncludeColumnPositions()
if len(includes) <= 0 {
includes = config.GetValueColumns()
}

red := color.New(color.FgRed).SprintfFunc()
green := color.New(color.FgGreen).SprintfFunc()

for _, addition := range diff.Additions {
fmt.Fprintln(f.stdout, green("{+%s+}", includes.MapToValue(addition)))
}

for _, modification := range diff.Modifications {
result := make([]string, 0, len(modification.Current))
for i := 0; i < len(includes) || i < len(modification.Current); i++ {
if modification.Original[i] != modification.Current[i] {
removed := red("[-%s-]", modification.Original[i])
added := green("{+%s+}", modification.Current[i])
result = append(result, fmt.Sprintf("%s%s", removed, added))
} else {
result = append(result, modification.Current[i])
}
}
fmt.Fprintln(f.stdout, strings.Join(result, digest.Separator))
}

return nil
}
5 changes: 3 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"fmt"
"io"
"os"
"strings"
"time"

"github.com/aswinkarthik/csvdiff/pkg/digest"
Expand Down Expand Up @@ -77,7 +78,7 @@ Most suitable for csv files created from database tables`,
config.GetPrimaryKeys(),
config.GetValueColumns(),
config.GetIncludeColumnPositions(),
false,
true,
)
deltaConfig := digest.NewConfig(
deltaFile,
Expand Down Expand Up @@ -137,7 +138,7 @@ func init() {
rootCmd.Flags().IntSliceVarP(&config.PrimaryKeyPositions, "primary-key", "p", []int{0}, "Primary key positions of the Input CSV as comma separated values Eg: 1,2")
rootCmd.Flags().IntSliceVarP(&config.ValueColumnPositions, "columns", "", []int{}, "Selectively compare positions in CSV Eg: 1,2. Default is entire row")
rootCmd.Flags().IntSliceVarP(&config.IncludeColumnPositions, "include", "", []int{}, "Include positions in CSV to display Eg: 1,2. Default is entire row")
rootCmd.Flags().StringVarP(&config.Format, "format", "o", "rowmark", "Available (rowmark|json|diff)")
rootCmd.Flags().StringVarP(&config.Format, "format", "o", "rowmark", fmt.Sprintf("Available (%s)", strings.Join(allFormats, "|")))

rootCmd.Flags().BoolVarP(&timed, "time", "", false, "Measure time")
}
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ require (
github.com/blang/semver v3.5.1+incompatible
github.com/cespare/xxhash v1.0.0
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/fatih/color v1.7.0
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/mattn/go-colorable v0.1.1 // indirect
github.com/mattn/go-isatty v0.0.6 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72 // indirect
github.com/spf13/cobra v0.0.2
Expand Down
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,15 @@ github.com/cespare/xxhash v1.0.0 h1:naDmySfoNg0nKS62/ujM6e71ZgM2AoVdaqGwMG0w18A=
github.com/cespare/xxhash v1.0.0/go.mod h1:fX/lfQBkSCDXZSUgv6jVIu/EVA3/JNseAX5asI4c4T4=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/mattn/go-colorable v0.1.1 h1:G1f5SKeVxmagw/IyvzvtZE4Gybcc4Tr1tf7I8z0XgOg=
github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ=
github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.6 h1:SrwhHcpV4nWrMGdNcC2kXpMfcBVYGDuTArqyhocJgvA=
github.com/mattn/go-isatty v0.0.6/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
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/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72 h1:qLC7fQah7D6K1B0ujays3HV9gkFtllcxhzImRR7ArPQ=
Expand All @@ -18,3 +25,5 @@ github.com/spf13/pflag v1.0.1 h1:aCvUg6QPl3ibpQUxyLkrEkCHtPqYJL4x9AuhqVqFis4=
github.com/spf13/pflag v1.0.1/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/stretchr/testify v1.2.1 h1:52QO5WkIUcHGIR7EnGagH88x1bUzqGXTC5/1bDTUQ7U=
github.com/stretchr/testify v1.2.1/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223 h1:DH4skfRX4EBpamg7iV4ZlCpblAHI6s6TDM39bFZumv8=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=

0 comments on commit 2e4d4e6

Please sign in to comment.