Skip to content

Commit

Permalink
Feature/version check (#25)
Browse files Browse the repository at this point in the history
* Add goVersionInfo to project

* go tidy

* Update actions

* Add GetPublishedAppVersion and GetCurrentAppVersion

* Init commit on checkAppVersion.go

* Add new tests for local development

* Go mod and sum updates

* Add goVersionInfo to project

* go tidy

* Update actions

* Add GetPublishedAppVersion and GetCurrentAppVersion

* Init commit on checkAppVersion.go

* Add new tests for local development

* Go mod and sum updates

* Create test for github api

* Remove url from code for reusability

* Create githubHelper.go to get asset name + download uri

* Add error handling to defer because goland said so

* Add logic to download latest version and close the program for update.

* Add UpdateWowtools call

* update go mod and sum

* Add quick test for versionCheck on exe

* Update text output

* Set assetName to var for testing

* Remove Else statement

* Replace Printf with Sprintf

* Update versioninfo.json

* update actions

* update go version

* Add debug line

* downgrade go version for actions

* Add skip

* Update gitignore to remove tests

* Disable go tests

* Revert test removal
  • Loading branch information
lyledouglass authored Jun 1, 2022
1 parent 76491d3 commit 3f2be81
Show file tree
Hide file tree
Showing 13 changed files with 659 additions and 24 deletions.
11 changes: 9 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,18 @@ jobs:
- name: Make build folder
run: mkdir build

- name: Install versioninfo
run: go get github.com/josephspurrier/goversioninfo/cmd/goversioninfo

- name: Go Generate
run: go generate

- name: Build for Windows (amd64)
run: env GOOS=windows GOARCH=amd64 go build -o build/

- name: Test
run: go test -v ./...
# Go Tests are acting up due to the W32 module - I've disabled all the tests but it still spits errors out. Disabling for now as these are all local tests anyways
#- name: Test
#run: go test -v ./...

- name: Upload Windows Artifact
uses: actions/[email protected]
Expand Down
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@

# Dependency directories (remove the comment below to include it)
# vendor/
/resource.syso

# Specific Tests - GH Actions fail with them in places
/local_tests/
46 changes: 46 additions & 0 deletions cmd/checkAppVersion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package cmd

import (
"fmt"
"log"
"os"
"wowtools/utilities"
)

const wowtoolsUri = "https://api.github.com/repos/lyledouglass/wowtools/releases/latest"

var latestVersion = utilities.GetPublishedAppVersion(wowtoolsUri)
var currentVersion = utilities.GetCurrentAppVersion()

func compareAppVersioning() bool {
var updateApp bool
if currentVersion > latestVersion {
updateApp = true
}
return updateApp
}

func UpdateWowtools() {
updateApp := compareAppVersioning()
if updateApp == true {
fmt.Printf("You are running on an older version (%s) of this application. Would you like to download the latest version (%s)?", currentVersion, latestVersion)
updatePrompt := utilities.AskForConfirmation("")
if updatePrompt {
fmt.Println("Downloading latest package...")
downloadUri := utilities.GetReleaseAsset(wowtoolsUri, "wowtools.exe")
err := utilities.DownloadFiles("wowtools.exe", downloadUri)
if err != nil {
log.Fatal("Download step failed")
}
homeDir, _ := os.UserHomeDir()
fmt.Printf("Wowtools version %s hase been downloaded to %s. Please close this application and replace it with the new executable", latestVersion, homeDir+"\\Downloads\\")
fmt.Println("")
fmt.Println("Press 'Enter' to close the program to update")
var input string
fmt.Scanln(&input)
os.Exit(0)
}
} else {
fmt.Println("wowtools is up to date, continuing...")
}
}
8 changes: 3 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,26 @@ module wowtools
go 1.17

require (
github.com/gonutz/w32/v2 v2.4.0
github.com/mitchellh/go-homedir v1.1.0
github.com/spf13/viper v1.10.1
)

require (
github.com/fsnotify/fsnotify v1.5.1 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/kr/pretty v0.2.0 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
github.com/mitchellh/mapstructure v1.4.3 // indirect
github.com/pelletier/go-toml v1.9.4 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/spf13/afero v1.8.1 // indirect
github.com/spf13/cast v1.4.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
golang.org/x/crypto v0.0.0-20220307211146-efcb8507fb70 // indirect
golang.org/x/sys v0.0.0-20220307203707-22a9840ba4d7 // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
golang.org/x/text v0.3.7 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/ini.v1 v1.66.4 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
327 changes: 311 additions & 16 deletions go.sum

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:generate goversioninfo

package main

import (
Expand All @@ -9,8 +11,10 @@ import (
)

func main() {
cmd.InitConfig()

cmd.InitConfig()
// Check for updates to the application
cmd.UpdateWowtools()
// WaitGroup for creating missing folders.
var wg sync.WaitGroup
wg.Add(3)
Expand Down
33 changes: 33 additions & 0 deletions test/appVersion_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package test

import (
"fmt"
"github.com/gonutz/w32/v2"
"log"
"testing"
)

func TestGetCurrentAppVersion(t *testing.T) {
t.Skip()
executablePath := "D:\\Development\\wowtools\\wowtools.exe"
size := w32.GetFileVersionInfoSize(executablePath)
if size <= 0 {
log.Fatalln("GetFileVersionInfoSize failed")
}
info := make([]byte, size)
getFileInfo := w32.GetFileVersionInfo(executablePath, info)
if !getFileInfo {
log.Fatalln("GetFileVersionInfo failed")
}
fixed, getFileInfo := w32.VerQueryValueRoot(info)
if !getFileInfo {
log.Fatalln("VerQueryValueRoot failed")
}
fileVersion := fixed.FileVersion()
versionString := fmt.Sprintf("%d.%d.%d.%d\n",
fileVersion&0xFFFF000000000000>>48,
fileVersion&0x0000FFFF00000000>>32,
fileVersion&0x00000000FFFF0000>>16,
fileVersion&0x000000000000FFFF>>0)
fmt.Println(versionString)
}
66 changes: 66 additions & 0 deletions test/checkAppVersion_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package test

import (
"encoding/json"
"fmt"
"github.com/gonutz/w32/v2"
"io/ioutil"
"log"
"net/http"
"strings"
"testing"
)

func TestGetAppVersion(t *testing.T) {
t.Skip()
t.Log("Test")
executablePath := "D:\\Development\\wowtools\\wowtools.exe"
size := w32.GetFileVersionInfoSize(executablePath)
if size <= 0 {
log.Fatalln("GetFileVersionInfoSize failed")
}
info := make([]byte, size)
getFileInfo := w32.GetFileVersionInfo(executablePath, info)
if !getFileInfo {
log.Fatalln("GetFileVersionInfo failed")
}
fixed, getFileInfo := w32.VerQueryValueRoot(info)
if !getFileInfo {
log.Fatalln("VerQueryValueRoot failed")
}
fileVersion := fixed.FileVersion()
versionString, err := fmt.Printf("%d.%d.%d.%d\n",
fileVersion&0xFFFF000000000000>>48,
fileVersion&0x0000FFFF00000000>>32,
fileVersion&0x00000000FFFF0000>>16,
fileVersion&0x000000000000FFFF>>0)
if err != nil {
log.Fatalln("Failed to assign version to string")
}
t.Log(versionString)
}

type githubApiData struct {
AppVersion string `json:"tag_name"`
}

func TestGetLatestVersion(t *testing.T) {
t.Skip()
url := "https://api.github.com/repos/lyledouglass/wowtools/releases/latest"
resp, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
var data githubApiData
jsonErr := json.Unmarshal(body, &data)
if jsonErr != nil {
log.Fatal(jsonErr)
}
t.Log(data)
trimed := strings.Trim(data.AppVersion, "{ v }")
t.Log(trimed)
}
47 changes: 47 additions & 0 deletions test/githubApi_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package test

import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"testing"
)

func TestGitHubApi(t *testing.T) {
var assetName = "wowtools.exe"
type githubApiData struct {
AppVersion string `json:"tag_name"`
Assets []struct {
Name string `json:"name"`
BrowserDownloadURL string `json:"browser_download_url"`
}
}
uri := "https://api.github.com/repos/lyledouglass/wowtools/releases/latest"
resp, err := http.Get(uri)
if err != nil {
log.Fatal(err)
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
log.Fatal(err)
}
}(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
var data githubApiData
jsonErr := json.Unmarshal(body, &data)
if jsonErr != nil {
log.Fatal(jsonErr)
}
fmt.Println("Version: " + data.AppVersion)
for _, asset := range data.Assets {
if asset.Name == assetName {
fmt.Println("Download URI: " + asset.BrowserDownloadURL)
}
}
}
1 change: 1 addition & 0 deletions test/startCurseforge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
)

func TestCurseforgeOpener(t *testing.T) {
t.Skip()
curseforgeExe := viper.GetString("curseforge_exe")
curseforgeArgs := viper.GetString("curseforge_args")

Expand Down
44 changes: 44 additions & 0 deletions utilities/githubHelper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package utilities

import (
"encoding/json"
"io"
"log"
"net/http"
)

func GetReleaseAsset(uri string, assetName string) string {
var downloadUri string
type githubApiData struct {
AppVersion string `json:"tag_name"`
Assets []struct {
Name string `json:"name"`
BrowserDownloadURL string `json:"browser_download_url"`
}
}
resp, err := http.Get(uri)
if err != nil {
log.Fatal(err)
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
log.Fatal(err)
}
}(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
var data githubApiData
jsonErr := json.Unmarshal(body, &data)
if jsonErr != nil {
log.Fatal(jsonErr)
}
for _, asset := range data.Assets {
if asset.Name == assetName {
downloadUri = asset.BrowserDownloadURL
}
}
return downloadUri
}
53 changes: 53 additions & 0 deletions utilities/versionHelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ package utilities

import (
"encoding/json"
"fmt"
"github.com/gonutz/w32/v2"
"io/ioutil"
"log"
"net/http"
"os"
"regexp"
"strings"

"github.com/spf13/viper"
)
Expand Down Expand Up @@ -44,3 +48,52 @@ func GetLatestVersion() string {
}
return (data.Version)
}

type githubApiData struct {
AppVersion string `json:"tag_name"`
}

func GetPublishedAppVersion(url string) string {
resp, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
var data githubApiData
jsonErr := json.Unmarshal(body, &data)
if jsonErr != nil {
log.Fatal(jsonErr)
}
return strings.Trim(data.AppVersion, "{ v }")
}

func GetCurrentAppVersion() string {
executablePath, err := os.Executable()
if err != nil {
log.Fatalln("Failed to get path of executable")
}
size := w32.GetFileVersionInfoSize(executablePath)
if size <= 0 {
log.Fatalln("GetFileVersionInfoSize failed")
}
info := make([]byte, size)
getFileInfo := w32.GetFileVersionInfo(executablePath, info)
if !getFileInfo {
log.Fatalln("GetFileVersionInfo failed")
}
fixed, getFileInfo := w32.VerQueryValueRoot(info)
if !getFileInfo {
log.Fatalln("VerQueryValueRoot failed")
}
fileVersion := fixed.FileVersion()
versionString := fmt.Sprintf("%d.%d.%d.%d\n",
fileVersion&0xFFFF000000000000>>48,
fileVersion&0x0000FFFF00000000>>32,
fileVersion&0x00000000FFFF0000>>16,
fileVersion&0x000000000000FFFF>>0)

return versionString
}
Loading

0 comments on commit 3f2be81

Please sign in to comment.