cliupdate

package
v1.5.18 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2026 License: Apache-2.0 Imports: 22 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 cliupdate lets the CLI find a newer release and replace its own executable with it, reusing selfupgrade's download, checksum and signature code.

Index

Constants

View Source
const (
	MethodBinary = "binary"
	MethodNPM    = "npm"
	MethodPNPM   = "pnpm"
	MethodYarn   = "yarn"
	MethodNPX    = "npx"
	MethodBrew   = "brew"
)

Install methods. Binary means a plain download the CLI may replace itself.

View Source
const (
	ChannelStable = "stable"
	ChannelBeta   = "beta"
)

Channel names accepted by Source.Channel.

View Source
const (
	ActionWouldRollback = "would-roll-back"
	ActionRolledBack    = "rolled-back"
)

Actions reported by Rollback.

View Source
const (
	ActionUpToDate   = "up-to-date"
	ActionWouldApply = "would-update"
	ActionUpdated    = "updated"
)

Actions reported in Result.Action.

View Source
const AliasName = "lrail"

AliasName is the short command installed next to the long one.

View Source
const EnvInstallMethod = "LEVELRAIL_CLI_INSTALL_METHOD"

EnvInstallMethod is set by the npm launcher so the binary knows a package manager owns it.

Variables

View Source
var ErrManagedPath = errors.New("the running executable is managed by a package manager")

ErrManagedPath means a package manager owns the executable.

View Source
var ErrNoBackup = errors.New("no previous binary was kept; an update keeps one at <executable>.bak")

ErrNoBackup means there is no previous binary to go back to.

View Source
var ErrNotWritable = errors.New("cannot write next to the running executable")

ErrNotWritable means the executable's directory cannot take a staged file.

Functions

func Asset

func Asset(base, goos, goarch string) string

Asset names the CLI release asset the way release.yml does.

func BackupPath

func BackupPath(exe string) string

BackupPath is where Update keeps the binary it replaced.

func DetectMethod

func DetectMethod(exePath string, lookupEnv func(string) (string, bool)) string

DetectMethod picks the install method from the launcher env var, then from where the executable lives. Unknown input falls back to a plain binary.

func ExecutablePath

func ExecutablePath() (string, error)

ExecutablePath returns the running binary with symlinks resolved, so the swap replaces the real file and never a link.

func InstalledVersion

func InstalledVersion(ctx context.Context, path string) (string, error)

InstalledVersion asks the executable at path for its version after an upgrade.

func ManagedPathHint

func ManagedPathHint(path string) string

ManagedPathHint returns the right update command when path sits under a package manager's tree, or "" when the CLI is self-managed.

func MinorBehind

func MinorBehind(cli, server string) bool

MinorBehind reports whether server is at least one minor (or major) ahead of cli.

func Newer

func Newer(current, latest string) bool

Newer reports whether latest is a higher release than current. A dev build or an unparsable version never reports newer.

func PackageManaged

func PackageManaged(method string) bool

PackageManaged reports whether a JS package manager owns the install.

func RecordServerVersion

func RecordServerVersion(dir, serverURL, ver string)

RecordServerVersion remembers the version a server reported, keyed by its URL.

func RefreshAlias

func RefreshAlias(exe, priorSHA string) string

RefreshAlias keeps an existing `lrail` next to exe on the new binary, but only when the file is ours: a symlink that resolves to exe or its backup is re-pointed, and a regular file is replaced only when its SHA-256 equals priorSHA (the binary just swapped out). Anything else, such as a user's wrapper script, is left alone. It never creates one and returns the path it refreshed or "".

func RunUpgrade

func RunUpgrade(ctx context.Context, argv []string, stdout, stderr io.Writer) error

RunUpgrade runs the package manager upgrade with the user's terminal attached.

func ServerVersionFor

func ServerVersionFor(dir, serverURL string) string

ServerVersionFor returns the last version recorded for serverURL, or "".

func SkewDue

func SkewDue(dir, key string, every time.Duration, now func() time.Time) bool

SkewDue reports whether the warning for key was not shown within every, and marks it shown when so.

func UpgradeArgv

