vcslocator

package module
v0.4.3 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2026 License: Apache-2.0 Imports: 24 Imported by: 5

README

vcslocator

A library to work with SPDX VCS locator that parses and downloads data references by locator URIs.

Intro

This library lets go programs work with SPDX VCS locators. These are specially crafted URI that point to content hosted in Version Control Systems.

For reference see table 19 in the SPDX standard documentation.

Features:

At the moment the focus of this module is parsing the locator strings and downloading data from repositories as referenced by the URIs.

Parsing

The library includes a parser that returns the components of the VCS locator:


// Create a new locator:
l = vcslocator.Locator("git+https://github.com/example/test@v1#filename.txt")

// Parse the locator:
components, err = l.Parse()
if err != nil {
    fmt.Fprintf(os.Stderr, "Error copying file data: %s\n", err.Error())
    os.Exit(1)
}

fmt.Printf("%+v\n",  components)

/*
{
    Tool      "git"
	Transport "https"
	Hostname  "github.com"
	RepoPath  "/example/test"
	RefString "v1"
	Commit    "
	Tag       "v1"
	Branch    ""
	SubPath   "filename.txt"
}
*/
Support for Short GitHub Repository "Slugs"

While not part of the specification, short repository slugs in the form of owner/repository are supported as VCS locators. For now we default the hostname of the vcs locator to github.com and the locator schema to git+https.

For example this short slug:

myorg/myrepo

Is equivalente to this VCS locator:

git+https://github.com/myorg/myrepo

Refrence strings and subpaths are fully supported in short slugs too.

Download and Copy

The library also supports copying and downloading the data referenced by the VCS locator (supported for ssh, http and file transports):


// This VCS locator points to the readme file of Kubernetes at the latest commit:
var filelocator = "git+https://https://github.com/kubernetes/kubernetes#README.md"

// Copy the README data to STDOUT:
if err := vcslocator.CopyFile(filelocator, Stdout); err != nil {
    fmt.Fprintf(os.Stderr, "Error copying file data: %s\n", err.Error())
    os.Exit(1)
}

// This VCS locator points to the go/predicates directory in the in-toto/attestation
// repository in GitHub at commit 159ab7123302f32caeae1b8ac99a2e465ae733f8:
var dirlocator = "git+https://github.com/in-toto/attestation@159ab7123302f32caeae1b8ac99a2e465ae733f8#go/predicates"

// Mirror the contents of the directory to mydir:
if err := vcslocator.Download(dirlocator, "mydir/"); err != nil {
    fmt.Fprintf(os.Stderr, "Error downloading data: %s\n", err.Error())
    os.Exit(1)
}

Install

To install simply go get the module into your project:

go get github.com/carabiner-dev/vcslocator

This module is released under the Apache 2.0 license and copyright by Carabiner Systems, Inc. Feel free to open issues, send pull requests to improve the module os simply let us know how you are using it. We love feedback!

Documentation

Overview

Package vcslocator offers functions and tools to parse SPDX VCS locator strings and access data referenced by them.

Index

Constants

View Source
const (

	// Supported transport strings
	TransportSSH   = "ssh"
	TransportHTTPS = "https"
	TransportFile  = "file"

	ToolGit = "git"
)

Variables

This section is empty.

Functions

func CloneRepository

func CloneRepository[T ~string](locator T, funcs ...fnOpt) (fs.FS, error)

CloneRepository clones the repository defined by the locator to a path.

func CopyFile

func CopyFile[T ~string](locator T, w io.Writer, funcs ...fnOpt) error

CopyFile downloads a file specified by the VCS locator and copies it to an io.Writer.

func CopyFileGroup added in v0.2.0

func CopyFileGroup[T ~string](locators []T, writers []io.Writer, funcs ...fnOpt) error

CopyFileGroup copies a group of locators to the specified writers

func Download

func Download[T ~string](locator T, localDir string, funcs ...fnOpt) error

Download copies data from the git repository to the specified directory

func GetAuthMethod added in v0.4.0

func GetAuthMethod[T ~string](locator T, funcs ...fnOpt) (transport.AuthMethod, error)

getAuthMethod returns an appropriate auth method based on the transport type and available credentials.

It mimics git's behavior by automatically detecting and using SSH keys, SSH agent, or configuring http credentials from the options.

func GetGroup added in v0.2.0

func GetGroup[T ~string](locators []T) ([][]byte, error)

GetGroup gets the data of several vcs locators in an efficient manner

func WithClonePath

func WithClonePath(path string) fnOpt

WithClonePath specifies the directory to clone the repository. When

func WithHttpAuth added in v0.4.0

func WithHttpAuth(user, password string) fnOpt

WithHttpAuth configures basic authentication for http operations

func WithRefAsBranch

func WithRefAsBranch(sino bool) fnOpt

WithRefAsBranch instructs the parser to treat the ref as branch name instead of a tag name.

func WithSystemCredentials added in v0.4.0

func WithSystemCredentials(yesno bool) fnOpt

WithSystemCredentials controls if cloning uses the system credentials

func WithTopLevelPath added in v0.4.1

func WithTopLevelPath(path string) fnOpt

WithTopLevelPath sets the uppermost directory the repository search will walk up to. The path must be a parent of the starting directory.

Types

type Components

type Components struct {
	Tool      string
	Transport string
	Hostname  string
	RepoPath  string
	RefString string
	Commit    string
	Tag       string
	Branch    string
	SubPath   string
}

Components captures the parsed pieces of a VCS locator.

func (*Components) RepoURL

func (c *Components) RepoURL() string

RepoURL forms the repository URL to clone based on the defined components

type ErrorList added in v0.2.0

type ErrorList struct {
	Errors []error
}

func (*ErrorList) Error added in v0.2.0

func (el *ErrorList) Error() string

type Locator

type Locator string

Locator is a type that wraps a VCS locator string to add functionality to it.

func NewFromPath added in v0.4.3

func NewFromPath(path string) Locator

NewFromPath builds a file:// Locator from a native filesystem path. It is the inverse of Locator.LocalPath: backslashes are normalized to forward slashes, and Windows drive-letter paths (e.g. "C:\foo\bar") are prefixed with an extra slash so the drive letter isn't parsed as a URL scheme (file:///C:/foo/bar). The input is treated as a pure filesystem path; "@ref" or "#subpath" suffixes are not interpreted.

func ReadFromRepo added in v0.4.1

func ReadFromRepo(startDir string, funcs ...fnOpt) (Locator, error)

ReadFromRepo opens a git repository by walking up from startDir toward the filesystem root (or the directory set via WithTopLevelPath) and returns a VCS Locator built from the repository's origin remote URL and current HEAD.

func (Locator) LocalPath added in v0.4.3

func (l Locator) LocalPath(funcs ...fnOpt) (string, error)

LocalPath returns the native filesystem path for a file:// locator, suitable for passing to os.Open, go-git's PlainOpen, and other local-path APIs. On Windows the parsed path takes the URL form "/C:/foo/bar"; this returns "C:/foo/bar" so the path isn't misread as drive-relative. For locators whose transport is not "file" the return value is an empty string. Returns an error only if the locator fails to parse.

func (Locator) Parse

func (l Locator) Parse(funcs ...fnOpt) (*Components, error)

Parse a VCS locator and returns its components

Directories

Path Synopsis
Demo program showing how to use vcslocator.Download to clone and extract files from a VCS locator string.
Demo program showing how to use vcslocator.Download to clone and extract files from a VCS locator string.

Jump to

Keyboard shortcuts

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