From 8224ed68332730db8cedc8d45dd8256fffeb9c6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1ximo=20Cuadros?= Date: Sun, 10 May 2020 20:18:55 +0200 Subject: [PATCH] package documentation and cleanup --- LICENSE | 3 +- README.md | 100 ++++++++++++++++++++++++++++++------------------ doc.go | 3 +- example_test.go | 45 ++++++++++++++-------- go.mod | 2 +- go.sum | 16 +++++--- type.go | 20 ---------- vcsurl.go | 2 - 8 files changed, 109 insertions(+), 82 deletions(-) delete mode 100644 type.go diff --git a/LICENSE b/LICENSE index 278266c..f3c4ad8 100644 --- a/LICENSE +++ b/LICENSE @@ -1,3 +1,4 @@ +Copyright (c) 2020, Máximo Cuadros Copyright (c) 2013, Quinn Slack All rights reserved. @@ -5,4 +6,4 @@ Redistribution and use in source and binary forms, with or without modification, Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. \ No newline at end of file diff --git a/README.md b/README.md index 38d2e09..a8285f3 100644 --- a/README.md +++ b/README.md @@ -1,47 +1,73 @@ # go-vcsurl -go-vcsurl parses VCS repository URLs in many common formats. +`go-vcsurl` library provides a VCS URL parser for HTTP, git or ssh remote URLs +and also frontend URLs from providers like GitHub, GitLab, and Bitbucket. +This library is based on the previous work done by [@sourcegraph](https://github.com/gitsight/go-vcsurl). + +Installation +------------ + +The recommended way to install go-syslog + +``` +go get github.com/gitsight/go-vcsurl +``` Usage -===== +----- ```go -import ( - "fmt" - - "github.com/gitsight/go-vcsurl" -) - -func ExampleParse() { - urls := []string{ - "github.com/alice/libfoo", - "git://github.com/bob/libbar", - "code.google.com/p/libqux", - "https://code.google.com/p/libbaz", - } - for i, url := range urls { - if info, err := vcsurl.Parse(url); err == nil { - fmt.Printf("%d. %s %s\n", i+1, info.VCS, info.CloneURL) - fmt.Printf(" name: %s\n", info.Name) - fmt.Printf(" host: %s\n", info.Host) - } else { - fmt.Printf("error parsing %s\n", err) - } +urls := []string{ + "github.com/alice/libfoo", + "git://github.com/bob/libbar", + "https://gitlab.com/foo/bar", + "https://github.com/go-enry/go-enry/releases/tag/v2.4.1", +} + +for i, url := range urls { + info, err := vcsurl.Parse(url) + if err != nil { + fmt.Printf("error parsing %s\n", err) } - // output: - // 1. git git://github.com/alice/libfoo.git - // name: libfoo - // host: github.com - // 2. git git://github.com/bob/libbar.git - // name: libbar - // host: github.com - // 3. hg https://code.google.com/p/libqux - // name: libqux - // host: code.google.com - // 4. hg https://code.google.com/p/libbaz - // name: libbaz - // host: code.google.com + fmt.Printf("%d. %s %s\n", i+1, info.Kind, info.ID) + fmt.Printf(" name: %s\n", info.Name) + fmt.Printf(" host: %s\n", info.Host) + + remote, _ := info.Remote(vcsurl.SSH) + fmt.Printf(" remote: %s\n", remote) + + if info.Committish != "" { + fmt.Printf(" commit-ish: %s\n", info.Committish) + } } -``` \ No newline at end of file +``` + + +``` +1. git github.com/alice/libfoo + name: libfoo + host: github.com + remote: git@github.com/alice/libfoo.git +2. git github.com/bob/libbar + name: libbar + host: github.com + remote: git@github.com/bob/libbar.git +3. git gitlab.com/foo/bar + name: bar + host: gitlab.com + remote: git@gitlab.com/foo/bar.git +4. git github.com/go-enry/go-enry + name: go-enry + host: github.com + remote: git@github.com/go-enry/go-enry.git + commit-ish: v2.4.1 +``` + + + +License +------- + +MIT, see [LICENSE](LICENSE) \ No newline at end of file diff --git a/doc.go b/doc.go index 2908d78..6eca900 100644 --- a/doc.go +++ b/doc.go @@ -1,2 +1,3 @@ -// Package vcsurl parses VCS repository URLs in many common formats. +// Package vcsurl provides a VCS URL parser for HTTP, git or ssh remote URLs +// and also frontend URLs from providers like GitHub, GitLab, and Bitbucket. package vcsurl diff --git a/example_test.go b/example_test.go index 6781acd..e57199e 100644 --- a/example_test.go +++ b/example_test.go @@ -10,30 +10,45 @@ func ExampleParse() { urls := []string{ "github.com/alice/libfoo", "git://github.com/bob/libbar", - "code.google.com/p/libqux", - "https://code.google.com/p/libbaz", + "https://gitlab.com/foo/bar", + "https://github.com/go-enry/go-enry/releases/tag/v2.4.1", } + for i, url := range urls { - if info, err := vcsurl.Parse(url); err == nil { - fmt.Printf("%d. %s %s\n", i+1, info.VCS, info.CloneURL) - fmt.Printf(" name: %s\n", info.Name) - fmt.Printf(" host: %s\n", info.Host) - } else { + info, err := vcsurl.Parse(url) + if err != nil { fmt.Printf("error parsing %s\n", err) } + + fmt.Printf("%d. %s %s\n", i+1, info.Kind, info.ID) + fmt.Printf(" name: %s\n", info.Name) + fmt.Printf(" host: %s\n", info.Host) + + remote, _ := info.Remote(vcsurl.SSH) + fmt.Printf(" remote: %s\n", remote) + + if info.Committish != "" { + fmt.Printf(" commit-ish: %s\n", info.Committish) + } + } // output: - // 1. git git://github.com/alice/libfoo.git + // 1. git github.com/alice/libfoo // name: libfoo // host: github.com - // 2. git git://github.com/bob/libbar.git + // remote: git@github.com/alice/libfoo.git + // 2. git github.com/bob/libbar // name: libbar // host: github.com - // 3. hg https://code.google.com/p/libqux - // name: libqux - // host: code.google.com - // 4. hg https://code.google.com/p/libbaz - // name: libbaz - // host: code.google.com + // remote: git@github.com/bob/libbar.git + // 3. git gitlab.com/foo/bar + // name: bar + // host: gitlab.com + // remote: git@gitlab.com/foo/bar.git + // 4. git github.com/go-enry/go-enry + // name: go-enry + // host: github.com + // remote: git@github.com/go-enry/go-enry.git + // commit-ish: v2.4.1 } diff --git a/go.mod b/go.mod index 6054368..ef42fc1 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,4 @@ module github.com/gitsight/go-vcsurl go 1.14 -require github.com/kr/pretty v0.2.0 +require github.com/stretchr/testify v1.5.1 diff --git a/go.sum b/go.sum index 443873d..331fa69 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,11 @@ -github.com/kr/pretty v0.2.0 h1:s5hAObm+yFO5uHYt5dYjxi2rXrsnmRpJx4OYvIWUaQs= -github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= -github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= -github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= -github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +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/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= +github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/type.go b/type.go deleted file mode 100644 index 8db8e93..0000000 --- a/type.go +++ /dev/null @@ -1,20 +0,0 @@ -package vcsurl - -import ( - "database/sql/driver" - "fmt" -) - -// Scan implements database/sql.Scanner. -func (x *Kind) Scan(v interface{}) error { - if data, ok := v.([]byte); ok { - *x = Kind(data) - return nil - } - return fmt.Errorf("%T.Scan failed: %v", x, v) -} - -// Scan implements database/sql/driver.Valuer -func (x Kind) Value() (driver.Value, error) { - return string(x), nil -} diff --git a/vcsurl.go b/vcsurl.go index b83b8a5..5fff960 100644 --- a/vcsurl.go +++ b/vcsurl.go @@ -65,8 +65,6 @@ var kindByHost = map[Host]Kind{ type VCS struct { // ID unique repository identification. ID string - // CloneURL git remote format. - CloneURL string // Kind of VCS. Kind Kind // Host is the public web of the repository.