Skip to content

Commit

Permalink
Update README.md and selfupdate.go
Browse files Browse the repository at this point in the history
  • Loading branch information
AhMyth committed Jul 31, 2019
1 parent 011b2eb commit 10870d7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 29 deletions.
25 changes: 3 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,11 @@ Enable your Golang applications to self update. Inspired by Chrome based on Her
ApiURL: "http://updates.yourdomain.com/",
BinURL: "http://updates.yourdomain.com/",
DiffURL: "http://updates.yourdomain.com/",
Dir: "update/",
CmdName: "myapp", // app name
LocalStateDir: "update/",
AppName: "myApp",
DirName: "cli", // direcroty name
}

if updater != nil {
go updater.BackgroundRun()
}

### Push Out and Update

go-selfupdate myapp 1.2

This will create a folder in your project called, *public* you can then rsync or transfer this to your webserver or S3.

If you are cross compiling you can specify a directory:

go-selfupdate /tmp/mybinares/ 1.2

The directory should contain files with the name, $GOOS-$ARCH. Example:

windows-386
darwin-amd64
linux-arm

If you are using [goxc](https://github.com/laher/goxc) you can output the files with this naming format by specifying this config:

"OutPath": "{{.Dest}}{{.PS}}{{.Version}}{{.PS}}{{.Os}}-{{.Arch}}",
15 changes: 8 additions & 7 deletions selfupdate/selfupdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,11 @@ var defaultHTTPRequester = HTTPRequester{}
type Updater struct {
CurrentVersion string // Currently running version.
ApiURL string // Base URL for API requests (json files).
CmdName string // Command name is appended to the ApiURL like http://apiurl/CmdName/. This represents one binary.
AppName string // Application name
DirName string // Directory name is appended to the ApiURL like http://apiurl/DirName/. This represents one binary.
BinURL string // Base URL for full binary downloads.
DiffURL string // Base URL for diff downloads.
Dir string // Directory to store selfupdate state.
LocalStateDir string // Directory to store selfupdate state.
ForceCheck bool // Check for update regardless of cktime timestamp
Requester Requester //Optional parameter to override existing http request handler
Info struct {
Expand All @@ -100,7 +101,7 @@ func (u *Updater) getExecRelativeDir(dir string) string {

// BackgroundRun starts the update check and apply cycle.
func (u *Updater) BackgroundRun() error {
if err := os.MkdirAll(u.getExecRelativeDir(u.Dir), 0777); err != nil {
if err := os.MkdirAll(u.getExecRelativeDir(u.LocalStateDir), 0777); err != nil {
// fail
return err
}
Expand All @@ -122,7 +123,7 @@ func (u *Updater) BackgroundRun() error {
}

func (u *Updater) wantUpdate() bool {
path := u.getExecRelativeDir(u.Dir + upcktimePath)
path := u.getExecRelativeDir(u.LocalStateDir + upcktimePath)
if u.CurrentVersion == "dev" || (!u.ForceCheck && readTime(path).After(time.Now())) {
return false
}
Expand Down Expand Up @@ -191,7 +192,7 @@ func (u *Updater) update() (error) {
}

func (u *Updater) fetchInfo() error {
r, err := u.fetch(u.ApiURL + url.QueryEscape(u.CmdName) + "/" + url.QueryEscape(plat) + ".json")
r, err := u.fetch(u.ApiURL + url.QueryEscape(u.DirName) + "/" + url.QueryEscape(plat) + ".json")
if err != nil {
return err
}
Expand All @@ -218,7 +219,7 @@ func (u *Updater) fetchAndVerifyPatch(old io.Reader) ([]byte, error) {
}

func (u *Updater) fetchAndApplyPatch(old io.Reader) ([]byte, error) {
r, err := u.fetch(u.DiffURL + url.QueryEscape(u.CmdName) + "/" + url.QueryEscape(u.CurrentVersion) + "/" + url.QueryEscape(u.Info.Version) + "/" + url.QueryEscape(plat))
r, err := u.fetch(u.DiffURL + url.QueryEscape(u.DirName) + "/" + url.QueryEscape(u.CurrentVersion) + "/" + url.QueryEscape(u.Info.Version) + "/" + url.QueryEscape(plat))
if err != nil {
return nil, err
}
Expand All @@ -241,7 +242,7 @@ func (u *Updater) fetchAndVerifyFullBin() ([]byte, error) {
}

func (u *Updater) fetchBin() ([]byte, error) {
r, err := u.fetch(u.BinURL + url.QueryEscape(u.CmdName) + "/" + url.QueryEscape(plat) + ".zip")
r, err := u.fetch(u.BinURL + url.QueryEscape(u.DirName) + "/" + url.QueryEscape(u.AppName)+"-"+url.QueryEscape(plat) + ".zip")
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 10870d7

Please sign in to comment.