updater

package
v1.13.0 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2021 License: Apache-2.0 Imports: 30 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrNoNewRelease = errors.New("no new release")
	ErrNoAsset      = errors.New("no asset found")

	AssetSeperators = []string{"_", "-"}
	AssetExtensions = []string{".tar.gz", ""}
)

Functions

func GetUserInput

func GetUserInput() (string, error)

func GetYesOrNoInput

func GetYesOrNoInput(ctx context.Context) (bool, error)

func NeedsUpdate

func NeedsUpdate(ctx context.Context, log logrus.FieldLogger, repo, version string, disabled,
	debugLog, includePrereleases, forceCheck bool) bool

`NeedsUpdate` reads a GitHub token from `~/.outreach/github.token`. If it can't, it prompts the user and saves it to the path above. GitHub Releases is checked for a new release, and if it's found, updates the base binary. If an update is required, it returns `true`.

If `repo` is not provided, it is determined via `debug.ReadBuildInfo` and the path the binary was built from. `repo` should be in the format of `getoutreach/$repoName`.

`version` is usually the value of `gobox/pkg/app.Version`, which is set at build time.

If `debugLog` is set to true, then the core updater's debug logging will be enabled.

Update checks can be disabled by setting `disabled` to true.

Example
package main

import (
	"context"
	"os"

	"github.com/getoutreach/gobox/pkg/updater"
	"github.com/sirupsen/logrus"
)

func main() {
	if updater.NeedsUpdate(context.Background(), logrus.New(), "", "v1.0.0", false, false, false, false) {
		// Stop to use the newer version
		os.Exit(0)
	}
}
Output:

Types

type Github

type Github struct {

	// Configuration Options
	Silent bool
	// contains filtered or unexported fields
}

func NewGithubUpdater

func NewGithubUpdater(ctx context.Context, token cfg.SecretData, org, repo string) *Github

NewGithubUpdater creates a new updater powered by Github

func NewGithubUpdaterWithClient

func NewGithubUpdaterWithClient(ctx context.Context, client *github.Client, org, repo string) *Github

NewGithubUpdaterWithClient creates a new updater with the provided Github Client

func (*Github) Check

func (g *Github) Check(ctx context.Context) error

Check checks if the credentials / repo are valid.

func (*Github) DownloadRelease

func (g *Github) DownloadRelease(ctx context.Context, r *github.RepositoryRelease, assetName, execName string) (downloadedBinary string, cleanup func(), err error)

DownloadRelease attempts to download a binary from a release.

If the asset found is an archive, it'll be extracted and the value of `execName` will be used to pull a file out of the root of the archive. If `execName` is not provided it is inferred as the name of the currently running basename of the running executable. The downloaded file is returned as `downloadedBinary` with a cleanup function being returned to remove all leftover data.

The cleanup function should be called even when an error occurs

func (*Github) GetLatestVersion

func (g *Github) GetLatestVersion(ctx context.Context, currentVersion string, includePrereleases bool) (*github.RepositoryRelease, error)

GetLatestVersion finds the latest release, based on semver, of a Github Repository (supplied when client was created). This is determined by the following algorithm:

Finding new release:

Github releases are then streaming evaluated to find the currentVersion. All releases
that are not == to the current version end up being stored in memory as "candidates"
for being evaluated as a possible new version. If the current version is not found
then it is ignored.

Including pre-releases:

If the current version is a pre-release:
 - pre-releases are considered
If includePrereleases is true
 - pre-releases are considered

Selecting a new version:

Once the current releases has been found (or not found) then all versions found before
it are considered as candidates and checked to see if a newer release exists. Using the
aforementioned pre-release logic pre-releases are included based on that.

func (*Github) GetRelease

func (g *Github) GetRelease(ctx context.Context, version string) (*github.RepositoryRelease, error)

GetRelease finds a release with a given version (tag)

func (*Github) ReplaceRunning

func (g *Github) ReplaceRunning(ctx context.Context, newBinary string) error

ReplaceRunning replaces the running executable with the specified path. This path is renamed to the current executable.

The running process is replaced with a new invocation of the new binary.

func (*Github) SelectAsset

func (g *Github) SelectAsset(ctx context.Context, assets []*github.ReleaseAsset, name string) (string, *github.ReleaseAsset, error)

SelectAsset finds an asset on a Github Release. This looks up the following file patterns: - name_GOOS_GOARCH - name_version_GOOS_GOARCH - name_GOOS_GOARCH.tar.gz - name_version_GOOS_GOARCH.tar.gz

type LastUpdateCheck

type LastUpdateCheck struct {
	Date       time.Time     `yaml:"date"`
	CheckEvery time.Duration `yaml:"checkEvery"`
}

Jump to

Keyboard shortcuts

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