Documentation
¶
Overview ¶
Package selfupdate implements automatic install-method detection and safe self-updating for the cchp CLI.
Index ¶
- Constants
- func ArchiveName(tag, goos, goarch string) (string, bool)
- func BinaryName(goos string) string
- func CompareVersions(current, target string) (cmp int, ok bool)
- func DisplayVersion(raw string) string
- func DocsURL(getenv func(string) string) string
- func ExtractBinary(archivePath, binaryName, dstPath string) error
- func IsReleaseVersion(raw string) bool
- func ManualUpgradeInstructions(method Method, targetVersion, goos string) string
- func NormalizeVersion(raw string) (string, bool)
- func ParseChecksums(data []byte) (map[string]string, error)
- func ReplaceBinary(targetPath, srcPath string) error
- func VerifyFileSHA256(filePath, expectedHex string) (string, error)
- type Asset
- type CommandRunner
- type Config
- type DetectEnv
- type Detection
- type ExecRunner
- type Method
- type Release
- type Result
- type RunOptions
- type Updater
Constants ¶
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" )
const (
DefaultAPIBaseURL = "https://api.github.com"
)
Variables ¶
This section is empty.
Functions ¶
func ArchiveName ¶
ArchiveName returns the GoReleaser archive filename for a release tag and platform. Only the six platforms built by .goreleaser.yaml are supported.
func BinaryName ¶
BinaryName returns the expected executable filename inside the release archive.
func CompareVersions ¶
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 ¶
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 ¶
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 ¶
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 ¶
IsReleaseVersion reports whether raw is a valid SemVer release version (as opposed to "dev", "test", empty, or a Go pseudo-version).
func ManualUpgradeInstructions ¶
ManualUpgradeInstructions returns concrete manual upgrade steps tailored to the install method and operating system.
func NormalizeVersion ¶
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 ¶
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 ¶
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 ¶
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.
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.
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 ¶
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.