Skip to content

Commit 31e41f0

Browse files
committed
Improve self-update
- don’t run update if already running same version - add some documentation
1 parent fb86add commit 31e41f0

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

command/SelfUpdate.go

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

1414
type SelfUpdate struct {
15+
CurrentVersion string
1516
GithubOrganization string
1617
GithubRepository string
1718
GithubAssetTemplate string
@@ -29,25 +30,35 @@ func (conf *SelfUpdate) 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()

main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func createArgparser() {
7070
}
7171

7272
argparser.AddCommand("version", "Show version", "Show current app version", &command.Version{Name:Name, Version:Version, Author:Author})
73-
argparser.AddCommand("self-update", "Self update", "Run self update of this application", &command.SelfUpdate{GithubOrganization:GithubOrganization, GithubRepository:GithubRepository, GithubAssetTemplate:GithubAssetTemplate})
73+
argparser.AddCommand("self-update", "Self update", "Run self update of this application", &command.SelfUpdate{GithubOrganization:GithubOrganization, GithubRepository:GithubRepository, GithubAssetTemplate:GithubAssetTemplate, CurrentVersion:Version})
7474

7575
argparser.AddCommand("mysql:debug", "MySQL debug", "Show MySQL query log", &command.MysqlDebug{})
7676
argparser.AddCommand("mysql:slowlog", "MySQL slow query log", "Show MySQL slow query log", &command.MysqlSlowLog{})

0 commit comments

Comments
 (0)