Skip to content

Commit

Permalink
package documentation and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mcuadros committed May 10, 2020
1 parent 75bb110 commit 8224ed6
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 82 deletions.
3 changes: 2 additions & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
Copyright (c) 2020, Máximo Cuadros
Copyright (c) 2013, Quinn Slack
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

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.
100 changes: 63 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)
}
}
```
```


```
1. git github.com/alice/libfoo
name: libfoo
host: github.com
remote: [email protected]/alice/libfoo.git
2. git github.com/bob/libbar
name: libbar
host: github.com
remote: [email protected]/bob/libbar.git
3. git gitlab.com/foo/bar
name: bar
host: gitlab.com
remote: [email protected]/foo/bar.git
4. git github.com/go-enry/go-enry
name: go-enry
host: github.com
remote: [email protected]/go-enry/go-enry.git
commit-ish: v2.4.1
```



License
-------

MIT, see [LICENSE](LICENSE)
3 changes: 2 additions & 1 deletion doc.go
Original file line number Diff line number Diff line change
@@ -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
45 changes: 30 additions & 15 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: [email protected]/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: [email protected]/bob/libbar.git
// 3. git gitlab.com/foo/bar
// name: bar
// host: gitlab.com
// remote: [email protected]/foo/bar.git
// 4. git github.com/go-enry/go-enry
// name: go-enry
// host: github.com
// remote: [email protected]/go-enry/go-enry.git
// commit-ish: v2.4.1
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 11 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
20 changes: 0 additions & 20 deletions type.go

This file was deleted.

2 changes: 0 additions & 2 deletions vcsurl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 8224ed6

Please sign in to comment.