gosu

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2024 License: MIT Imports: 13 Imported by: 0

README

gosu

A package for self updating Go applications

Build Status Go Report Card Go Docs License GitHub tag

A package for self updating Go applications. Gets the latest application release from the project's Github repository (public or private), when a new version is detected, downloads the update archive, upgrades the application and restarts it automatically. Works in Windows and Linux.

Works in Go v1.18+.

Contents

Install

go get github.com/alexandermac/gosu

Usage

package main

import (
	"fmt"
	"log"
	"os"
	"time"

	"github.com/alexandermac/gosu"
)

type AppUpdater struct {
	gosu *gosu.Updater
}

func NewUpdater(appVersion string) AppUpdater {
	return AppUpdater{
		gosu: gosu.New(
			os.Getenv("GH_ORG_NAME"),     // organization name + project name
			os.Getenv("GH_ACCESS_TOKEN"), // github access token to access private repos
			appVersion,                   // local version of the app
		),
	}
}

func (updater *AppUpdater) CheckUpdates() bool {
	result := updater.gosu.CheckUpdates()

	switch result.Code {
	case gosu.CODE_LATEST_VERSION_IS_ALREADY_IN_USE, gosu.CODE_UNRELEASED_VERSION_IS_IN_USE, gosu.CODE_NEW_VERSION_DETECTED:
		fmt.Println(">>>", result.Message, result.Details)
	case gosu.CODE_ERROR:
		err := fmt.Errorf("%s. %s", result.Message, result.Details)
		log.Panic(err)
	}

	return result.Code == gosu.CODE_NEW_VERSION_DETECTED
}

func (updater *AppUpdater) DownloadUpdate() bool {
	progressCh := make(chan gosu.DownloadingProgress)
	go func() {
		for {
			progress, ok := <-progressCh
			if !ok {
				return
			}
			fmt.Printf("Asset downloading progress: %d/%d\n", progress.CurrentSize, progress.TotalSize)
		}
	}()

	result := updater.gosu.DownloadAsset(progressCh)
	switch result.Code {
	case gosu.CODE_DOWNLOADING_CANCELLED, gosu.CODE_DOWNLOADING_COMPLETED:
		fmt.Println(">>>", result.Message, result.Details)
	case gosu.CODE_ERROR:
		err := fmt.Errorf("%s. %s", result.Message, result.Details)
		log.Panic(err)
	}

	return result.Code == gosu.CODE_DOWNLOADING_COMPLETED
}

func (updater *AppUpdater) CancelDownloading() {
	updater.gosu.CancelAssetDownloading()
}

func (updater *AppUpdater) UpdateApp() {
	result := updater.gosu.UpdateApp()
	switch result.Code {
	case gosu.CODE_ERROR:
		err := fmt.Errorf("%s. %s", result.Message, result.Details)
		log.Panic(err)
	}
}

func main() {
	updater := NewUpdater("1.3.0")
	result := updater.CheckUpdates()
	if !result {
		return
	}

	go func() {
		time.Sleep(time.Second * 5)
		updater.CancelDownloading()
	}()
	result = updater.DownloadUpdate()
	if !result {
		return
	}

	updater.UpdateApp()
}

API

New(orgRepoName, ghAccessToken, localVersion string) *Updater

Creates a new instance of gosu.Updater.

gosu := gosu.New(
	"alexandermac/superapp", // organization name + project name
	"",                      // github access token to access private repos
	"1.3.0",                 // local version of the app
)
SetLogger(l Logger)

Sets a custom logger instead of the standard log used by default. The provided logger must satisfy the Logger interface.

gosu.SetLogger(logrus.StandardLogger())
CheckUpdates() UpdateResult

Checks for application's updates. Returns struct with code and message indicating that new version exists or not.

DownloadAsset(progressCh chan<- DownloadingProgress) UpdateResult

Downloads a release asset. Accepts an optional channel to get downloading progress notifications.

CancelAssetDownloading()

Cancels an asset downloading.

UpdateApp() UpdateResult

Installs the downloaded update.

License

Licensed under the MIT license.

Author

Alexander Mac

Documentation

Index

Constants

View Source
const (
	// CheckUpdates
	CODE_LATEST_VERSION_IS_ALREADY_IN_USE = iota
	CODE_UNRELEASED_VERSION_IS_IN_USE     = iota
	CODE_NEW_VERSION_DETECTED             = iota
	// DownloadAsset
	CODE_DOWNLOADING_CANCELLED = iota
	CODE_DOWNLOADING_COMPLETED = iota
	CODE_ERROR                 = iota
)

Variables

This section is empty.

Functions

func SetLogger added in v0.2.0

func SetLogger(l Logger)

Types

type DownloadingProgress added in v0.4.0

type DownloadingProgress struct {
	TotalSize       int
	CurrentSize     int
	ProgressPercent int
}

type Logger added in v0.2.0

type Logger interface {
	Debug(args ...any)
	Info(args ...any)
	Warn(args ...any)
	Error(args ...any)

	Debugf(format string, args ...any)
	Infof(format string, args ...any)
	Warnf(format string, args ...any)
	Errorf(format string, args ...any)
}

type UpdateResult added in v0.4.0

type UpdateResult struct {
	Code    int
	Message string
	Details string
}

type Updater

type Updater struct {
	ReleasesUrl       string
	ChangelogUrl      string
	LocalVersion      string
	GhAccessToken     string
	DownloadChangelog bool
	// contains filtered or unexported fields
}

func New

func New(orgRepoName, ghAccessToken, localVersion string) *Updater

func (*Updater) CancelAssetDownloading added in v0.4.0

func (updater *Updater) CancelAssetDownloading()

func (*Updater) CheckUpdates

func (updater *Updater) CheckUpdates() UpdateResult

func (*Updater) DownloadAsset added in v0.4.0

func (updater *Updater) DownloadAsset(progressCh chan<- DownloadingProgress) UpdateResult

func (*Updater) UpdateApp added in v0.4.0

func (updater *Updater) UpdateApp() UpdateResult

Jump to

Keyboard shortcuts

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