updater

package module
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2022 License: MIT Imports: 5 Imported by: 6

README

go-updater

GoDoc CircleCI

Usage

a software which using go-updater

package main

import (
	"fmt"

	version "github.com/hashicorp/go-version"
	updater "github.com/ktr0731/go-updater"
)

var current = version.Must(version.NewSemver("0.1.0"))

func main() {
	// determine what use means for update this software
	// in this example, use GitHub release
	u := updater.New(current, github.NewGitHubReleaseMeans("ktr0731", "evans"))

	// in default, update if minor update found
	u.UpdateIf = updater.FoundPatchUpdate

	updatable, latest, _ := u.Updatable()
	if updatable {
		_ = u.Update()
		fmt.Println("update from %s to %s", current, latest)
	} else {
		fmt.Println("already latest")
	}
}

more advanced step.

package main

import (
	"context"
	"encoding/json"
	"fmt"
	"os"

	version "github.com/hashicorp/go-version"
	updater "github.com/ktr0731/go-updater"
	"github.com/ktr0731/go-updater/brew"
	"github.com/ktr0731/go-updater/github"
)

var current = version.Must(version.NewSemver("0.1.0"))

type Config struct {
	UpdateBy string `json:"updateBy"`
}

func main() {
	f, _ := os.Open("config.json")
	defer f.Close()

	cfg := Config{}
	json.NewDecoder(f).Decode(&cfg)

	var m updater.Means
	switch cfg.UpdateBy {
	case "brew":
		m = brew.NewHomebrewMeans("ktr0731/evans", "evans")
	case "gh-release":
		m = github.NewGitHubReleaseMeans("ktr0731", "evans")
	default:
		panic("unknown means")
	}

	u := updater.New(current, m)

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
	var latest *version.Version
	go func() {
		defer cancel()
		var updatable bool
		updatable, latest, _ = u.Updatable()
		if updatable {
			_ = u.Update()
		}
	}()

	// do something

	// always call cancel.
	// because if updating is WIP, need to cancel for stop application immediately.
	// if updating is finished, cancel() do nothing.
	cancel()
	<-ctx.Done()
	if latest != nil {
		fmt.Println("updated from %s to %s", current, latest)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrUnavailable is used when the means unavailable.
	// For example, Homebrew cannot be used from Linux.
	//
	// The condition is checked at the constructor like github.New(...).
	// Clients which use go-updater must check this error.
	ErrUnavailable = errors.New("unavailable means")
)

Functions

This section is empty.

Types

type Means

type Means interface {
	LatestTag(context.Context) (*version.Version, error)
	Update(context.Context, *version.Version) error
	Installed(context.Context) bool

	CommandText(*version.Version) string

	Type() MeansType
}

Means manages methods related to specified update means. For example, fetches the latest tag, update binary, or check whether the software is installed by this.

func NewMeans

func NewMeans(b MeansBuilder) (Means, error)

func SelectAvailableMeansFrom

func SelectAvailableMeansFrom(ctx context.Context, ma ...MeansBuilder) (Means, error)

type MeansBuilder

type MeansBuilder func() (Means, error)

type MeansType

type MeansType string

type UpdateCondition

type UpdateCondition func(*version.Version, *version.Version) bool

UpdateCondition is the condition for update the binary. when the binary will be updated, always go-updater checks the condition specified by this.

var (
	FoundMajorUpdate UpdateCondition = func(current, latest *version.Version) bool {
		return current.LessThan(latest) && current.Segments()[0] < latest.Segments()[0]
	}

	FoundMinorUpdate UpdateCondition = func(current, latest *version.Version) bool {
		return current.LessThan(latest) && (current.Segments()[0] < latest.Segments()[0] || current.Segments()[1] < latest.Segments()[1])
	}

	FoundPatchUpdate UpdateCondition = func(current, latest *version.Version) bool {
		return current.LessThan(latest)
	}
)

type Updater

type Updater struct {
	UpdateIf UpdateCondition
	// contains filtered or unexported fields
}

Updater updates the binary using with UpdateCondition and Means. updating can be executed by m when UpdateIf is true.

func New

func New(current *version.Version, m Means) *Updater

func (*Updater) PrintInstruction

func (u *Updater) PrintInstruction(w io.Writer, v *version.Version) error

func (*Updater) Updatable

func (u *Updater) Updatable(ctx context.Context) (bool, *version.Version, error)

func (*Updater) Update

func (u *Updater) Update(ctx context.Context) error

Update updates the binary if Updatable()

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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