Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: app patcher #86

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ tasks:
ldflags: =-X 'main.appVersion=${APP_VERSION}' -X 'main.steamUsername=${STEAM_USERNAME}' -X 'main.steamPassword=${STEAM_PASSWORD}' -X 'main.capIDEmail=${CAP_ID_EMAIL}' -X 'main.capIDPassword=${CAP_ID_PASSWORD}' -X 'main.runHeadless="true"'
cmds:
- cmd: env GOOS=darwin GOARCH=amd64 wails build -ldflags="{{.ldflags}}"
platforms: [darwin]
platforms: [darwin/amd64]
- cmd: env GOOS=darwin GOARCH=arm64 wails build -ldflags="{{.ldflags}}"
platforms: [darwin/arm64]
- cmd: env GOOS=windows GOARCH=amd64 wails build -ldflags="{{.ldflags}}"
platforms: [windows]
27 changes: 0 additions & 27 deletions core/browser/browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/go-rod/rod/lib/launcher"
"github.com/go-rod/rod/lib/launcher/flags"
"github.com/go-rod/rod/lib/proto"
"github.com/hashicorp/go-version"
)

type Browser struct {
Expand Down Expand Up @@ -74,29 +73,3 @@ func NewBrowser(headless bool) (*Browser, error) {
HijackRouter: router,
}, nil
}

