selfupdate

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2026 License: MIT Imports: 31 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 selfupdate implements automatic install-method detection and safe self-updating for the cchp CLI.

Index

Constants

View Source
const (
	DefaultRepo      = "CCH-HQ/cchp-cli"
	HomebrewFormula  = "cch-hq/tap/cchp"
	GoInstallPkg     = "github.com/CCH-HQ/cchp-cli/cmd/cchp"
	InstallScriptURL = "https://raw.githubusercontent.com/CCH-HQ/cchp-cli/main/install.sh"
	ReleasesPageURL  = "https://github.com/CCH-HQ/cchp-cli/releases/latest"
)
View Source
const (
	DefaultAPIBaseURL = "https://api.github.com"
)

Variables

This section is empty.

Functions

func ArchiveName

func ArchiveName(tag, goos, goarch string) (string, bool)

ArchiveName returns the GoReleaser archive filename for a release tag and platform. Only the six platforms built by .goreleaser.yaml are supported.

func BinaryName

func BinaryName(goos string) string

BinaryName returns the expected executable filename inside the release archive.

func CompareVersions

func CompareVersions(current, target string) (cmp int, ok bool)

CompareVersions compares two SemVer release versions. It returns -1 if current < target, 0 if equal, +1 if current > target. If either version is not a valid SemVer release version, ok is false.

func DisplayVersion

func DisplayVersion(raw string) string

DisplayVersion formats a version string cleanly: release versions carry a leading "v" (e.g. "0.2.0" -> "v0.2.0"), while non-release strings like "dev" are returned as-is (defaulting to "dev" if empty).

func DocsURL

func DocsURL(getenv func(string) string) string

DocsURL resolves the locale-appropriate official CLI install/update docs URL via sitedocs.Resolve("", getenv), defaulting to https://cch-plus.com/en/docs/cli#install.

func ExtractBinary

func ExtractBinary(archivePath, binaryName, dstPath string) error

ExtractBinary extracts binaryName (e.g. "cchp" or "cchp.exe") from a .tar.gz or .zip archive into dstPath. Every archive entry is validated against path traversal before anything is accepted.

func IsReleaseVersion

func IsReleaseVersion(raw string) bool

IsReleaseVersion reports whether raw is a valid SemVer release version (as opposed to "dev", "test", empty, or a Go pseudo-version).

func ManualUpgradeInstructions

func ManualUpgradeInstructions(method Method, targetVersion, goos string) string

ManualUpgradeInstructions returns concrete manual upgrade steps tailored to the install method and operating system.

func NormalizeVersion

func NormalizeVersion(raw string) (string, bool)

NormalizeVersion validates a release SemVer string (accepting with or without a leading "v") and returns its canonical "v"-prefixed form. Go pseudo-versions and non-SemVer strings such as "dev" return ("", false).

func ParseChecksums

func ParseChecksums(data []byte) (map[string]string, error)

ParseChecksums parses a GoReleaser checksums.txt file into a map of filename -> lowercase 64-char hex SHA-256 digest. Duplicate entries or malformed lines cause a fail-closed error.

func ReplaceBinary

func ReplaceBinary(targetPath, srcPath string) error

ReplaceBinary atomically replaces targetPath with the contents of srcPath. The replacement is staged in a temporary file inside targetPath's directory, synced, chmodded to 0755, and renamed into place. On Windows, the running binary is first moved aside to targetPath+".old" with automatic rollback on failure.

func VerifyFileSHA256

func VerifyFileSHA256(filePath, expectedHex string) (string, error)

VerifyFileSHA256 computes the SHA-256 digest of filePath and compares it against expectedHex in constant time. It returns the actual lowercase hex digest and a non-nil error on any read failure or checksum mismatch.

Types

type Asset

type Asset struct {
	Name               string `json:"name"`
	BrowserDownloadURL string `json:"browser_download_url"`
	Size               int64  `json:"size"`
}

Asset is one downloadable release asset.

type CommandRunner

type CommandRunner interface {
	Run(ctx context.Context, name string, args ...string) (stdout, stderr string, err error)
}

CommandRunner executes external update tools (brew, go) and is injectable for unit testing.

type Config

type Config struct {
	CurrentVersion string
	UserAgent      string
	APIBaseURL     string
	Repo           string
	HTTPClient     *http.Client
	RetryMax       *int
	CmdRunner      CommandRunner
	Executable     func() (string, error)
	EvalSymlinks   func(string) (string, error)
	Getenv         func(string) string
	LookPath       func(string) (string, error)
	UserHomeDir    func() (string, error)
	GOOS           string
	GOARCH         string
	Debug          io.Writer
	OnProgress     func(string)
}

