goupdater

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 3, 2018 License: MIT Imports: 12 Imported by: 1

README

Goupdater

Easily build self-updating programs

Goupdater makes it easier for you to update your Go programs (or other single-file targets). A program can update itself by replacing its executable file with a new version.

It provides the flexibility to implement different updating user experiences like auto-updating, or manual user-initiated updates.

Install

We use dep to manage our dependencies. you can easily add this library to your application by doing:

Using dep

dep ensure --add github.com/italolelis/goupdater

Using go get

go get github.com/italolelis/goupdater

Usage

Goupdater allows you to have different resolvers from where to fetch your binaries. At the moment we only support github releases, but you can easily write your own resolver.

Here is an example of how you can update your application using the github releases resover:

// we define all the necessary configurations.
var (
    currentVersion = "v1.2.3"    // the version of your app
    githubToken    = "yourToken" // This is only required for private github repositories
    owner          = "hellofresh"
    repo           = "myRepo"
)

// We choose the github release resolver. You can implement your own resolver by implementing
// the goupdater.Resolver interface
resolver, err := goupdater.NewGithub(
    goupdater.WithToken(githubToken),
    goupdater.WithOwner(owner),
    goupdater.WithRepo(repo),
})
if err != nil {
    panic(err)
}

// Updates the running binary to the latest available version
updated, err := goupdater.Update(resolver, currentVersion)
if err != nil {
    panic(err)
}

if updated {
    fmt.Print("You are now using the latest version!")
} else {
    fmt.Print("You already have the latest version!")
}

You can also pass a context to it:

ctx := context.Background()

resolver, err := goupdater.NewGithubWithContext(
    ctx,
    goupdater.WithToken(githubToken),
    goupdater.WithOwner(owner),
    goupdater.WithRepo(repo),
})
if err != nil {
    panic(err)
}

updated, err := goupdater.UpdateWithContext(ctx, resolver, currentVersion)
if err != nil {
    panic(err)
}

if updated {
    fmt.Print("You are now using the latest version!")
} else {
    fmt.Print("You already have the latest version!")
}

Contributing

To start contributing, please check CONTRIBUTING.

Documentation

Go Docs: godoc.org/github.com/italolelis/goupdater

Documentation

Overview

Package goupdater helps you to easily build self-updating programs

Goupdater makes it easier for you to update your Go programs (or other single-file targets). A program can update itself by replacing its executable file with a new version. It provides the flexibility to implement different updating user experiences like auto-updating, or manual user-initiated updates.

Example (PrivateRepo)
package main

import (
	"fmt"

	"github.com/italolelis/goupdater"
)

func main() {
	// we define all the necessary configurations.
	var (
		currentVersion = "v1.2.3"    // the version of your app
		githubToken    = "yourToken" // This is only required for private github repositories
		owner          = "hellofresh"
		repo           = "myRepo"
	)

	// We choose the github release resolver. You can implement your own resolver by implementing
	// the goupdater.Resolver interface
	resolver, err := goupdater.NewGithub(goupdater.GithubOpts{
		Token: githubToken,
		Owner: owner,
		Repo:  repo,
	})
	if err != nil {
		panic(err)
	}

	// Updates the running binary to the latest available version
	updated, err := goupdater.Update(resolver, currentVersion)
	if err != nil {
		panic(err)
	}

	if updated {
		fmt.Print("You are now using the latest version!")
	} else {
		fmt.Print("You already have the latest version!")
	}
}
Output:

Example (PublicRepoWithContext)
package main

import (
	"context"
	"fmt"

	"github.com/italolelis/goupdater"
)

func main() {
	// we define all the necessary configurations.
	var (
		currentVersion = "v1.2.3" // the version of your app
		owner          = "hellofresh"
		repo           = "myRepo"
	)

	// we create a context to be used, this could also be a ContextTimeout that would
	// fail in case of a network error
	ctx := context.Background()

	// We choose the github release resolver. You can implement your own resolver by implementing
	// the goupdater.Resolver interface
	resolver, err := goupdater.NewGithubWithContext(ctx, goupdater.GithubOpts{
		Owner: owner,
		Repo:  repo,
	})
	if err != nil {
		panic(err)
	}

	// Updates the running binary to the latest available version
	updated, err := goupdater.UpdateWithContext(ctx, resolver, currentVersion)
	if err != nil {
		panic(err)
	}

	if updated {
		fmt.Print("You are now using the latest version!")
	} else {
		fmt.Print("You already have the latest version!")
	}
}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrMissingGithubToken is used when a github token is not provided
	ErrMissingGithubToken = errors.New("to check for updates you must provide a github token")
)

Functions

func Update

func Update(resolver Resolver, currentVersion string) (bool, error)

Update updates the current binary with the chosen resolver

func UpdateWithContext

func UpdateWithContext(ctx context.Context, resolver Resolver, currentVersion string) (bool, error)

UpdateWithContext updates the current binary with the chosen resolver and accepts a context

Types

type Github

type Github struct {
	// contains filtered or unexported fields
}

Github represents a github releases resolver

func NewGithub

func NewGithub(opts ...Option) (*Github, error)

NewGithub creates a new instance of Github

func NewGithubWithContext

func NewGithubWithContext(ctx context.Context, opts ...Option) (*Github, error)

NewGithubWithContext creates a new instance of Github and accepts a context param

func (*Github) Update

func (p *Github) Update(ctx context.Context, currentVersion string) (io.ReadCloser, error)

Update checks and returns a new binary on github releases

type Option added in v0.1.1

type Option func(*Github)

Option represents the client options

func WithOwner added in v0.1.1

func WithOwner(owner string) Option

WithOwner sets the github owner

func WithRepo added in v0.1.1

func WithRepo(repo string) Option

WithRepo sets the github repo

func WithToken added in v0.1.1

func WithToken(token string) Option

WithToken sets the github token

type Resolver

type Resolver interface {
	Update(ctx context.Context, currentVersion string) (io.ReadCloser, error)
}

Resolver represents an upstream that will be called when checking for a new version

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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