From 9a0a43ad23614aaf87386b47cac1f610319d9e3b Mon Sep 17 00:00:00 2001 From: k1LoW Date: Sun, 10 Mar 2019 20:37:47 +0900 Subject: [PATCH] Change `tbls diff` output to unified diff --- config/config.go | 12 ++++++++++++ go.mod | 2 +- output/md/md.go | 47 ++++++++++++++++++++++++++++++++--------------- 3 files changed, 45 insertions(+), 16 deletions(-) diff --git a/config/config.go b/config/config.go index 928e94d67..d8bf0d0f7 100644 --- a/config/config.go +++ b/config/config.go @@ -4,6 +4,7 @@ import ( "bytes" "html/template" "io/ioutil" + "net/url" "os" "path/filepath" "regexp" @@ -214,6 +215,17 @@ func (c *Config) MergeAdditionalData(s *schema.Schema) error { return nil } +// MaskedDSN return DSN mask password +func (c *Config) MaskedDSN() (string, error) { + u, err := url.Parse(c.DSN) + if err != nil { + return c.DSN, errors.WithStack(err) + } + tmp := "-----tbls-----" + u.User = url.UserPassword(u.User.Username(), tmp) + return strings.Replace(u.String(), tmp, "*****", 1), nil +} + func mergeAdditionalRelations(s *schema.Schema, relations []AdditionalRelation) error { for _, r := range relations { relation := &schema.Relation{ diff --git a/go.mod b/go.mod index 6b72cdd2d..f9b5fea57 100644 --- a/go.mod +++ b/go.mod @@ -13,8 +13,8 @@ require ( github.com/mattn/go-runewidth v0.0.4 github.com/mattn/go-sqlite3 v1.10.0 github.com/pkg/errors v0.8.1 + github.com/pmezard/go-difflib v1.0.0 github.com/rogpeppe/go-internal v1.2.2 // indirect - github.com/sergi/go-diff v1.0.0 github.com/spf13/cobra v0.0.3 github.com/xo/dburl v0.0.0-20190203050942-98997a05b24f golang.org/x/sys v0.0.0-20190308023053-584f3b12f43e // indirect diff --git a/output/md/md.go b/output/md/md.go index 49af169d3..08befb90f 100644 --- a/output/md/md.go +++ b/output/md/md.go @@ -15,7 +15,7 @@ import ( "github.com/k1LoW/tbls/schema" "github.com/mattn/go-runewidth" "github.com/pkg/errors" - "github.com/sergi/go-diff/diffmatchpatch" + "github.com/pmezard/go-difflib/difflib" ) // Md struct @@ -144,8 +144,6 @@ func Diff(s *schema.Schema, c *config.Config) (string, error) { return "", errors.New("target files does not exists") } - dmp := diffmatchpatch.New() - // README.md a := new(bytes.Buffer) er := false @@ -166,13 +164,24 @@ func Diff(s *schema.Schema, c *config.Config) (string, error) { b = []byte{} } - da, db, dc := dmp.DiffLinesToChars(a.String(), string(b)) - diffs := dmp.DiffMain(da, db, false) - result := dmp.DiffCharsToLines(diffs, dc) + from, err := c.MaskedDSN() + if err != nil { + return "", errors.WithStack(err) + } + to := filepath.Join(docPath, "README.md") - if len(result) != 1 || result[0].Type != diffmatchpatch.DiffEqual { - diff += fmt.Sprintf("diff [database] %s\n", filepath.Join(docPath, "README.md")) - diff += fmt.Sprintln(dmp.DiffPrettyText(result)) + d := difflib.UnifiedDiff{ + A: difflib.SplitLines(a.String()), + B: difflib.SplitLines(string(b)), + FromFile: from, + ToFile: to, + Context: 3, + } + + text, _ := difflib.GetUnifiedDiffString(d) + if text != "" { + diff += fmt.Sprintf("diff %s %s\n", from, to) + diff += text } // tables @@ -195,12 +204,20 @@ func Diff(s *schema.Schema, c *config.Config) (string, error) { b = []byte{} } - da, db, dc := dmp.DiffLinesToChars(a.String(), string(b)) - diffs := dmp.DiffMain(da, db, false) - result := dmp.DiffCharsToLines(diffs, dc) - if len(result) != 1 || result[0].Type != diffmatchpatch.DiffEqual { - diff += fmt.Sprintf("diff %s %s\n", t.Name, filepath.Join(docPath, fmt.Sprintf("%s.md", t.Name))) - diff += fmt.Sprintln(dmp.DiffPrettyText(result)) + to := filepath.Join(docPath, fmt.Sprintf("%s.md", t.Name)) + + d := difflib.UnifiedDiff{ + A: difflib.SplitLines(a.String()), + B: difflib.SplitLines(string(b)), + FromFile: from, + ToFile: to, + Context: 3, + } + + text, _ := difflib.GetUnifiedDiffString(d) + if text != "" { + diff += fmt.Sprintf("diff %s %s\n", from, to) + diff += text } } return diff, nil