fetch

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2026 License: MIT Imports: 19 Imported by: ? modules, 0 packages

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 fetch brings a C library of lib/ into a Go project before it is built, so that the cgo, ccgo and purego drivers find it there. It reads a library.Citation, downloads each target's published archive (checking its SHA-256 before it uses a byte of it), and lays out a Go package in the project's folder:

cgo     include/ and lib/<goos>_<goarch>/<static library>, with a cgo file whose #cgo
        directives name them through ${SRCDIR}, so no CGO_* variable and no system install is
        needed
c2go    the translated Go source of each target, copied as published
purego  lib/<goos>_<goarch>/<shared library>, embedded into the program and registered with
        library.Provide, so it travels inside the binary

The project imports that package once (a blank import) to use it. Go runs no code before a build, so this is a step the project runs first: go generate with `go tool ext bundle fetch`.

Index

Constants

View Source
const (
	CGo    = "cgo"
	C2Go   = "c2go"
	PureGo = "purego"
)

The drivers fetch lays out.

Variables

View Source
var ErrOffline = errors.New("fetch: not cached and offline")

ErrOffline is an archive that is not cached when Options.Offline is set.

Functions

func Fetch

func Fetch(ctx context.Context, o Options) error

Fetch lays the library out in o.Dir for every target and driver, replacing what fetch wrote there before. It writes nothing until every archive has been downloaded and verified.

func HTTPS

func HTTPS(ctx context.Context, url string) (io.ReadCloser, error)

HTTPS is the download fetch uses when Options.Fetch is nil: a GET over HTTPS that follows only HTTPS redirects. A caller that wraps Fetch, to report a download, calls it.

func Shared

func Shared(ctx context.Context, so SharedOptions) (string, error)

Shared returns the path of the purego shared library the citation publishes for o.Target, verified against the citation. A copy kept by an earlier call is used when it still matches its digest; otherwise the archive is taken from the archive cache or downloaded, checked against the citation's SHA-256, and the library is extracted again. It writes by renaming complete files into place, so processes that bring in the same library at once agree on its contents.

Types

type Limits

type Limits struct {
	ArchiveBytes int64 // the downloaded archive; default 256 MiB
	FileBytes    int64 // one extracted file; default 256 MiB
	Files        int   // files extracted from one archive; default 10000
}

Limits bound what an archive may hold.

type Options

type Options struct {
	Citation library.Citation
	// Drivers to lay out: CGo, C2Go and PureGo. At least one.
	Drivers []string
	// Targets are "goos/goarch" pairs; at least one.
	Targets []string
	// Dir is the package directory to write, inside the project's module.
	Dir string
	// Package is the package name of the files written; default the library name without "ext_".
	Package string
	// Fetch downloads a URL; nil uses an HTTPS GET with http.DefaultClient. A program with its own
	// downloader (ext has one) passes it here.
	Fetch func(ctx context.Context, url string) (io.ReadCloser, error)
	// Cache is a directory of verified archives, reused across runs; empty is the user cache's
	// ext-go/archives. Offline refuses any download not already there.
	Cache   string
	Offline bool
	Limits  Limits
}

Options say what to fetch and where.

type Selection

type Selection struct {
	Target string        `json:"target"`
	Driver string        `json:"driver"`
	Asset  library.Asset `json:"asset"`
}

Selection is the exact pinned asset used for one target and driver.

func Plan

func Plan(citation library.Citation, drivers, targets []string) ([]Selection, error)

Plan resolves every requested target/driver pair without downloading, consulting caches, or writing files. Order follows targets, then drivers. Bundles can inspect this plan before preparing any package or native output.

type SharedOptions

type SharedOptions struct {
	Citation library.Citation
	// Target is the "goos/goarch" pair; default this program's.
	Target string
	// Dir holds the extracted libraries; empty is the user cache's ext-go/library. A library is kept at
	// Dir/<library>/<archive sha256>/<file>, beside <file>.sha256, the digest of the file itself.
	Dir string
	// Fetch, Cache, Offline and Limits are as in Options.
	Fetch   func(ctx context.Context, url string) (io.ReadCloser, error)
	Cache   string
	Offline bool
	Limits  Limits
}

SharedOptions say which shared library to bring in at run time and where to keep it.

Jump to

Keyboard shortcuts

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