update

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jun 4, 2026 License: MIT Imports: 10 Imported by: 0

README

update

Reusable self-update for Go CLIs: latest-version resolution, SHA256 checksum verification, and atomic binary replacement with rollback.

Features

  • Latest Version Resolution: Find the latest release tag from a GitHub repository.
  • Checksum Verification: Verify downloaded assets against a checksums.txt file.
  • Atomic Binary Replacement: Swap the current binary with a new one, with automatic backup and rollback support.
  • Cross-Filesystem Support: Robustly move files even across different mount points.
  • GitHub API Integration: Uses recommended headers and supports authentication via GITHUB_TOKEN or GH_TOKEN to avoid rate limits.

API Documentation

See docs/API.md for a concise list of exported symbols and their contracts.

Usage Examples

1. Resolve, Download, and Verify
package main

import (
    "fmt"
    "github.com/tinywasm/update"
)

func main() {
    source := "https://github.com/tinywasm/tinywasm"

    // 1. Resolve latest version
    latest, err := update.ResolveLatestVersion(source, update.DefaultDownload)
    if err != nil {
        panic(err)
    }
    fmt.Printf("Latest version: %s\n", latest)

    // 2. Download asset and checksums
    assetURL := fmt.Sprintf("%s/releases/download/%s/tinywasm-linux-amd64", source, latest)
    sumsURL := fmt.Sprintf("%s/releases/download/%s/checksums.txt", source, latest)

    data, err := update.DefaultDownload(assetURL)
    if err != nil {
        panic(err)
    }

    sums, err := update.DefaultDownload(sumsURL)
    if err != nil {
        panic(err)
    }

    // 3. Verify checksum
    if err := update.VerifyChecksum("tinywasm-linux-amd64", data, sums); err != nil {
        panic(err)
    }
    fmt.Println("Checksum verified!")
}
2. Check if Outdated
current := "v0.1.0"
latest := "v0.2.0"

if update.IsOutdated(current, latest) {
    fmt.Println("A new version is available!")
}
3. Atomic Swap with Rollback
target := "/usr/local/bin/mytool"
newBinary := "/tmp/mytool-new"

backup, err := update.Swap(target, newBinary)
if err != nil {
    panic(err)
}

// Perform health check on the new binary...
success := runHealthCheck(target)
if !success {
    fmt.Println("Health check failed, rolling back...")
    if err := update.Rollback(target, backup); err != nil {
        panic(err)
    }
}

Environment Variables

  • GITHUB_TOKEN: Optional GitHub personal access token.
  • GH_TOKEN: Fallback GitHub personal access token.

Documentation

Index

Constants

View Source
const (
	EnvGitHubToken = "GITHUB_TOKEN"
	EnvGHToken     = "GH_TOKEN"
)

Variables

This section is empty.

Functions

func DefaultDownload added in v0.0.2

func DefaultDownload(url string) ([]byte, error)

DefaultDownload was MOVED verbatim from tinywasm/installer (mode_binary.go).

func IsOutdated added in v0.0.2

func IsOutdated(current, latest string) bool

IsOutdated reports whether current is strictly older than latest (MAJOR.MINOR.PATCH, leading "v" optional, pre-release/build suffix ignored). Any parse failure -> false, so "dev"/empty builds are never reported as outdated.

func ResolveLatestVersion added in v0.0.2

func ResolveLatestVersion(source string, download func(string) ([]byte, error)) (string, error)

ResolveLatestVersion was MOVED from tinywasm/installer (mode_binary.go: resolveLatestVersion). The only change is decoupling: the *installer.Deps parameter was replaced by an injected download func so the proven logic lives here without depending on the installer package.

func Rollback added in v0.0.2

func Rollback(targetPath, backupPath string) error

Rollback restores backupPath over targetPath (used after a post-swap failure, e.g. the new process failed its health check).

func Swap added in v0.0.2

func Swap(targetPath, newFilePath string) (string, error)

Swap backs up targetPath (if it exists) to targetPath+".old" and renames newFilePath into its place. It returns the backup path ("" when targetPath did not exist). On install failure it restores the backup before returning the error.

func VerifyChecksum added in v0.0.2

func VerifyChecksum(asset string, data []byte, sums []byte) error

VerifyChecksum was MOVED verbatim from tinywasm/installer (mode_binary.go: verifyChecksum) — only the name was exported. It verifies data's SHA256 against the entry for asset in a checksums.txt body ("<hex> <asset>" per line).

Types

This section is empty.

Jump to

Keyboard shortcuts

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