Skip to content
This repository has been archived by the owner on Apr 28, 2023. It is now read-only.

Commit

Permalink
build: fix update command
Browse files Browse the repository at this point in the history
After moving to Goreleaser in 3947282
the artifacts are put into a gzipped tar archive.
  • Loading branch information
Max Jonas Werner committed Apr 5, 2023
1 parent 6ba2e87 commit 7cb6488
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions cmd/update/update.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package update

import (
"archive/tar"
"compress/gzip"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -36,16 +38,17 @@ func updateCommand(dryRun bool, includePreReleases bool, upgradeMajor bool, out
latestVersionString, repo+"/releases/"+latestVersionString)
return nil
}
downloadURL := fmt.Sprintf("%s/releases/download/%s/gitlab_%s_%s_%s",
downloadURL := fmt.Sprintf("%s/releases/download/%s/gitlab_%s_%s_%s.tar.gz",
repo, latestVersionString, latestVersionString, runtime.GOOS, runtime.GOARCH)
fmt.Fprintf(out, "Updating to %s\n", latestVersionString)
resp, err := http.Get(downloadURL) // #nosec G107
if err != nil {
return fmt.Errorf("could not download latest release: %w", err)
return fmt.Errorf("could not download latest release from %s: %w", downloadURL, err)
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("could not download latest release: received HTTP status %d", resp.StatusCode)
return fmt.Errorf("could not download latest release from %s: received HTTP status %d", downloadURL, resp.StatusCode)
}

exec, err := getExecutable()
if err != nil {
return fmt.Errorf("could not get current executable to update: %w", err)
Expand All @@ -59,7 +62,22 @@ func updateCommand(dryRun bool, includePreReleases bool, upgradeMajor bool, out
if err != nil {
return fmt.Errorf("could not open binary for updating: %w", err)
}
_, err = io.Copy(binary, resp.Body)
defer func() {
os.Remove(dest)
}()

gzReader, err := gzip.NewReader(resp.Body)
if err != nil {
return fmt.Errorf("failed creating gzip reader: %w", err)
}
defer gzReader.Close()

tarReader := tar.NewReader(gzReader)
if _, err := tarReader.Next(); err != nil {
return fmt.Errorf("failed advancing in tar archive: %w", err)
}

_, err = io.Copy(binary, tarReader)
binary.Close()
if err != nil {
return fmt.Errorf("could not download new version: %w", err)
Expand Down

0 comments on commit 7cb6488

Please sign in to comment.