Skip to content

Commit c162c86

Browse files
committed
Improve self-update
- don’t run update if already running same version - add some documentation - add error message if patching of application fails
1 parent f65865b commit c162c86

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

command_selfupdate.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
)
1313

1414
type SelfUpdateCommand struct {
15+
CurrentVersion string
1516
GithubOrganization string
1617
GithubRepository string
1718
GithubAssetTemplate string
@@ -29,25 +30,35 @@ func (conf *SelfUpdateCommand) Execute(args []string) error {
2930

3031
fmt.Println(fmt.Sprintf(" - latest version is %s", release.GetName()))
3132

33+
// check if latest version is current version
34+
if release.GetName() == conf.CurrentVersion {
35+
fmt.Println(" - already using the latest version")
36+
return nil
37+
}
38+
39+
// translate OS names
3240
os := runtime.GOOS
3341
switch (runtime.GOOS) {
3442
case "darwin":
3543
os = "osx"
3644
}
3745

46+
// translate arch names
3847
arch := runtime.GOARCH
3948
switch (arch) {
4049
case "amd64":
4150
arch = "x64"
4251
case "386":
4352
arch = "x32"
4453
}
54+
55+
// build asset name
4556
assetName := conf.GithubAssetTemplate
4657
assetName = strings.Replace(assetName, "%OS%", os, -1)
4758
assetName = strings.Replace(assetName, "%ARCH%", arch, -1)
4859

60+
// search assets in release for the desired filename
4961
fmt.Println(fmt.Sprintf(" - searching for asset \"%s\"", assetName))
50-
5162
for _, asset := range release.Assets {
5263
if asset.GetName() == assetName {
5364
downloadUrl := asset.GetBrowserDownloadURL()
@@ -73,6 +84,7 @@ func (conf *SelfUpdateCommand) runUpdate(url string) error {
7384
err = update.Apply(resp.Body, update.Options{})
7485
if err != nil {
7586
// error handling
87+
fmt.Println(fmt.Sprintf(" - updating application failed: %s", err))
7688
}
7789
return err
7890
}

main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func handleArgParser() {
6565
}
6666

6767
argparser.AddCommand("version", "Show version", fmt.Sprintf("Show %s version", Name), &VersionCommand{Name:Name, Version:Version, Author:Author})
68-
argparser.AddCommand("self-update", "Self update", "Run self update of this application", &SelfUpdateCommand{GithubOrganization:GithubOrganization, GithubRepository:GithubRepository, GithubAssetTemplate:GithubAssetTemplate})
68+
argparser.AddCommand("self-update", "Self update", "Run self update of this application", &SelfUpdateCommand{GithubOrganization:GithubOrganization, GithubRepository:GithubRepository, GithubAssetTemplate:GithubAssetTemplate, CurrentVersion:Version})
6969

7070
argparser.AddCommand("list", "List server configurations", "List server configurations", &ListCommand{})
7171
argparser.AddCommand("sync", "Sync from server", "Sync filesystem and databases from server", &SyncCommand{})

0 commit comments

Comments
 (0)