Skip to content

Commit

Permalink
Refactor self-update code
Browse files Browse the repository at this point in the history
  • Loading branch information
thedevsaddam committed Nov 3, 2021
1 parent 2aeced0 commit 633edb2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 20 deletions.
5 changes: 4 additions & 1 deletion update/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,11 @@ func fetchReleaseInfo(ctx context.Context) (*ReleaseInfo, error) {
return &rf, nil
}

// updateBinary download the binary in /user/local/bin which is the default path and replace the old one
// updateBinary download the binary in current binary and replace the old one
func updateBinary(ctx context.Context, url string) error {
if url == "" {
return errors.New("update: empty download url")
}
ctx, cancel := context.WithTimeout(ctx, 120*time.Second)
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
Expand Down
23 changes: 4 additions & 19 deletions update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,26 +60,11 @@ func SelfUpdate(ctx context.Context, buildDate, version string) error {
s.Prefix = fmt.Sprintf("Updating from %s to %s ( ", version, releaseInfo.Name)
s.Suffix = ")"
s.Start()

switch os {
case "windows":
if arch == "amd64" || arch == "x86_64" {
err = updateBinary(ctx, releaseInfo.getDownloadURL("windows_amd64.exe"))
} else {
err = updateBinary(ctx, releaseInfo.getDownloadURL("windows_386.exe"))
}

case "darwin":
err = updateBinary(ctx, releaseInfo.getDownloadURL("mac_amd64"))

case "linux":
if arch == "amd64" || arch == "x86_64" {
err = updateBinary(ctx, releaseInfo.getDownloadURL("linux_amd64"))
} else {
err = updateBinary(ctx, releaseInfo.getDownloadURL("linux_386")) // i386
}
name := fmt.Sprintf("%s_%s", os, arch)
if os == "windows" {
name = name + ".exe"
}

err = updateBinary(ctx, releaseInfo.getDownloadURL(name))
s.Stop()
fmt.Println()

Expand Down

0 comments on commit 633edb2

Please sign in to comment.