selfupdater

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2024 License: GPL-3.0 Imports: 13 Imported by: 0

README

ghr-self-update

A small library to create self-updating app in Go based on github releases

Usage

  1. Download the dependency

go get github.com/mJehanno/ghr-self-updater

go get github.com/blang/semver/v4

  1. Instanciate new Updater
//main.go 
package main

import(
    "context"

    "github.com/blang/semver/v4"
    selfupdate "github.com/mJehanno/ghr-self-updater"
)

func main() {
    ctx := context.Background()
    current = semver.MustParse("1.2.3")
    updater := selfupdate.New("mJehanno", "gtop", current, WithContext(ctx)) // ownerName, repoName, currentVersion, and a bunch of options that are not required.
}
  1. Check for latest and Update

either in two steps:

//main.go 
package main

import(
    "context"

    "github.com/blang/semver/v4"
    selfupdate "github.com/mJehanno/ghr-self-updater"
)

func main() {
    ctx := context.Background()
    current = semver.MustParse("1.2.3")
    updater := selfupdate.New("mJehanno", "gtop", current, WithContext(ctx)) // ownerName, repoName, currentVersion, and a bunch of options that are not required.

    latest,err := updater.CheckLatest()
    if err != nil {
        // handleErr
    }

    if !latest {
        // here you can ask for user consent for example
        err := updater.Update()
        if err != nil {
            // handleErr
        }
    }
}

or in one step:

//main.go 
package main

import(
    "context"

    "github.com/blang/semver/v4"
    selfupdate "github.com/mJehanno/ghr-self-updater"
)

func main() {
    ctx := context.Background()
    current = semver.MustParse("1.2.3")
    updater := selfupdate.New("mJehanno", "gtop", current, WithContext(ctx)) // ownerName, repoName, currentVersion, and a bunch of options that are not required.

    latest,err := updater.CheckAndUpdate()
    if err != nil {
        // handleErr
    }
}

Documentation

Overview

Package selfupdater implements logic behind self-updating App. It uses github releases to update your app.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Updater

type Updater struct {
	Owner   string
	Repo    string
	Current semver.Version
	// contains filtered or unexported fields
}

Updater is the main structure in charge to check latest version and update your app.

func New

func New(owner, repo string, current semver.Version, options ...UpdaterOpts) *Updater

New creates a new instance of Updater. It needs the owner and repo name to work and the current version of your app (in semver format -> [semver package]) You can pass some options (WithContext, WithHttpClient) so that the updater can fits your need. If you don't, the Updater will use context.Background and http.DefaultClient by default. [semver package]: https://github.com/blang/semver

func (*Updater) CheckAndUpdate

func (u *Updater) CheckAndUpdate() error

CheckAndUpdate will perform both the Updater.CheckLatest and Updater.Update actions. It may seems a better solution for the developper as you don't have to do some plumbering but it enforce the user to update the application.

func (*Updater) CheckLatest

func (u *Updater) CheckLatest() (bool, error)

CheckLatest will check if the current version is the latest. It returns a boolean and an error. To avoid wrong behaviour, it returns true if an error is encountered.

func (*Updater) Update

func (u *Updater) Update() error

Update will perfom the update process which means : 1. Retrieve the corresponding asset (based on platform - os/arch - it needs to appear in the name like `my-super-app_linux-amd64`). 2. Download latest release asset for the current platform (os/arch). 3. Rename the current process executable with a `-old` suffix. 4. Give execution permission to the new executable. 5. Try to launch the new executable. 6. Try to rollack if it fails by removing the download executable and remove the `-old` suffix.

type UpdaterOpts

type UpdaterOpts func(*Updater)

UpdaterOpts represent an option you can pass to Updater constructor. It uses functional option pattern so the underlying type is `func(*Updater)`.

func WithContext

func WithContext(ctx context.Context) UpdaterOpts

WithContext will pass the given context to an Updater instance.

func WithHttpClient

func WithHttpClient(client *http.Client) UpdaterOpts

WithHttpClient will pass the given *http.Client to an Updater instance.

Jump to

Keyboard shortcuts

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