1
0
mirror of https://github.com/taigrr/go-selfupdate synced 2025-01-18 04:33:12 -08:00
This commit is contained in:
Thomas Frössman 2015-01-07 18:31:36 +01:00
parent 19468084c4
commit 1f1bb201ba
2 changed files with 61 additions and 41 deletions

View File

@ -14,7 +14,6 @@ import (
"runtime" "runtime"
"github.com/kr/binarydist" "github.com/kr/binarydist"
//"encoding/base64"
) )
var version, genDir string var version, genDir string

View File

@ -1,37 +1,4 @@
package selfupdate // Update protocol:
import (
"bitbucket.org/kardianos/osext"
"bytes"
"compress/gzip"
"crypto/sha256"
"encoding/json"
"errors"
"fmt"
"github.com/inconshreveable/go-update"
"github.com/kr/binarydist"
"io"
"io/ioutil"
"log"
"math/rand"
"net/http"
"os"
"path/filepath"
"runtime"
"time"
)
const (
upcktimePath = "cktime"
plat = runtime.GOOS + "-" + runtime.GOARCH
)
const devValidTime = 7 * 24 * time.Hour
var ErrHashMismatch = errors.New("new file hash mismatch after patch")
var up = update.New()
// Update protocol.
// //
// GET hk.heroku.com/hk/linux-amd64.json // GET hk.heroku.com/hk/linux-amd64.json
// //
@ -54,13 +21,66 @@ var up = update.New()
// //
// 200 ok // 200 ok
// [gzipped executable data] // [gzipped executable data]
//
//
package selfupdate
import (
"bytes"
"compress/gzip"
"crypto/sha256"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"math/rand"
"net/http"
"os"
"path/filepath"
"runtime"
"time"
"bitbucket.org/kardianos/osext"
"github.com/inconshreveable/go-update"
"github.com/kr/binarydist"
)
const (
upcktimePath = "cktime"
plat = runtime.GOOS + "-" + runtime.GOARCH
)
const devValidTime = 7 * 24 * time.Hour
var ErrHashMismatch = errors.New("new file hash mismatch after patch")
var up = update.New()
// Updater is the configuration and runtime data for doing an update.
//
// Note that ApiURL, BinURL and DiffURL should have the same value if all files are available at the same location.
//
// Example:
//
// updater := &selfupdate.Updater{
// CurrentVersion: version,
// ApiURL: "http://updates.yourdomain.com/",
// BinURL: "http://updates.yourdownmain.com/",
// DiffURL: "http://updates.yourdomain.com/",
// Dir: "update/",
// CmdName: "myapp", // app name
// }
// if updater != nil {
// go updater.BackgroundRun()
// }
type Updater struct { type Updater struct {
CurrentVersion string CurrentVersion string // Currently running version.
ApiURL string ApiURL string // Base URL for API requests (json files).
CmdName string CmdName string // Command name is appended to the ApiURL like http://apiurl/CmdName/. This represents one binary.
BinURL string BinURL string // Base URL for full binary downloads.
DiffURL string DiffURL string // Base URL for diff downloads.
Dir string Dir string // Directory to store selfupdate state.
Info struct { Info struct {
Version string Version string
Sha256 []byte Sha256 []byte
@ -74,6 +94,7 @@ func (u *Updater) getExecRelativeDir(dir string) string {
return path return path
} }
// BackgroundRun starts the update check and apply cycle.
func (u *Updater) BackgroundRun() { func (u *Updater) BackgroundRun() {
os.MkdirAll(u.getExecRelativeDir(u.Dir), 0777) os.MkdirAll(u.getExecRelativeDir(u.Dir), 0777)
if u.wantUpdate() { if u.wantUpdate() {