func UpgradeArgv(method, spec string) []string

UpgradeArgv returns the package manager invocation that upgrades the CLI to spec ("latest" or a version), or nil when the method has no such command.

func UpgradeCommand

func UpgradeCommand(method, prog string) string

UpgradeCommand returns the right upgrade command for the method, or the CLI's own update command for a plain binary.

Types

type Hinter

type Hinter struct {
	Source  Source
	Current string
	// Dir is the CLI's config directory, where the check result is cached.
	Dir string
	// IntervalDays of 0 or less disables the hint.
	IntervalDays int
	// Method is the install method; empty means a plain binary.
	Method string
	Now    func() time.Time
}

Hinter prints a one-line notice at most once per Interval days.

func (Hinter) Cached

func (h Hinter) Cached() (latest string, checkedAt time.Time, ok bool)

Cached returns the last stored check result without touching the network; ok is false when no check has succeeded yet.

func (Hinter) Check

func (h Hinter) Check(ctx context.Context) string

Check returns the line to print, or "" when there is nothing to say. It never returns an error: every failure just means no hint this time.

func (Hinter) Notice

func (h Hinter) Notice(every time.Duration) string

Notice reads only the cached check result, never the network, and returns the hint line when a newer release is known and none was shown within every.

type Options

type Options struct {
	Source Source
	// Current is the running version, Target an explicit tag ("" means latest).
	Current string
	Target  string
	DryRun  bool
	// Force skips the package-manager path refusal.
	Force bool
	// ExePath is the resolved path of the running executable.
	ExePath string
	// TUFCacheDir keeps Sigstore trust metadata between runs.
	TUFCacheDir string
	// Confirm runs after the plan is known and before any download; an error aborts.
	Confirm func(current, target string) error
	// Verify checks the signature over checksums.txt. Nil verifies in process,
	// in require mode, so a release without a valid signature is never applied.
	Verify func(ctx context.Context, checksumsPath, bundlePath string) (string, error)
}

Options configures one update run.

type Result

type Result struct {
	Action       string `json:"action"`
	Current      string `json:"current"`
	Target       string `json:"target"`
	Asset        string `json:"asset"`
	Path         string `json:"path"`
	Verification string `json:"verification,omitempty"`
	Backup       string `json:"backup,omitempty"`
	// Alias is the refreshed lrail link or copy, empty when there is none.
	Alias string `json:"alias,omitempty"`
}

Result describes what a run did or would do.

func Rollback

func Rollback(ctx context.Context, o RollbackOptions) (Result, error)

Rollback puts the binary Update kept back in place. The backup is opened once without following links and every check runs on that descriptor, so what is vetted is what is copied. The staged copy must start and report a version before anything is swapped, so a bad backup changes nothing.

func Update

func Update(ctx context.Context, o Options) (Result, error)

Update replaces o.ExePath with the target release after verifying it. On any error before the final rename the current binary is untouched.

type RollbackOptions

type RollbackOptions struct {
	Current string
	DryRun  bool
	Force   bool
	// ExePath is the resolved path of the running executable.
	ExePath string
	Confirm func(current, target string) error
}

RollbackOptions configures one rollback run.

type Source

type Source struct {
	// Repo is "owner/name".
	Repo string
	// APIBase serves the releases listing, for example https://api.github.com.
	APIBase string
	// DownloadBase is the asset prefix, the tag is appended by the fetcher.
	DownloadBase string
	// Asset is the release asset for this OS and arch.
	Asset string
	// Channel is "stable" (stable only), "beta" (prereleases count too) or ""
	// (stable, else the newest prerelease), the same rules as install-cli.sh.
	Channel string
	Client  *http.Client
	// AllowHTTP permits plain http bases, for tests against a local server.
	AllowHTTP bool
}

Source says where releases are published and which asset belongs to this build.

func (Source) Latest

func (s Source) Latest(ctx context.Context) (string, error)

Latest returns the highest tag on s.Channel that ships s.Asset; the default channel falls back to a prerelease when there is no stable one. An empty tag with a nil error means nothing suitable is published.

Jump to

Keyboard shortcuts

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