vcslocator

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2025 License: Apache-2.0 Imports: 17 Imported by: 1

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"
}
*/

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

Index

Constants

This section is empty.

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 Download

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

Download copies data from the git repository to the specified directory

func WithClonePath

func WithClonePath(path string) fnOpt

WithClonePath specifies the directory to clone the repository. When

func WithRefAsBranch

func WithRefAsBranch(sino bool) fnOpt

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

Types

type Components

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

func (*Components) RepoURL

func (c *Components) RepoURL() string

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

type Locator

type Locator string

func (Locator) Parse

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

Parses a VCS locator and returns its components

type Options

type Options struct {
	RefIsBranch bool
	ClonePath   string
}

Jump to

Keyboard shortcuts

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