Skip to content

Commit

Permalink
Merge pull request #85 from k1LoW/change-diff-output
Browse files Browse the repository at this point in the history
Change `tbls diff` output to unified format
  • Loading branch information
k1LoW authored Mar 10, 2019
2 parents 5a2df42 + 9a0a43a commit e412fe8
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 16 deletions.
12 changes: 12 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"html/template"
"io/ioutil"
"net/url"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
47 changes: 32 additions & 15 deletions output/md/md.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit e412fe8

Please # to comment.