updater

package module
v0.0.0-...-9aceb99 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2018 License: MIT Imports: 10 Imported by: 0

README

updater: golang library for self updating programs

Features

  • update from repository (HTML index)
  • Semantic Versioning support This repository is a golang library that provides an auto-updater embeddable in your go binary.

Updates are to be fetched from a repository. Currently only basic HTML indexes are supported, such as those generated by Sonatype Nexus.

Installation

You are free to go get the lib as any golang library but the prefered way to install it is using dep.

dep ensure -add github.com/fabienm/updater

Code examples

For the sake of simplicity, errors are silenced in this example.

package main
import (
	"fmt"
	"os"
	"strings"
	
	"github.com/fabienm/updater"
)

var version = "1.0.1"

func main() {
	u:= updater.New(updater.Config{
		BinaryName: "mybinary",
		Repository:"http://example/nexus/content/repositories/releases",
	})
	build, _:= u.FindLatest()

	if build.NewerThan(version) {
		fmt.Printf("Do you want to update to %s? [Y/n]\n", build.Version)
		input:= "y"
		fmt.Scanln(&input)
		if strings.ToLower(input[0:0]) == "y" {
			u.UpdateTo(build)
			os.Exit(0)
		} 
	}
}

Documentation

Overview

Package updater provides a tool to update a binary relying from a http repository (such as Nexus)

Index

Constants

View Source
const (
	// FieldName is the binary name
	FieldName field = iota
	// FieldVersion is the binary version number
	FieldVersion
	// FieldOs is the binary target OS
	FieldOs
	// FieldArch is the binary target architecture
	FieldArch
)
View Source
const (
	// SortSemver sorts repository entries by version number, according to semver
	SortSemver sortCriteria = iota
)

Variables

This section is empty.

Functions

This section is empty.

Types

type BuildInfo

type BuildInfo struct {
	Name    string
	File    string
	Version *semver.Version
	Os      string
	Arch    string
	MD5     string
	URL     string
}

BuildInfo is an entry of the repository

func (*BuildInfo) NewerThan

func (build *BuildInfo) NewerThan(version string) bool

NewerThan returns true if the referenced build is newer that the given version string.

type Config

type Config struct {
	// BinaryName is the base name of binary artifacts
	BinaryName string
	// TargetPath is the local path to update (default to os.Executable())
	TargetPath string
	// Fields is an ordered array describing filename structure, using field* constants
	Fields []field
	// FieldSeparator is the string separating fields in the filename
	FieldSeparator string
	// SortCriteria is the sort order that will be applied to find the latest version
	SortCriteria sortCriteria
	// Matcher is a pointer to the wanted Matcher func
	Matcher *Matcher
	// Repository is the url of the repository where updates are to be found
	Repository string
	// TmpPattern is a sprintf pattern defining the local temporary storage for downloaded files
	TmpPattern string
}

Config is the public configuration object to create an Updater

type Matcher

type Matcher func(info *BuildInfo) bool

Matcher is a function that can be called to know if a repository entry can be considered as a valid update

type Updater

type Updater struct {
	Config
}

Updater is the main object

func New

func New(config Config) Updater

New creates an updater with default values if they are missing in the Config

func (Updater) FindLatest

func (u Updater) FindLatest() (*BuildInfo, error)

FindLatest returns the latest eligible build in the repository. It finds all anchors in a html page and try to consider them as a valid build. The latest build that matches, according to the matcher and the sortCriteria order is returned.

func (Updater) UpdateTo

func (u Updater) UpdateTo(build *BuildInfo) error

UpdateTo download the referenced build and move it to the target path

Jump to

Keyboard shortcuts

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