func (b *Browser) GetLatestAppVersion() (*version.Version, error) {
log.Println(`Check for new version`)
err := b.Page.Navigate(`https://github.com/williamsjokvist/cfn-tracker/releases`)
if err != nil {
return nil, fmt.Errorf(`navigate to github: %w`, err)
}
err = b.Page.WaitLoad()
if err != nil {
return nil, fmt.Errorf(`wait for github to load: %w`, err)
}
versionElement, err := b.Page.Element(`turbo-frame div.mr-md-0:nth-child(3) > a:nth-child(1)`)
if err != nil {
return nil, fmt.Errorf(`get version element: %w`, err)
}
versionText, err := versionElement.Text()
if err != nil {
return nil, fmt.Errorf(`get version element text: %w`, err)
}
versionNumber := strings.Split(versionText, `v`)[1]
latestVersion, err := version.NewVersion(versionNumber)
if err != nil {
return nil, fmt.Errorf(`parse version: %w`, err)
}
return latestVersion, nil
}
15 changes: 1 addition & 14 deletions core/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ import (
"os/exec"
"runtime"
"strings"
"time"

"github.com/hashicorp/go-version"

"github.com/williamsjokvist/cfn-tracker/core/browser"
"github.com/williamsjokvist/cfn-tracker/core/data"
Expand All @@ -21,16 +18,6 @@ import (
"github.com/williamsjokvist/cfn-tracker/core/tracker"
)

var (
SteamUsername string
SteamPassword string
CapIDEmail string
CapIDPassword string
AppVersion *version.Version
RefreshInterval time.Duration = 30 * time.Second
RunHeadless bool
)

// The CommandHandler is the interface between the GUI and the core
type CommandHandler struct {
ctx context.Context
Expand All @@ -52,7 +39,7 @@ func (ch *CommandHandler) AssignRuntimeContext(ctx context.Context) {
}

func (ch *CommandHandler) GetAppVersion() string {
return AppVersion.Original()
return AppVersion
}

func (ch *CommandHandler) StopTracking() {
Expand Down
13 changes: 13 additions & 0 deletions core/consts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package core

import "time"

var (
SteamUsername string
SteamPassword string
CapIDEmail string
CapIDPassword string
AppVersion string
RefreshInterval time.Duration = 30 * time.Second
RunHeadless bool
)
31 changes: 31 additions & 0 deletions core/update/check.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package update

import (
"fmt"
"log"

"github.com/blang/semver"
"github.com/rhysd/go-github-selfupdate/selfupdate"
)

func CheckForUpdate(appVersion string) (*selfupdate.Release, error) {
selfupdate.EnableLog()
latest, found, err := selfupdate.DetectLatest(`GoogleCloudPlatform/terraformer`)

if err != nil {
return nil, fmt.Errorf(`get latest app version: %w`, err)
}

v, err := semver.Parse(appVersion)
if err != nil {
return nil, fmt.Errorf(`parse app version: %w`, err)
}

if !found || latest.Version.LTE(v) {
log.Println("Current version is the latest")
return nil, nil
}

log.Println("New app version available: ", latest.Version.String())
return latest, nil
}
96 changes: 96 additions & 0 deletions core/update/update_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
//go:build darwin

package update

import (
"bufio"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"

"github.com/rhysd/go-github-selfupdate/selfupdate"
)

type ProgressStatus struct {
Progress int
Err error
}

func (s *ProgressStatus) WithProgress(progress int) *ProgressStatus {
s.Progress = progress
return s
}

func (s *ProgressStatus) WithError(err error) *ProgressStatus {
s.Err = err
return s
}

func DoUpdate(to *selfupdate.Release, progChan chan ProgressStatus) {
log.Println("Started updating to", to.Version)
status := &ProgressStatus{Progress: 0, Err: nil}

homeDir, err := os.UserHomeDir()
if err != nil {
progChan <- *status.WithError(fmt.Errorf(`get user home dir: %w`, err))
return
}
downloadPath := filepath.Join(homeDir, "Downloads", `CFN_Tracker.zip`)
cmd := exec.Command("curl", "-#", "-L", to.AssetURL, "-o", downloadPath)
fmt.Println(cmd.String())
// curl outputs download progress to stderr
stderr, _ := cmd.StderrPipe()
defer stderr.Close()

if err := cmd.Start(); err != nil {
progChan <- *status.WithError(fmt.Errorf("failed to download new version: %w", err))
return
}

scanner := bufio.NewScanner(stderr)
scanner.Split(bufio.ScanWords)
for scanner.Scan() {
m := scanner.Text()
// only parse percentage numbers, not the bar
if !strings.Contains(m, "#") {
progress, err := strconv.Atoi(strings.Split(m, ".")[0])
if err != nil {
progChan <- *status.WithError(fmt.Errorf(`failed to parse progress: %w`, err))
}
fmt.Println(progress)
progChan <- *status.WithProgress(progress)
}
}

if err := cmd.Wait(); err != nil {
progChan <- *status.WithError(fmt.Errorf(`failed to download new version: %w`, err))
return
}

var appPath string
// cmdPath, err := os.Executable()
// if err != nil {
// appPath = filepath.Join(homeDir, `Applications`)
// } else {
// appPath = strings.TrimSuffix(cmdPath, filepath.Join(`CFN Tracker.app`, `Contents`, `MacOS`, `CFN Tracker`))
// }
appPath = filepath.Join(homeDir, `Applications`)

err = exec.Command("ditto", "-xk", downloadPath, appPath).Run()
if err != nil {
progChan <- *status.WithError(fmt.Errorf(`failed to extract archive: %w`, err))
return
}
err = exec.Command("rm", downloadPath).Run()
if err != nil {
progChan <- *status.WithError(fmt.Errorf(`failed to remove archive: %w`, err))
return
}

log.Println(`Update successfully done to version`, to.Version)
log.Println(`Release note:\n`, to.ReleaseNotes)
}
27 changes: 27 additions & 0 deletions core/update/update_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//go:build windows

package update

import (
"fmt"
"log"

"github.com/blang/semver"
"github.com/rhysd/go-github-selfupdate/selfupdate"
)

func DoUpdate(to semver.Version) (bool, error) {
latest, err := selfupdate.UpdateSelf(to, `GoogleCloudPlatform/terraformer`)
if err != nil {
return false, fmt.Errorf(`failed to update to %s: %w`, latest.Version, err)
}

if to.Equals(latest.Version) {
log.Println("Current binary is the latest version")
return false, nil
}

log.Println("Update successfully done to version", latest.Version)
log.Println("Release note:\n", latest.ReleaseNotes)
return true, nil
}
11 changes: 10 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ go 1.21
require github.com/go-rod/rod v0.112.2

require (
github.com/blang/semver v3.5.1+incompatible
github.com/jmoiron/sqlx v1.3.5
github.com/joho/godotenv v1.4.0
github.com/mattn/go-sqlite3 v1.14.17
github.com/rhysd/go-github-selfupdate v1.2.3
github.com/shirou/gopsutil v3.21.11+incompatible
github.com/wailsapp/wails/v2 v2.7.0
golang.org/x/sys v0.13.0
Expand Down Expand Up @@ -39,23 +41,30 @@ require (
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/godbus/dbus/v5 v5.1.0 // indirect
github.com/golang/protobuf v1.3.2 // indirect
github.com/google/go-github/v30 v30.1.0 // indirect
github.com/google/go-querystring v1.0.0 // indirect
github.com/inconshreveable/go-update v0.0.0-20160112193335-8152e7eb6ccf // indirect
github.com/kr/text v0.2.0 // indirect
github.com/leaanthony/u v1.1.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/rogpeppe/go-internal v1.6.1 // indirect
github.com/tcnksm/go-gitconfig v0.1.2 // indirect
github.com/tklauser/go-sysconf v0.3.11 // indirect
github.com/tklauser/numcpus v0.6.0 // indirect
github.com/ulikunitz/xz v0.5.9 // indirect
github.com/wailsapp/go-webview2 v1.0.10 // indirect
github.com/yusufpapurcu/wmi v1.2.3 // indirect
golang.org/x/oauth2 v0.0.0-20181106182150-f42d05182288 // indirect
google.golang.org/appengine v1.3.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

require (
github.com/hashicorp/go-version v1.6.0
github.com/stretchr/testify v1.8.1
github.com/ysmood/goob v0.4.0 // indirect
github.com/ysmood/gson v0.7.3 // indirect
Expand Down
Loading