diff options
| -rw-r--r-- | common.go | 50 | ||||
| -rw-r--r-- | common_test.go | 43 | ||||
| -rw-r--r-- | env.go | 22 |
3 files changed, 108 insertions, 7 deletions
@@ -15,6 +15,56 @@ import ( ) // +// GetCompareURL return the URL that compare two versions of package from +// given remote URL. Remote URL can be in git format +// ("git@github.com:<username>/<reponame>") or in HTTP format. +// +// On package that hosted on Github, the compare URL format is, +// +// https://github.com/<username>/<reponame>/compare/<old-version>...<new-version> +// +func GetCompareURL(remoteURL, oldVer, newVer string) (url string) { + if len(remoteURL) == 0 { + return + } + + remoteURL = strings.TrimPrefix(remoteURL, "git@") + remoteURL = strings.TrimPrefix(remoteURL, "https://") + remoteURL = strings.TrimPrefix(remoteURL, "www.") + remoteURL = strings.TrimSuffix(remoteURL, ".git") + + var host, user, repo string + + colIdx := strings.IndexByte(remoteURL, ':') + if colIdx > 0 { + names := strings.Split(remoteURL[colIdx+1:], "/") + + host = remoteURL[0:colIdx] + user = names[0] + repo = names[len(names)-1] + } else { + names := strings.Split(remoteURL, "/") + if len(names) < 3 { + return + } + host = names[0] + user = names[1] + repo = names[len(names)-1] + } + + switch host { + case "github.com": + url = fmt.Sprintf("https://%s/%s/%s/compare/%s...%s", host, + user, repo, oldVer, newVer) + case "golang.org": + url = fmt.Sprintf("https://github.com/golang/%s/compare/%s...%s", + repo, oldVer, newVer) + } + + return +} + +// // IsDirEmpty will return true if directory is not exist or empty; otherwise // it will return false. // diff --git a/common_test.go b/common_test.go index 5080a37..ffdc5c5 100644 --- a/common_test.go +++ b/common_test.go @@ -9,6 +9,49 @@ import ( "github.com/shuLhan/share/lib/test" ) +func TestGetCompareURL(t *testing.T) { + cases := []struct { + desc string + remoteURL string + oldVer string + newVer string + exp string + }{{ + desc: "With empty remoteURL", + }, { + desc: "With git format", + remoteURL: "git@github.com:shuLhan/beku.git", + oldVer: "A", + newVer: "B", + exp: "https://github.com/shuLhan/beku/compare/A...B", + }, { + desc: "With HTTP format", + remoteURL: "https://github.com/shuLhan/beku", + oldVer: "A", + newVer: "B", + exp: "https://github.com/shuLhan/beku/compare/A...B", + }, { + desc: "With golang.org as hostname", + remoteURL: "https://golang.org/x/net", + oldVer: "A", + newVer: "B", + exp: "https://github.com/golang/net/compare/A...B", + }, { + desc: "With unknown hostname", + remoteURL: "https://gopkg.in/yaml.v2", + oldVer: "A", + newVer: "B", + }} + + for _, c := range cases { + t.Log(c.desc) + + got := GetCompareURL(c.remoteURL, c.oldVer, c.newVer) + + test.Assert(t, "", c.exp, got, true) + } +} + func TestIsDirEmpty(t *testing.T) { emptyDir := "testdata/dirempty" err := os.MkdirAll(emptyDir, 0700) @@ -832,9 +832,10 @@ func (env *Env) SyncAll() (err error) { buf bytes.Buffer ) - format := fmt.Sprintf("%%-%ds %%-12s %%-12s\n", env.fmtMaxPath) + format := fmt.Sprintf("%%-%ds %%-12s %%-12s %%s\n", env.fmtMaxPath) fmt.Fprintf(&buf, ">>> The following packages will be updated,\n\n") - fmt.Fprintf(&buf, format+"\n", "ImportPath", "Old Version", "New Version") + fmt.Fprintf(&buf, format+"\n", "ImportPath", "Old Version", + "New Version", "Compare URL") fmt.Println(">>> Updating all packages ...") @@ -844,13 +845,20 @@ func (env *Env) SyncAll() (err error) { if err != nil { return } - if pkg.Version != pkg.VersionNext { - fmt.Printf(">>> Latest version is %s\n", pkg.VersionNext) - fmt.Fprintf(&buf, format, pkg.ImportPath, pkg.Version, pkg.VersionNext) - countUpdate++ - } else { + if pkg.Version == pkg.VersionNext { fmt.Println(">>> No update.") + continue } + + fmt.Printf(">>> Latest version is %s\n", pkg.VersionNext) + + compareURL := GetCompareURL(pkg.RemoteURL, pkg.Version, + pkg.VersionNext) + + fmt.Fprintf(&buf, format, pkg.ImportPath, pkg.Version, + pkg.VersionNext, compareURL) + + countUpdate++ } if countUpdate == 0 { |
