cliupdater

package module
v0.0.0-...-3da4a94 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2017 License: BSD-3-Clause Imports: 12 Imported by: 0

README

cliupdater: Download and keep a Go command-line tool up to date

This is a hack proof-of-concept designed similarly to gcloud's auto-update mechanism. The intention is:

  • At most once a day, the command checks if an update is available.
  • If there is an update, the tool displays some information about it, and provides a command that can be run to install the update.
  • Executing the update command causes the new version to be downloaded and the existing version to be replaced.

This proof-of-concept does this in the simplest way I could think of:

  • The binary creates a zero-length hidden "stamp" file with the name .binary.check to record the timestamp of the last update check.
  • On startup, if this file is older than a day, it does an HTTP HEAD request to a download URL (customized for the OS and architecture). This request returns a Last-Modified header. The value of this header is compared to to the timestamp of the executable. If it is more recent, the tool can print a message.
  • If the user invokes Update, it performs an update:
    • Downloads the file as .binary.download
    • Backs up the existing file as .binary.backup
    • Renames the download over the existing file

Ideally, the update should be atomic, so multiple concurrent updates or concurrently executing commands will not cause any problems.

TODO

  • Verify the downloaded file? E.g. check length or a signature?
  • Display release notes or changes?

Documentation

Index

Constants

View Source
const DefaultCheckInterval = 24 * time.Hour

DefaultCheckInterval is the minimum time between checks to see if the program is updated.

Variables

This section is empty.

Functions

This section is empty.

Types

type Metadata

type Metadata struct {
	// The time the source binary was updated.
	Updated time.Time
	// The difference between the update time of the source binary and the binary on disk.
	Diff time.Duration
}

Metadata contains information about the source binary and the binary on disk.

func (Metadata) DaysOld

func (u Metadata) DaysOld() int

DaysOld returns the number of days that the local binary is out of date.

func (Metadata) Outdated

func (u Metadata) Outdated() bool

Outdated returns true if the local binary is out of date.

type Updater

type Updater struct {
	// URL where the binary can be downloaded.
	BaseURL string
	// absolute path to the binary to be updated; defaults to the currently executing binary
	Path string
	// interval between automatic update checks
	CheckInterval time.Duration
	// Function used to output logs if not nil.
	Logf func(message string, args ...interface{})
	// Call the new binary with these arguments to "apply" an update. If it fails, the binary
	// will not be replaced.
	ApplyArgs []string
}

Updater keeps a Go binary up to date with a version available via HTTP(S). This can be used to implement auto-updating command line tools.

func (*Updater) MaybeCheckForUpdate

func (u *Updater) MaybeCheckForUpdate() (Metadata, error)

MaybeCheckForUpdate checks for an update if it has been long enough since the last check. It returns the metadata or an error if it executes a check. It returns a zero Metadata value if it does not check for an update.

func (*Updater) Update

func (u *Updater) Update() error

Update downloads the most recent version and replaces the current version.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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