Config configures an Updater. Zero fields use production defaults.

type DetectEnv

type DetectEnv struct {
	Getenv       func(string) string
	LookPath     func(string) (string, error)
	EvalSymlinks func(string) (string, error)
	UserHomeDir  func() (string, error)
	GOOS         string
}

DetectEnv holds injectable environment hooks for Detect.

type Detection

type Detection struct {
	// Method is the effective installation method ("homebrew", "go_install",
	// "direct", or "dev").
	Method Method
	// UnderlyingMethod is the path-based installation method ("homebrew",
	// "go_install", or "direct"), used when --force overrides a dev build.
	UnderlyingMethod Method
	// ExecutablePath is the symlink-resolved path to the running binary.
	ExecutablePath string
	// OriginalPath is the path reported by os.Executable before symlink resolution.
	OriginalPath string
	// IsDev is true when the current binary's version is "dev" or non-SemVer.
	IsDev bool
}

Detection describes the detected installation method and executable path.

func Detect

func Detect(execPath, currentVersion string, env DetectEnv) Detection

Detect determines how cchp was installed from its executable path, environment variables, PATH lookup, and build version.

type ExecRunner

type ExecRunner struct{}

ExecRunner runs commands via os/exec.

func (ExecRunner) Run

func (ExecRunner) Run(ctx context.Context, name string, args ...string) (string, string, error)

Run executes name with args under ctx and captures stdout and stderr.

type Method

type Method string

Method identifies how cchp was installed.

const (
	MethodHomebrew  Method = "homebrew"
	MethodGoInstall Method = "go_install"
	MethodDirect    Method = "direct"
	MethodDev       Method = "dev"
)

type Release

type Release struct {
	TagName    string  `json:"tag_name"`
	HTMLURL    string  `json:"html_url"`
	Draft      bool    `json:"draft"`
	Prerelease bool    `json:"prerelease"`
	Assets     []Asset `json:"assets"`
}

Release is the subset of a GitHub Release response used by cchp update.

func (*Release) FindAsset

func (r *Release) FindAsset(name string) (Asset, bool)

FindAsset returns the asset matching name exactly.

type Result

type Result struct {
	CurrentVersion  string   `json:"current_version"`
	LatestVersion   string   `json:"latest_version,omitempty"`
	TargetVersion   string   `json:"target_version,omitempty"`
	InstallMethod   Method   `json:"install_method"`
	EffectiveMethod Method   `json:"effective_method,omitempty"`
	ExecutablePath  string   `json:"executable_path,omitempty"`
	UpdateAvailable bool     `json:"update_available"`
	Updated         bool     `json:"updated"`
	UpToDate        bool     `json:"up_to_date,omitempty"`
	CheckOnly       bool     `json:"check_only,omitempty"`
	DryRun          bool     `json:"dry_run,omitempty"`
	DevBuild        bool     `json:"dev_build,omitempty"`
	NeedsForce      bool     `json:"needs_force,omitempty"`
	Action          string   `json:"action,omitempty"`
	PlannedCommand  []string `json:"planned_command,omitempty"`
	Command         []string `json:"command,omitempty"`
	ArchiveName     string   `json:"archive_name,omitempty"`
	DownloadURL     string   `json:"download_url,omitempty"`
	ChecksumsURL    string   `json:"checksums_url,omitempty"`
	SHA256          string   `json:"sha256,omitempty"`
	ReleaseURL      string   `json:"release_url,omitempty"`
	DocsURL         string   `json:"docs_url"`
	Message         string   `json:"message,omitempty"`
}

Result describes the outcome of cchp update.

type RunOptions

type RunOptions struct {
	Check   bool
	Force   bool
	DryRun  bool
	Version string
}

RunOptions controls one invocation of Updater.Run.

type Updater

type Updater struct {
	// contains filtered or unexported fields
}

Updater detects the install method and safely updates cchp.

func New

func New(cfg Config) *Updater

New constructs an Updater with production defaults for unset fields.

func (*Updater) Run

func (u *Updater) Run(ctx context.Context, opts RunOptions) (*Result, error)

Run executes the update check, dry-run plan, or actual update. Every returned error is a *contract.Failure whose Hint includes concrete manual upgrade instructions and the locale-appropriate official docs URL.

Jump to

Keyboard shortcuts

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