toolinstall

package
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: MIT Imports: 18 Imported by: 0 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 toolinstall turns a tool's GitHub release into a verified program in place: it picks the release's archive and checksums file, downloads both into a fresh private folder, checks the archive against the checksums file and GitHub's digests, extracts the program's one entry, tries it, and puts it in its folder so that the target is never missing, rolling back when the placed copy won't run.

Nothing outside the download folder changes until the last step, and every refusal or cancel before it leaves nothing behind. It has no database and speaks no HTTP itself: the caller hands it a Downloader, and records what it placed.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoBuild is returned when the release has no archive for this OS and architecture.
	ErrNoBuild = errors.New("the release has no build for this machine")

	// ErrNoChecksums is returned when the release has no checksums file: nothing is
	// installed unchecked.
	ErrNoChecksums = errors.New("the release has no checksums file")

	// ErrShortDownload is returned when a download's size isn't the size GitHub lists.
	ErrShortDownload = errors.New("the download was cut short")

	// ErrChecksum is returned when a download doesn't match its checksum or GitHub's
	// digest, or the checksums file doesn't list the archive exactly once.
	ErrChecksum = errors.New("the download didn't match its checksum")

	// ErrArchive is returned for an archive that doesn't hold the program as one plain
	// file at its root, or holds something unsafe.
	ErrArchive = errors.New("the archive isn't as expected")

	// ErrSmoke is returned when the downloaded program won't run or reports another
	// version. The download is thrown away and nothing changes.
	ErrSmoke = errors.New("the downloaded program didn't run")

	// ErrLinkTarget is returned when the target is a symbolic link, which is never
	// replaced.
	ErrLinkTarget = errors.New("the target is a link")

	// ErrRolledBack is returned when the placed program didn't run and the old copy was
	// put back, or the new one taken away when there was none.
	ErrRolledBack = errors.New("the new copy didn't run and was rolled back")

	// ErrRollbackFailed is returned when the placed program didn't run and putting the old
	// copy back failed too: the new copy is still in place.
	ErrRollbackFailed = errors.New("the new copy didn't run, and the old one couldn't be put back")

	// ErrWrite is returned when the target's folder can't be written.
	ErrWrite = errors.New("can't write the folder")
)

Functions

This section is empty.

Types

type Asset

type Asset struct {
	Name string
	URL  string
	Size int64

	// Digest is GitHub's digest of it, as in sha256:<hex>, or "" when it has none.
	Digest string
}

Asset is one file of a release, as GitHub lists it.

type Downloader

type Downloader interface {
	DownloadAsset(ctx context.Context, rawURL string, w io.Writer, maxBytes int64) (int64, error)
}

Downloader streams a public release file into w, at most maxBytes, and returns how many bytes it wrote. *github.Client implements it.

type Installer

type Installer struct {
	Downloads string
	Download  Downloader
	Slug      string
	// contains filtered or unexported fields
}

Installer installs releases. Downloads is the folder the downloads go in, each in a fresh folder of its own; Slug names those folders and the files placed beside a target, so cleanup finds only its own.

func (*Installer) Cleanup

func (in *Installer) Cleanup(dirs []string, now time.Time, maxAge time.Duration)

Cleanup removes what installs that stopped short left behind, and only that, by this app's own names: download folders in Downloads older than maxAge, and files beside a target in dirs (<binary>.<slug>-new and -old) older than maxAge. It reports nothing: a leftover that can't be removed is tried again next time.

func (*Installer) Install

func (in *Installer) Install(ctx context.Context, req Request) (Placed, error)

Install downloads, checks, tries, and places the release req names, and returns what it placed. On any failure before the program is placed, nothing outside the download folder has changed, and that folder is removed. Every file in the download folder is made and read through an os.Root of it, so no name, an asset's from GitHub included, reaches out of it.

type Placed

type Placed struct {
	// Path is the program's path, in the folder Dir resolves to.
	Path string

	// SHA256 is the placed program's SHA-256, in hex.
	SHA256 string

	// Version and Archive are the release's version and the archive it came from.
	Version string
	Archive string

	// Replaced reports that a program was there before and was replaced.
	Replaced bool
}

Placed is what an install put in place.

type Progress

type Progress struct {
	Step  Step
	Done  int64
	Total int64
}

Progress is how far an install has got: its step, and while downloading, the bytes so far of the archive's total.

type Request

type Request struct {
	// Tool names the tool in the download folder's name, as in mage-x, and Binary is its
	// program, as in magex.
	Tool   string
	Binary string

	// Version is the release's version, without a leading v.
	Version string

	// Archive and Checksums are the names of the release's archive for this machine and
	// of its checksums file, and Assets the release's files.
	Archive   string
	Checksums string
	Assets    []Asset

	// Dir is the folder the program goes in. It is created when missing, and a link to a
	// folder is followed: the program goes in the folder it names.
	Dir string

	// Smoke runs the program at path and returns an error unless it reports Version. It
	// runs on the extracted copy, then on the placed one.
	Smoke func(ctx context.Context, path string) error

	// Progress hears each step and the download's bytes. It may be nil.
	Progress func(Progress)
}

Request is one install or update of a tool.

type Step

type Step string

Step is where an install is.

const (
	StepDownloading Step = "downloading"
	StepVerifying   Step = "verifying"
	StepTrying      Step = "trying"
	StepInstalling  Step = "installing"
)

The steps an install reports.

Jump to

Keyboard shortcuts

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