diff --git a/.goreleaser.yml b/.goreleaser.yml index 290c083..50991b8 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -6,23 +6,16 @@ before: builds: - env: - CGO_ENABLED=0 - binary: goshs goos: - linux - windows - darwin - goarch: - - "amd64" - - "386" - - "arm64" - archives: - name_template: >- {{ .ProjectName }}_ {{- .Os }}_ {{- if eq .Arch "amd64" }}x86_64{{ else }}{{ .Arch }}{{ end }} {{- if .Arm }}v{{ .Arm }}{{ end }} - checksum: name_template: 'checksums.txt' snapshot: @@ -32,4 +25,4 @@ changelog: filters: exclude: - '^docs:' - - '^test:' + - '^test:' \ No newline at end of file diff --git a/update/update.go b/update/update.go index 198a6ac..16e6c3c 100644 --- a/update/update.go +++ b/update/update.go @@ -1,10 +1,14 @@ package update import ( + "archive/tar" + "compress/gzip" "context" "fmt" + "io" "net/http" "runtime" + "strings" "time" "github.com/google/go-github/v42/github" @@ -15,14 +19,14 @@ import ( const ( owner = "patrickhener" repo = "goshs" - GOSHS_WINDOWS_64 = "goshs_windows_x86_64.exe" - GOSHS_WINDOWS_32 = "goshs_windows_386.exe" - GOSHS_WINDOWS_ARM = "goshs_windows_arm64.exe" - GOSHS_LINUX_64 = "goshs_linux_x86_64" - GOSHS_LINUX_32 = "goshs_linux_386" - GOSHS_LINUX_ARM = "goshs_linux_arm64" - GOSHS_DARWIN_64 = "goshs_darwin_x86_64" - GOSHS_DARWIN_ARM = "goshs_darwin_arm64" + GOSHS_WINDOWS_64 = "goshs_windows_x86_64.tar.gz" + GOSHS_WINDOWS_32 = "goshs_windows_386.tar.gz" + GOSHS_WINDOWS_ARM = "goshs_windows_arm64.tar.gz" + GOSHS_LINUX_64 = "goshs_linux_x86_64.tar.gz" + GOSHS_LINUX_32 = "goshs_linux_386.tar.gz" + GOSHS_LINUX_ARM = "goshs_linux_arm64.tar.gz" + GOSHS_DARWIN_64 = "goshs_darwin_x86_64.tar.gz" + GOSHS_DARWIN_ARM = "goshs_darwin_arm64.tar.gz" ) func CheckForUpdates(version string) (bool, string) { @@ -94,10 +98,16 @@ func applyUpdate(assetURL string) error { } defer resp.Body.Close() - err = update.Apply(resp.Body, update.Options{}) + goshsReader, err := tarXVZF(resp) + if err != nil { + return fmt.Errorf("failed to decompress the downloaded file: %+v", err) + } + + err = update.Apply(goshsReader, update.Options{}) if err != nil { return fmt.Errorf("failed to apply update: %+v", err) } + return nil } @@ -111,3 +121,30 @@ func assetMatchesOSAndArch(assetName string) bool { (runtime.GOOS == "darwin" && runtime.GOARCH == "amd64" && assetName == GOSHS_DARWIN_64) || (runtime.GOOS == "darwin" && runtime.GOARCH == "arm64" && assetName == GOSHS_DARWIN_ARM) } + +func tarXVZF(response *http.Response) (io.Reader, error) { + gzipReader, err := gzip.NewReader(response.Body) + if err != nil { + return nil, err + } + defer gzipReader.Close() + + tarReader := tar.NewReader(gzipReader) + + for { + header, err := tarReader.Next() + if err == io.EOF { + break + } + + if err != nil { + return nil, err + } + + if strings.Contains(header.Name, "goshs") { + return io.LimitReader(tarReader, header.Size), nil + } + } + + return nil, fmt.Errorf("%s", "end of function - should not happen") +}