Documentation
¶
Overview ¶
go-selfupdate detects the information of the latest release via GitHub Releases API and checks the current version. If newer version than itself is detected, it downloads released binary from GitHub and replaces itself.
- Automatically detects the latest version of released binary on GitHub
- Retrieve the proper binary for the OS and arch where the binary is running
- Update the binary with rollback support on failure
- Tested on Linux, macOS and Windows
- Many archive and compression formats are supported (zip, gzip, xzip, bzip2, tar)
There are some naming rules. Please read following links.
Naming Rules of Released Binaries:
https://github.com/creativeprojects/go-selfupdate#naming-rules-of-released-binaries
Naming Rules of Git Tags:
https://github.com/creativeprojects/go-selfupdate#naming-rules-of-versions-git-tags
This package is hosted on GitHub:
https://github.com/creativeprojects/go-selfupdate
Small CLI tools as wrapper of this library are available also:
https://github.com/creativeprojects/go-selfupdate/cmd/detect-latest-release https://github.com/creativeprojects/go-selfupdate/cmd/go-get-release
Index ¶
- Variables
- func DecompressCommand(src io.Reader, url, cmd, os, arch string) (io.Reader, error)
- func SetLogger(logger Logger)
- func UpdateTo(assetURL, assetFileName, cmdPath string) error
- type ChecksumValidator
- type Config
- type ECDSAValidator
- type GitHubAsset
- type GitHubConfig
- type GitHubRelease
- func (r *GitHubRelease) GetAssets() []SourceAsset
- func (r *GitHubRelease) GetDraft() bool
- func (a *GitHubRelease) GetID() int64
- func (r *GitHubRelease) GetName() string
- func (r *GitHubRelease) GetPrerelease() bool
- func (r *GitHubRelease) GetPublishedAt() time.Time
- func (r *GitHubRelease) GetReleaseNotes() string
- func (r *GitHubRelease) GetTagName() string
- func (r *GitHubRelease) GetURL() string
- type GitHubSource
- type GiteaAsset
- type GiteaConfig
- type GiteaRelease
- func (r *GiteaRelease) GetAssets() []SourceAsset
- func (r *GiteaRelease) GetDraft() bool
- func (r *GiteaRelease) GetID() int64
- func (r *GiteaRelease) GetName() string
- func (r *GiteaRelease) GetPrerelease() bool
- func (r *GiteaRelease) GetPublishedAt() time.Time
- func (r *GiteaRelease) GetReleaseNotes() string
- func (r *GiteaRelease) GetTagName() string
- func (r *GiteaRelease) GetURL() string
- type GiteaSource
- type Logger
- type Release
- type SHAValidator
- type Source
- type SourceAsset
- type SourceRelease
- type Updater
- func (up *Updater) DetectLatest(slug string) (release *Release, found bool, err error)
- func (up *Updater) DetectVersion(slug string, version string) (release *Release, found bool, err error)
- func (up *Updater) UpdateCommand(cmdPath string, current string, slug string) (*Release, error)
- func (up *Updater) UpdateSelf(current string, slug string) (*Release, error)
- func (up *Updater) UpdateTo(rel *Release, cmdPath string) error
- type Validator
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrInvalidSlug = errors.New("invalid slug format, expected 'owner/name'") ErrIncorrectParameterOwner = errors.New("incorrect parameter \"owner\"") ErrIncorrectParameterRepo = errors.New("incorrect parameter \"repo\"") ErrAssetNotFound = errors.New("asset not found") ErrIncorrectChecksumFile = errors.New("incorrect checksum file format") ErrChecksumValidationFailed = errors.New("sha256 validation failed") ErrHashNotFound = errors.New("hash not found in checksum file") ErrECDSAValidationFailed = errors.New("ECDSA signature verification failed") ErrInvalidECDSASignature = errors.New("invalid ECDSA signature") )
Possible errors returned
Functions ¶
func DecompressCommand ¶
DecompressCommand decompresses the given source. Archive and compression format is automatically detected from 'url' parameter, which represents the URL of asset. This returns a reader for the decompressed command given by 'cmd'. '.zip', '.tar.gz', '.tar.xz', '.tgz', '.gz', '.bz2' and '.xz' are supported.
func SetLogger ¶
func SetLogger(logger Logger)
SetLogger redirects all logs to the logger defined in parameter. By default logs are not sent anywhere.
Example ¶
// you can plug-in any logger providing the 2 methods Print and Printf // the default log.Logger satisfies the interface logger := stdlog.New(os.Stdout, "selfupdate ", 0) SetLogger(logger)
func UpdateTo ¶
UpdateTo downloads an executable from assetURL and replaces the current binary with the downloaded one. This function is low-level API to update the binary. Because it does not use a source provider and downloads asset directly from the URL via HTTP, this function is not available to update a release for private repositories. cmdPath is a file path to command executable.
Types ¶
type ChecksumValidator ¶
type ChecksumValidator struct {
// UniqueFilename is the name of the global file containing all the checksums
// Usually "checksums.txt", "SHA256SUMS", etc.
UniqueFilename string
}
ChecksumValidator is a SHA256 checksum validator where all the validation hash are in a single file (one per line)
func (*ChecksumValidator) GetValidationAssetName ¶
func (v *ChecksumValidator) GetValidationAssetName(releaseFilename string) string
GetValidationAssetName returns the unique asset name for SHA256 validation.
type Config ¶
type Config struct {
// Source where to load the releases from (example: GitHubSource)
Source Source
// Validator represents types which enable additional validation of downloaded release.
Validator Validator
// Filters are regexp used to filter on specific assets for releases with multiple assets.
// An asset is selected if it matches any of those, in addition to the regular tag, os, arch, extensions.
// Please make sure that your filter(s) uniquely match an asset.
Filters []string
// OS is set to the value of runtime.GOOS by default, but you can force another value here
OS string
// Arch is set to the value of runtime.GOARCH by default, but you can force another value here
Arch string
// Arm 32bits version. Valid values are 0 (unknown), 5, 6 or 7. Default is detected value (if any)
Arm uint8
// Draft permits an upgrade to a "draft" version (default to false)
Draft bool
// Prerelease permits an upgrade to a "pre-release" version (default to false)
Prerelease bool
}
Config represents the configuration of self-update.
type ECDSAValidator ¶
ECDSAValidator specifies a ECDSA validator for additional file validation before updating.
func (*ECDSAValidator) GetValidationAssetName ¶
func (v *ECDSAValidator) GetValidationAssetName(releaseFilename string) string
GetValidationAssetName returns the asset name for ECDSA validation.
type GitHubAsset ¶
type GitHubAsset struct {
// contains filtered or unexported fields
}
func NewGitHubAsset ¶
func NewGitHubAsset(from *github.ReleaseAsset) *GitHubAsset
func (*GitHubAsset) GetBrowserDownloadURL ¶
func (a *GitHubAsset) GetBrowserDownloadURL() string
func (*GitHubAsset) GetID ¶
func (a *GitHubAsset) GetID() int64
func (*GitHubAsset) GetName ¶
func (a *GitHubAsset) GetName() string
func (*GitHubAsset) GetSize ¶
func (a *GitHubAsset) GetSize() int
type GitHubConfig ¶
type GitHubConfig struct {
// APIToken represents GitHub API token. If it's not empty, it will be used for authentication of GitHub API
APIToken string
// EnterpriseBaseURL is a base URL of GitHub API. If you want to use this library with GitHub Enterprise,
// please set "https://{your-organization-address}/api/v3/" to this field.
EnterpriseBaseURL string
// EnterpriseUploadURL is a URL to upload stuffs to GitHub Enterprise instance. This is often the same as an API base URL.
// So if this field is not set and EnterpriseBaseURL is set, EnterpriseBaseURL is also set to this field.
EnterpriseUploadURL string
// Context used by the http client (default to context.Background)
Context context.Context
}
GitHubConfig is an object to pass to NewGitHubSource
type GitHubRelease ¶
type GitHubRelease struct {
// contains filtered or unexported fields
}
func NewGitHubRelease ¶
func NewGitHubRelease(from *github.RepositoryRelease) *GitHubRelease
func (*GitHubRelease) GetAssets ¶
func (r *GitHubRelease) GetAssets() []SourceAsset
func (*GitHubRelease) GetDraft ¶
func (r *GitHubRelease) GetDraft() bool
func (*GitHubRelease) GetID ¶ added in v0.5.0
func (a *GitHubRelease) GetID() int64
func (*GitHubRelease) GetName ¶
func (r *GitHubRelease) GetName() string
func (*GitHubRelease) GetPrerelease ¶
func (r *GitHubRelease) GetPrerelease() bool
func (*GitHubRelease) GetPublishedAt ¶
func (r *GitHubRelease) GetPublishedAt() time.Time
func (*GitHubRelease) GetReleaseNotes ¶
func (r *GitHubRelease) GetReleaseNotes() string
func (*GitHubRelease) GetTagName ¶
func (r *GitHubRelease) GetTagName() string
func (*GitHubRelease) GetURL ¶
func (r *GitHubRelease) GetURL() string
type GitHubSource ¶
type GitHubSource struct {
// contains filtered or unexported fields
}
GitHubSource is used to load release information from GitHub
func NewGitHubSource ¶
func NewGitHubSource(config GitHubConfig) (*GitHubSource, error)
NewGitHubSource creates a new GitHubSource from a config object. It initializes a GitHub API client. If you set your API token to the $GITHUB_TOKEN environment variable, the client will use it. You can pass an empty GitHubSource{} to use the default configuration The function will return an error if the GitHub Entreprise URLs in the config object cannot be parsed
func (*GitHubSource) DownloadReleaseAsset ¶
func (s *GitHubSource) DownloadReleaseAsset(owner, repo string, releaseID, id int64) (io.ReadCloser, error)
DownloadReleaseAsset downloads an asset from its ID. It returns an io.ReadCloser: it is your responsability to Close it. Please note releaseID is not used by GitHubSource.
func (*GitHubSource) ListReleases ¶
func (s *GitHubSource) ListReleases(owner, repo string) ([]SourceRelease, error)
ListReleases returns all available releases
type GiteaAsset ¶ added in v0.5.0
type GiteaAsset struct {
// contains filtered or unexported fields
}
func NewGiteaAsset ¶ added in v0.5.0
func NewGiteaAsset(from *gitea.Attachment) *GiteaAsset
func (*GiteaAsset) GetBrowserDownloadURL ¶ added in v0.5.0
func (a *GiteaAsset) GetBrowserDownloadURL() string
func (*GiteaAsset) GetID ¶ added in v0.5.0
func (a *GiteaAsset) GetID() int64
func (*GiteaAsset) GetName ¶ added in v0.5.0
func (a *GiteaAsset) GetName() string
func (*GiteaAsset) GetSize ¶ added in v0.5.0
func (a *GiteaAsset) GetSize() int
type GiteaConfig ¶ added in v0.5.0
type GiteaConfig struct {
// APIToken represents Gitea API token. If it's not empty, it will be used for authentication for the API
APIToken string
// BaseURL is a base URL of your gitea instance
BaseURL string
// Context used by the http client (default to context.Background)
Context context.Context
}
GiteaConfig is an object to pass to NewGiteaSource
type GiteaRelease ¶ added in v0.5.0
type GiteaRelease struct {
// contains filtered or unexported fields
}
func NewGiteaRelease ¶ added in v0.5.0
func NewGiteaRelease(from *gitea.Release) *GiteaRelease
func (*GiteaRelease) GetAssets ¶ added in v0.5.0
func (r *GiteaRelease) GetAssets() []SourceAsset
func (*GiteaRelease) GetDraft ¶ added in v0.5.0
func (r *GiteaRelease) GetDraft() bool
func (*GiteaRelease) GetID ¶ added in v0.5.0
func (r *GiteaRelease) GetID() int64
func (*GiteaRelease) GetName ¶ added in v0.5.0
func (r *GiteaRelease) GetName() string
func (*GiteaRelease) GetPrerelease ¶ added in v0.5.0
func (r *GiteaRelease) GetPrerelease() bool
func (*GiteaRelease) GetPublishedAt ¶ added in v0.5.0
func (r *GiteaRelease) GetPublishedAt() time.Time
func (*GiteaRelease) GetReleaseNotes ¶ added in v0.5.0
func (r *GiteaRelease) GetReleaseNotes() string
func (*GiteaRelease) GetTagName ¶ added in v0.5.0
func (r *GiteaRelease) GetTagName() string
func (*GiteaRelease) GetURL ¶ added in v0.5.0
func (r *GiteaRelease) GetURL() string
type GiteaSource ¶ added in v0.5.0
type GiteaSource struct {
// contains filtered or unexported fields
}
GiteaSource is used to load release information from Gitea
func NewGiteaSource ¶ added in v0.5.0
func NewGiteaSource(config GiteaConfig) (*GiteaSource, error)
NewGiteaSource creates a new NewGiteaSource from a config object. It initializes a Gitea API Client. If you set your API token to the $GITEA_TOKEN environment variable, the client will use it. You can pass an empty GiteaSource{} to use the default configuration
func (*GiteaSource) DownloadReleaseAsset ¶ added in v0.5.0
func (s *GiteaSource) DownloadReleaseAsset(owner, repo string, releaseID, id int64) (io.ReadCloser, error)
DownloadReleaseAsset downloads an asset from its ID. It returns an io.ReadCloser: it is your responsability to Close it.
func (*GiteaSource) ListReleases ¶ added in v0.5.0
func (s *GiteaSource) ListReleases(owner, repo string) ([]SourceRelease, error)
ListReleases returns all available releases
type Logger ¶
type Logger interface {
// Print calls Output to print to the standard logger. Arguments are handled in the manner of fmt.Print.
Print(v ...interface{})
// Printf calls Output to print to the standard logger. Arguments are handled in the manner of fmt.Printf.
Printf(format string, v ...interface{})
}
Logger interface. Compatible with standard log.Logger
type Release ¶
type Release struct {
// AssetURL is a URL to the uploaded file for the release
AssetURL string
// AssetSize represents the size of asset in bytes
AssetByteSize int
// AssetID is the ID of the asset on the source platform
AssetID int64
// ReleaseID is the ID of the release on the source platform
ReleaseID int64
// AssetName is the filename of the asset
AssetName string
// ValidationAssetID is the ID of additional validaton asset on the source platform
ValidationAssetID int64
// URL is a URL to release page for browsing
URL string
// ReleaseNotes is a release notes of the release
ReleaseNotes string
// Name represents a name of the release
Name string
// PublishedAt is the time when the release was published
PublishedAt time.Time
// OS this release is for
OS string
// Arch this release is for
Arch string
// Arm 32bits version (if any). Valid values are 0 (unknown), 5, 6 or 7
Arm uint8
// contains filtered or unexported fields
}
Release represents a release asset for current OS and arch.
func DetectLatest ¶
DetectLatest detects the latest release of the slug (owner/repo). This function is a shortcut version of updater.DetectLatest.
func DetectVersion ¶
DetectVersion detects the given release of the slug (owner/repo) from its version.
func UpdateCommand ¶
UpdateCommand updates a given command binary to the latest version. This function is a shortcut version of updater.UpdateCommand.
func UpdateSelf ¶
UpdateSelf updates the running executable itself to the latest version. This function is a shortcut version of updater.UpdateSelf.
func (Release) GreaterOrEqual ¶
GreaterOrEqual tests if one version is greater than or equal to another one.
func (Release) GreaterThan ¶
GreaterThan tests if one version is greater than another one.
func (Release) LessOrEqual ¶
LessOrEqual tests if one version is less than or equal to another one.
type SHAValidator ¶
type SHAValidator struct {
}
SHAValidator specifies a SHA256 validator for additional file validation before updating.
func (*SHAValidator) GetValidationAssetName ¶
func (v *SHAValidator) GetValidationAssetName(releaseFilename string) string
GetValidationAssetName returns the asset name for SHA256 validation.
type Source ¶
type Source interface {
ListReleases(owner, repo string) ([]SourceRelease, error)
DownloadReleaseAsset(owner, repo string, releaseID, id int64) (io.ReadCloser, error)
}
Source interface to load the releases from (GitHubSource for example)
type SourceAsset ¶
type SourceRelease ¶
type Updater ¶
type Updater struct {
// contains filtered or unexported fields
}
Updater is responsible for managing the context of self-update.
func DefaultUpdater ¶
func DefaultUpdater() *Updater
DefaultUpdater creates a new updater instance with default configuration. It initializes GitHub API client with default API base URL. If you set your API token to $GITHUB_TOKEN, the client will use it. Every call to this function will always return the same instance, it's only created once
func NewUpdater ¶
NewUpdater creates a new updater instance. If you don't specify a source in the config object, GitHub will be used
func (*Updater) DetectLatest ¶
DetectLatest tries to get the latest version from the source provider. 'slug' means 'owner/name' formatted string. It fetches releases information from the source provider and find out the latest release with matching the tag names and asset names. Drafts and pre-releases are ignored. Assets would be suffixed by the OS name and the arch name such as 'foo_linux_amd64' where 'foo' is a command name. '-' can also be used as a separator. File can be compressed with zip, gzip, zxip, bzip2, tar&gzip or tar&zxip. So the asset can have a file extension for the corresponding compression format such as '.zip'. On Windows, '.exe' also can be contained such as 'foo_windows_amd64.exe.zip'.
func (*Updater) DetectVersion ¶
func (up *Updater) DetectVersion(slug string, version string) (release *Release, found bool, err error)
DetectVersion tries to get the given version of the repository on the source. `slug` means `owner/name` formatted string. And version indicates the required version.
func (*Updater) UpdateCommand ¶
UpdateCommand updates a given command binary to the latest version. 'slug' represents 'owner/name' repository on GitHub and 'current' means the current version.
func (*Updater) UpdateSelf ¶
UpdateSelf updates the running executable itself to the latest version. 'slug' represents 'owner/name' repository on GitHub and 'current' means the current version.
type Validator ¶
type Validator interface {
// Validate validates release bytes against an additional asset bytes.
// See SHAValidator or ECDSAValidator for more information.
Validate(filename string, release, asset []byte) error
// GetValidationAssetName returns the additional asset name containing the validation checksum.
// The asset containing the checksum can be based on the release asset name
GetValidationAssetName(releaseFilename string) string
}
Validator represents an interface which enables additional validation of releases.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
detect-latest-release
command
|
|
|
detect-latest-release-custom
command
|
|
|
go-get-release
command
|
|
|
go-get-release-custom
command
|
|
|
Package update provides functionality to implement secure, self-updating Go programs (or other single-file targets).
|
Package update provides functionality to implement secure, self-updating Go programs (or other single-file targets). |