vcslocator

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2026 License: Apache-2.0 Imports: 24 Imported by: 4

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:


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

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 (Locator) Parse

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

Parse a VCS locator and returns its components

Jump to

Keyboard shortcuts

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