Skip to content

Commit

Permalink
fix goreleaser and update mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickhener committed Oct 17, 2024
1 parent 7532d88 commit 59b5cdb
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 17 deletions.
9 changes: 1 addition & 8 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -32,4 +25,4 @@ changelog:
filters:
exclude:
- '^docs:'
- '^test:'
- '^test:'
55 changes: 46 additions & 9 deletions update/update.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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) {
Expand Down Expand Up @@ -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
}

Expand All @@ -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")
}

0 comments on commit 59b5cdb

Please sign in to comment.