uriget

package
v1.22.0 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2026 License: Apache-2.0 Imports: 19 Imported by: 1 module, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package uriget provides a mechanism for "loading" the contents of a file from a uri with flexible support for different uri schemes. This is primarily used for loading Score provisioner files for various Score implementations. There are similar packages such as hashicorp/go-getter, however this version is maintained to have zero dependencies and use the binaries that exist on the local system where possible.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetFile deprecated

func GetFile(ctx context.Context, rawUri string, optionFuncs ...Option) ([]byte, error)

GetFile attempts to parse and retrieve file contents from the given url/uri. The scheme is used to inform what sources are supported and how the remainder of the url should be handled. Supported schemes: - http/https: reads the file using http. - file or no scheme: attempts to read the file from local file system. - git-ssh / git-https: attempts to perform a sparse checkout of just the target file. - oci: retrieves a file from a remote OCI registry based on the reference and optional fragment.

Deprecated: Use GetFiles instead, which supports both single files and directories.

Example (Git)
buff, err := GetFile(context.Background(), "git-https://github.com/score-spec/score.dev.git/README.md")
fmt.Println(len(buff) > 0, err)
Output:
true <nil>
Example (Http)
buff, err := GetFile(context.Background(), "http://example.com")
fmt.Println(len(buff) > 0, err)
Output:
true <nil>
Example (Local)
buff, err := GetFile(context.Background(), "../README.md")
fmt.Println(len(buff) > 0, err)
_, err = GetFile(context.Background(), "./does/not/exist.txt")
fmt.Println(err)
Output:
true <nil>
open ./does/not/exist.txt: no such file or directory
Example (Oci)
testUrl := "oci://ghcr.io/score-spec/score-compose-community-provisioners:latest#10-service.provisioners.yaml"
buff, err := GetFile(context.Background(), testUrl)
if err != nil {
	fmt.Println("failed to pull OCI image:", err)
	return
}
fmt.Println(len(buff) > 0)
Output:
true
Example (Oci_git)
ociTestUrl := "oci://ghcr.io/score-spec/score-compose-community-provisioners:latest#10-service.provisioners.yaml"
ociBuff, err := GetFile(context.Background(), ociTestUrl)
if err != nil {
	fmt.Println("failed to pull OCI image:", err)
	return
}
gitTestUrl := "git-https://github.com/score-spec/community-provisioners.git/service/score-compose/10-service.provisioners.yaml"
gitBuff, err := GetFile(context.Background(), gitTestUrl)
if err != nil {
	fmt.Println("failed to pull file in git:", err)
	return
}
fmt.Println(len(ociBuff) > 0 && len(gitBuff) > 0)
Output:
true
Example (Oci_https)
ociTestUrl := "oci://ghcr.io/score-spec/score-compose-community-provisioners:latest#10-service.provisioners.yaml"
ociBuff, err := GetFile(context.Background(), ociTestUrl)
if err != nil {
	fmt.Println("failed to pull OCI image:", err)
	return
}
httpsTestUrl := "https://github.com/score-spec/community-provisioners/raw/main/service/score-compose/10-service.provisioners.yaml"
httpsbuff, err := GetFile(context.Background(), httpsTestUrl)
if err != nil {
	fmt.Println("failed to pull file by HTTPS:", err)
	return
}
fmt.Println(len(ociBuff) > 0 && len(httpsbuff) > 0)
Output:
true

Types

type FileContent added in v1.15.0

type FileContent struct {
	// URI is the URI or path of the file.
	URI string
	// Content is the raw bytes of the file.
	Content []byte
}

FileContent holds the URI and content of a file retrieved by GetFiles.

func GetFiles added in v1.15.0

func GetFiles(ctx context.Context, rawUri string, optionFuncs ...Option) ([]FileContent, error)

GetFiles is like GetFile but with support for importing multiple files from a directory. Currently, directory support is implemented for the file and git schemes. For other schemes (http, oci), the target is treated as a single file and returned as a single-element slice.

TODO: Add directory support for oci scheme.

Example (Local)
results, err := GetFiles(context.Background(), "../README.md")
fmt.Println(len(results) == 1, len(results[0].Content) > 0, err)
Output:
true true <nil>

type HttpDoer

type HttpDoer interface {
	Do(req *http.Request) (*http.Response, error)
}

HttpDoer is an http.Client interface used for overrides during testing or other http fetching implementations.

type Option

type Option func(*options)

Option is an option function that modifies the options structure in place.

func WithHttpClient

func WithHttpClient(c HttpDoer) Option

WithHttpClient sets the http client that may be used.

Example
customClient := &http.Client{
	Transport: &http.Transport{
		Proxy: func(*http.Request) (*url.URL, error) {
			return nil, fmt.Errorf("no proxy")
		},
	},
}
_, err := GetFile(context.Background(), "https://example.com", WithHttpClient(customClient))
fmt.Println(err)
Output:
failed to make get request: Get "https://example.com": no proxy

func WithLimit

func WithLimit(b int) Option

WithLimit sets the io reader limit in bytes.

Example
_, err := GetFile(context.Background(), "../README.md", WithLimit(1))
fmt.Println(err)
Output:
failed to read file: 1 byte limit exceeded

func WithLogger

func WithLogger(l *log.Logger) Option

WithLogger sets the logging output

func WithTempDir

func WithTempDir(p string) Option

WithTempDir sets the temporary data directory.

Jump to

Keyboard shortcuts

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