Skip to content

Commit

Permalink
Auto updates 馃殌
Browse files Browse the repository at this point in the history
  • Loading branch information
SavageCore committed Apr 11, 2023
1 parent 2f33086 commit 080ad8e
Show file tree
Hide file tree
Showing 7 changed files with 244 additions and 5 deletions.
4 changes: 3 additions & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ tasks:
- lint
- assets
cmds:
- go build -ldflags="-s -w -H=windowsgui -extldflags '-static -static-libgcc'" -buildmode=exe -o bin/coh3-replay-manager.exe
- go build -ldflags="-s -w -H=windowsgui -extldflags '-static -static-libgcc'" -buildmode=exe -o bin/coh3-replay-manager-go.exe
env:
CGO_ENABLED: 1
CC: C:\msys64\mingw64\bin\gcc.exe
Expand All @@ -17,6 +17,8 @@ tasks:
cmds:
- air -c .air.toml
silent: false
env:
DEV_MODE: true
release:
deps:
- lint
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require github.com/go-ole/go-ole v1.2.6 // indirect

require (
github.com/StackExchange/wmi v1.2.1
github.com/fynelabs/selfupdate v0.2.0
github.com/getlantern/context v0.0.0-20190109183933-c447772a6520 // indirect
github.com/getlantern/errors v0.0.0-20190325191628-abdb3e3e36f7 // indirect
github.com/getlantern/golog v0.0.0-20190830074920-4ef2e798c2d7 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
github.com/fynelabs/selfupdate v0.2.0 h1:IDqwgV7BYj4lCcoD8hHvIapVGmS5ifWrc0sQTWh1eFw=
github.com/fynelabs/selfupdate v0.2.0/go.mod h1:rCdliRnLw+koUanA+lrqub9wWlNc2wPDTsyRC6A+vfc=
github.com/gen2brain/beeep v0.0.0-20230307103607-6e717729cb4f h1:oRm7Hy2dQWfHgOuOWRaYZf+kZcWJst7fxAlq+yjdLss=
github.com/gen2brain/beeep v0.0.0-20230307103607-6e717729cb4f/go.mod h1:0W7dI87PvXJ1Sjs0QPvWXKcQmNERY77e8l7GFhZB/s4=
github.com/getlantern/context v0.0.0-20190109183933-c447772a6520 h1:NRUJuo3v3WGC/g5YiyF790gut6oQr5f3FBI88Wv0dx4=
Expand Down
115 changes: 111 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,35 @@ import (
"coh3-replay-manager-go/modules/replay"
"coh3-replay-manager-go/modules/utils"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"time"

"github.com/Teages/go-autostart"
"github.com/fynelabs/selfupdate"
"github.com/gen2brain/beeep"
"github.com/getlantern/systray"
)

const CurrentVersion = "v0.1.3"
const CurrentVersion = "v0.1.2"

func main() {
if os.Getenv("DEV_MODE") != "true" {
autoUpdate()

// Run auto update every 24 hours
go func() {
for {
time.Sleep(24 * time.Hour)
autoUpdate()
}
}()
}

// Check if the app was opened with a command line argument
if len(os.Args) > 1 && strings.HasPrefix(os.Args[1], "coh3-replay-manager-go://") {
// Get the string containing the replay id and game version from the command line argument (without the protocol)
Expand Down Expand Up @@ -51,7 +68,7 @@ func onReady() {

mSetStartup := systray.AddMenuItem("Launch on startup", "Start this app when your computer starts")
systray.AddSeparator()
mAbout := systray.AddMenuItem("About", "")
mAbout := systray.AddMenuItem(fmt.Sprintf("About (%s)", CurrentVersion), "")
mQuit := systray.AddMenuItem("Exit", "")

if app.IsEnabled() {
Expand Down Expand Up @@ -113,7 +130,7 @@ func parseUrlInput(input string) {
message := fmt.Sprintf("鈿狅笍 Game version: %s does not equal Replay's version: %s", gameVersion, replayGameVersion)
err := beeep.Notify(title, message, "")
if err != nil {
panic(err)
fmt.Println(err)
}

return
Expand All @@ -135,7 +152,97 @@ func getIconData(file string) []byte {

iconData, err := Asset("assets/icons/" + file)
if err != nil {
panic(err)
fmt.Println(err)
}
return iconData
}

func autoUpdate() bool {
release, err := utils.GetLatestRelease()
if err != nil {
fmt.Println(err)
}

if release.TagName != CurrentVersion {
// New version available
title := "New version available"
message := fmt.Sprintf("Version %s is available. Downloading and restarting the app.", release.TagName)
err := beeep.Notify(title, message, "")
if err != nil {
fmt.Println(err)
}

for _, asset := range release.Assets {
if asset.Name == "coh3-replay-manager-go_Windows_x86_64.zip" {
// Set file path to tmp folder
downloadPath := filepath.Join(os.TempDir(), asset.Name)

utils.DownloadFile(asset.DownloadURL, downloadPath)
fmt.Println("Downloaded file to", downloadPath)

// Unzip the file
utils.ExtractZip(downloadPath, os.TempDir())

// Delete the zip file
err := os.Remove(downloadPath)
if err != nil {
fmt.Println("Failed to delete file:", err)
return false
}

extractedFilePath := filepath.Join(os.TempDir(), "coh3-replay-manager-go.exe")

// Read the extracted file coh3-replay-manager-go.exe
file, err := os.Open(extractedFilePath)
if err != nil {
fmt.Println("Failed to open file:", err)
return false
}
defer file.Close()

reader := io.Reader(file)

// Update the app
err = selfupdate.Apply(reader, selfupdate.Options{})
if err != nil {
// error handling
fmt.Println("Failed to update:", err)
}

err = file.Close()
if err != nil {
fmt.Println("Failed to close file:", err)
return false
}

// Get the path to the current executable
exePath, err := os.Executable()
if err != nil {
fmt.Println("Failed to get executable path:", err)
return false
}

// Delete the extracted file
err = os.Remove(extractedFilePath)
if err != nil {
fmt.Println("Failed to delete file:", err)
return false
}

// Restart the app
cmd := exec.Command(exePath)
_, err = cmd.Output()
if err != nil {
fmt.Println("Failed to restart:", err)
return false
}

os.Exit(0)
}
}

return true
} else {
return false
}
}
31 changes: 31 additions & 0 deletions modules/utils/download.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package utils

import (
"io"
"net/http"
"os"
)

func DownloadFile(url string, filepath string) error {
// Create the file
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()

// Get the data
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()

// Write the body to file
_, err = io.Copy(out, resp.Body)
if err != nil {
return err
}

return nil
}
39 changes: 39 additions & 0 deletions modules/utils/latest-release.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package utils

import (
"encoding/json"
"fmt"
"net/http"
)

// Release represents a GitHub release
type Release struct {
TagName string `json:"tag_name"`
Name string `json:"name"`
Assets []struct {
Name string `json:"name"`
DownloadURL string `json:"browser_download_url"`
} `json:"assets"`
}

func GetLatestRelease() (Release, error) {
var release Release
repoOwner := "SavageCore"
repoName := "coh3-replay-manager-go"

url := fmt.Sprintf("https://api.github.com/repos/%s/%s/releases/latest", repoOwner, repoName)
resp, err := http.Get(url)
if err != nil {
fmt.Println("Error:", err)
return release, err
}
defer resp.Body.Close()

err = json.NewDecoder(resp.Body).Decode(&release)
if err != nil {
fmt.Println("Error:", err)
return release, err
}

return release, nil
}
57 changes: 57 additions & 0 deletions modules/utils/zip.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package utils

import (
"archive/zip"
"io"
"os"
"path/filepath"
)

func ExtractZip(zipFile string, destPath string) error {
// Open the ZIP file for reading
r, err := zip.OpenReader(zipFile)
if err != nil {
return err
}
defer r.Close()

// Create the destination directory if it doesn't exist
err = os.MkdirAll(destPath, 0755)
if err != nil {
return err
}

// Extract each file from the ZIP archive
for _, f := range r.File {
// Open the file in the ZIP archive
rc, err := f.Open()
if err != nil {
return err
}
defer rc.Close()

// Create the destination file
destPath := filepath.Join(destPath, f.Name)
if f.FileInfo().IsDir() {
// If the file entry in the ZIP archive is a directory, create the directory
err = os.MkdirAll(destPath, f.Mode())
if err != nil {
return err
}
} else {
// If the file entry in the ZIP archive is a file, create the file and write its content
destFile, err := os.OpenFile(destPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
return err
}
defer destFile.Close()

_, err = io.Copy(destFile, rc)
if err != nil {
return err
}
}
}

return nil
}

0 comments on commit 080ad8e

Please sign in to comment.