selfupdate

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2026 License: AGPL-3.0 Imports: 20 Imported by: 1 module, 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 ScopeWeaver's one-click update: download a GitHub release executable, verify and stage it, then atomically replace it on startup.

Responsibilities (see start.sh / start.bat):

Startup scripts: supervise the process and decide whether to restart from its exit code. This package: download, SHA256 verification, smoke testing, replacement, and rollback.

Replacement lives in Go because shell and batch would need separate checksum and smoke-test implementations (sha256sum/shasum/certutil) at the most critical safety boundary. An unexecutable update would otherwise require manual recovery.

A complete upgrade involves three process starts:

1. Old server receives /api/update/apply, downloads/verifies, stages scopeweaver-cli.new, exits 75. 2. Supervisor restarts old server; Bootstrap verifies/smoke-tests/replaces the staged file, exits 75. 3. Supervisor starts the new version; Bootstrap records an attempt and clears the marker after stability.

Failure preserves/restores the old version: step 2 discards invalid staging; three consecutive unstable starts in step 3 automatically restore scopeweaver-cli.old.

Index

Constants

View Source
const ExitRestart = 75

ExitRestart asks the supervisor to relaunch immediately (EX_TEMPFAIL), without crash backoff. Exit 0 is a normal stop; other exit codes indicate crashes.

View Source
const Repo = "cskwork/scopeweaver-cli"

Repo is the fixed release source. Making it configurable would grant anyone who can edit configuration a remote-code-execution path, which is unacceptable here.

View Source
const SettleDelay = 30 * time.Second

SettleDelay is the stable runtime required to confirm that the new version survived.

Variables

This section is empty.

Functions

func AssetName

func AssetName(tag, goos, goarch string) string

AssetName returns the platform package name, matching build.sh package_binary: scopeweaver-cli-<version>-<os>-<arch>.zip, with the leading v removed from the version.

func Bootstrap

func Bootstrap() (Action, State)

Bootstrap runs first in main, before opening a database or listening on ports.

Three possible states:

1. scopeweaver-cli.new exists: verify and smoke-test, then replace/restart; discard on failure. 2. Only a marker remains: increment the post-replacement attempt count; roll back after repeated failures. 3. Neither exists: start normally.

func CompareVersions

func CompareVersions(a, b string) (int, bool)

CompareVersions returns -1/0/1 for a<b, a==b, or a>b. ok=false means at least one version is not comparable, such as dev or git-describe output like 0.3.7-2-gabc1234-dirty. Disable one-click updates then, rather than overwriting a development build and uncommitted changes with a release.

func HasBackup

func HasBackup() bool

HasBackup reports whether a previous version exists, controlling the rollback button.

func InDocker

func InDocker() bool

InDocker reports container execution. Replacement modifies the writable layer; recreating the container with docker compose restores the image's version. This is expected when pulling a new image, but the UI must explain it accurately.

func NewClient

func NewClient(proxy string) *http.Client

NewClient accepts only GitHub hosts. An empty proxy uses a direct connection.

Do not reuse the default transport: updates must require TLS and certificate verification regardless of InsecureSkipVerify settings elsewhere.

func Rollback

func Rollback() error

Rollback implements /api/update/rollback, explicitly restoring the previous version. It only swaps files; the caller exits with ExitRestart and the supervisor restarts it.

func Settle

func Settle()

Settle confirms stable operation of the new version and clears the upgrade marker.

main calls it after a delay following HTTP startup. Crashing before that point leaves the marker so subsequent attempts accumulate until rollback is triggered.

func Stage

func Stage(ctx context.Context, c *http.Client, rel *Release, currentVersion string, prog Progress) error

Stage downloads and verifies the platform package, then stages scopeweaver-cli.new.

Use the full ZIP rather than a bare executable because SHA256SUMS covers ZIPs. This matches the release pipeline and its existing archives. The package also includes skills for potential future synchronization, costing only a few hundred KB.

Returning successfully means staging is complete; the caller then shuts down with ExitRestart.

func StageMessages

func StageMessages(ctx context.Context, c *http.Client, rel *Release, currentVersion string, prog MessageProgress) error

StageMessages retains built-in progress templates for per-subscriber rendering. Stage remains compatible with existing callers that consume plain text.

Types

type Action

type Action int

Action is Bootstrap's instruction to main.

const (
	// Continue starts the server normally.
	Continue Action = iota
	// Restart exits immediately with ExitRestart so the supervisor relaunches it.
	Restart
)

type Asset

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

Asset describes one attached release file.

type MessageProgress

type MessageProgress func(ph Phase, pct int, msg locale.Message)

MessageProgress carries a localizable progress event without modifying raw arguments.

type Paths

type Paths struct {
	Dir     string // Executable directory.
	Current string // Current executable: scopeweaver-cli / scopeweaver-cli.exe.
	New     string // Staged version: scopeweaver-cli.new / scopeweaver-cli.new.exe.
	Sum     string // Staged SHA256 (hex): scopeweaver-cli.new.sha256 / scopeweaver-cli.new.exe.sha256.
	Old     string // Previous executable: scopeweaver-cli.old / scopeweaver-cli.old.exe.
	Marker  string // Upgrade state marker: scopeweaver-cli.upgrade.json.
}

Paths contains all upgrade files, rooted in the executable's directory. Do not use CWD: a service may run from / or any other directory, which would put staging files elsewhere and break replacement.

func ResolvePaths

func ResolvePaths() (Paths, error)

ResolvePaths derives all upgrade paths from the current executable.

Windows .new/.old files must retain .exe for smoke testing and execution. Strip the extension before adding the suffix to keep platform naming symmetric.

type Phase

type Phase string

Phase is the upgrade stage, used directly in the SSE phase field.

const (
	PhaseIdle     Phase = "idle"
	PhaseDownload Phase = "downloading"
	PhaseVerify   Phase = "verifying"
	PhaseExtract  Phase = "extracting"
	PhaseStaged   Phase = "staged"
	PhaseFailed   Phase = "failed"
)

type Progress

type Progress func(ph Phase, pct int, msg string)

Progress reports to the caller/UI. pct is meaningful only during download (0-100); other phases use -1.

type Release

type Release struct {
	TagName     string    `json:"tag_name"`
	Name        string    `json:"name"`
	Body        string    `json:"body"`
	Draft       bool      `json:"draft"`
	Prerelease  bool      `json:"prerelease"`
	PublishedAt time.Time `json:"published_at"`
	HTMLURL     string    `json:"html_url"`
	Assets      []Asset   `json:"assets"`
}

Release contains the GitHub release fields needed by the updater.

func FetchLatest

func FetchLatest(ctx context.Context, c *http.Client) (*Release, error)

FetchLatest fetches the latest stable release.

func (*Release) FindAsset

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

FindAsset looks up a release asset by name.

type State

type State struct {
	Pending     bool   // Replacement has not yet been confirmed stable.
	RolledBack  bool   // An automatic rollback occurred during this startup.
	FailedStage bool   // Staged update failed verification/smoke testing and was discarded.
	Detail      string // One-sentence user-facing explanation.
}

State describes this startup's upgrade state so /api/update/check can report accurately whether the previous upgrade succeeded or was rolled back.

Jump to

Keyboard shortcuts

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