vcsurl

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 22, 2023 License: BSD-2-Clause Imports: 6 Imported by: 8

README

go-vcsurl GoDoc Test

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.

Installation

The recommended way to install go-vcsurl

go get github.com/gitsight/go-vcsurl

Usage

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)
	}

	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: 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

Documentation

Overview

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.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrUnknownURL          = errors.New("unknown URL format")
	ErrUnableParse         = errors.New("unable to determine name or full name")
	ErrEmptyURL            = errors.New("empty URL")
	ErrEmptyPath           = errors.New("empty path in URL")
	ErrUnknownProtocol     = errors.New("remote protocol should be SSH or HTTPS")
	ErrUnsupportedProtocol = errors.New("unsupported remote protocol")
)

Errors returned by Parse function.

Functions

This section is empty.

Types

type Host

type Host string

Host VCS provider.

const (
	GitHub    Host = "github.com"
	Bitbucket Host = "bitbucket.org"
	GitLab    Host = "gitlab.com"
)

Supported VCS host provider.

type Kind

type Kind string

Kind of VCS

const (
	Git Kind = "git"
)

Supported VCS kinds.

type Protocol

type Protocol string

Protocol of remote

const (
	SSH   Protocol = "ssh"
	HTTPS Protocol = "https"
)

Supported VCS protocols.

type VCS

type VCS struct {
	// ID unique repository identification.
	ID string
	// Kind of VCS.
	Kind Kind
	// Host is the public web of the repository.
	Host Host
	// Username of repo owner on repo hosting site.
	Username string
	// Name base name of repo on repo hosting site.
	Name string
	// FullName full name of repo on repo hosting site.
	FullName string
	// Committish is a reference to an object that can be recursively
	// dereferenced to a commit object. They can be commits, tags or branches.
	Committish string
	// Raw is the original parsed URL.
	Raw string
}

VCS describes a VCS repository.

func Parse

func Parse(raw string) (*VCS, error)

Parse parses a string that resembles a VCS repository URL. See TestParse for a list of supported URL formats.

Example
package main

import (
	"fmt"

	"github.com/gitsight/go-vcsurl"
)

func main() {
	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)
		}

		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 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

func (*VCS) Remote

func (v *VCS) Remote(p Protocol) (string, error)

Remote returns a remote URL in the given protocol. ErrUnsupportedProtocol is returned if the protocol it's not supported by the VCS.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL