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/refactor cobra logrus app update #72

Merged
merged 5 commits into from
Jan 1, 2024
Merged
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
20 changes: 3 additions & 17 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,7 @@ jobs:

- name: Build for Windows (amd64)
run: |
cd cmd/cli && env GOOS=windows GOARCH=amd64 go build -o /home/runner/work/wowtools/wowtools/build/wowtools_cli.exe
cd ../watcher && env GOOS=windows GOARCH=amd64 go build -o /home/runner/work/wowtools/wowtools/build/wowtools_watcher.exe

- name: Build for Linux (amd64)
run: cd cmd/watcher && env GOOS=linux GOARCH=amd64 go build -o /home/runner/work/wowtools/wowtools/build/wowtools_watcher
env GOOS=windows GOARCH=amd64 go build -o /home/runner/work/wowtools/wowtools/build/wowtools.exe

# 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
Expand All @@ -43,12 +39,7 @@ jobs:
with:
name: WoWTools-AMD64
path: |
build/wowtools_cli.exe
build/wowtools_watcher.exe
build/wowtools_watcher
LICENSE
README.md
config.yaml
build/wowtools.exe

release:
runs-on: ubuntu-latest
Expand All @@ -66,9 +57,4 @@ jobs:
uses: softprops/action-gh-release@v1
with:
files: |
LICENSE
README.md
build/wowtools_cli.exe
build/wowtools_watcher.exe
build/wowtools_watcher
config.yaml
build/wowtools.exe
7 changes: 3 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Binaries for programs and plugins
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
Expand All @@ -18,9 +18,8 @@
# Specific Tests - GH Actions fail with them in places
/local_tests/

# Configs in cmd folder
/cmd/cli/config.yaml
/cmd/watcher/config.yaml
# Configs
config.yaml

# Linux binary
wowtools_server
Expand Down
75 changes: 0 additions & 75 deletions cmd/cli/main.go

This file was deleted.

34 changes: 0 additions & 34 deletions cmd/watcher/main.go

This file was deleted.

15 changes: 2 additions & 13 deletions config.yaml → example_config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# Windows Client
application_name: "wowtools_client.exe"
application_name: "wowtools.exe"
wow_dir: "C:\\Program Files (x86)\\World of Warcraft"
retail_dir: "C:\\Program Files (x86)\\World of Warcraft\\_retail_\\"
wtf_dir: "C:\\Program Files (x86)\\World of Warcraft\\_retail_\\WTF"
Expand All @@ -8,14 +7,4 @@ elvui_dir: "C:\\Program Files (x86)\\World of Warcraft\\_retail_\\Interface\\Add
elvui_options_dir: "C:\\Program Files (x86)\\World of Warcraft\\_retail_\\Interface\\AddOns\\ElvUI_OptionsUI\\"
addons_dir: "C:\\Program Files (x86)\\World of Warcraft\\_retail_\\Interface\\AddOns"
curseforge_exe: "C:\\Users\\<USERNAME>\\AppData\\Local\\Programs\\CurseForge\\CurseForge.exe"
retention_rate: 15

# Linux Server
blizzard_clientid: ""
blizzard_clientsecret: ""
discord_webhook_url: ""
wow_token_min: 2000000000
discord_embed_image: "https://media.tenor.com/f42nTKr9aggAAAAC/excited-ronpaul.gif"

# Global Settings
log_level: "debug"
retention_rate: 15
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ go 1.20
require (
github.com/FuzzyStatic/blizzard v1.3.3
github.com/disgoorg/disgo v0.14.1
github.com/google/go-github/v56 v56.0.0
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.8.0
github.com/spf13/viper v1.10.1
)

Expand All @@ -18,7 +20,9 @@ require (
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/go-playground/validator/v10 v10.11.1 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/mitchellh/mapstructure v1.4.3 // indirect
Expand Down
13 changes: 12 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDk
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
github.com/cncf/udpa/go v0.0.0-20200629203442-efcf912fb354/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand Down Expand Up @@ -119,7 +120,11 @@ github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-github/v56 v56.0.0 h1:TysL7dMa/r7wsQi44BjqlwaHvwlFlqkK8CtBWCX3gb4=
github.com/google/go-github/v56 v56.0.0/go.mod h1:D8cdcX98YWJvi7TLo7zM4/h8ZTx6u6fwGEkCdisopo0=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
github.com/google/martian/v3 v3.1.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
Expand All @@ -144,6 +149,8 @@ github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
Expand Down Expand Up @@ -174,6 +181,7 @@ github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFR
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sasha-s/go-csync v0.0.0-20210812194225-61421b77c44b h1:qYTY2tN72LhgDj2rtWG+LI6TXFl2ygFQQ4YezfVaGQE=
github.com/sasha-s/go-csync v0.0.0-20210812194225-61421b77c44b/go.mod h1:/pA7k3zsXKdjjAiUhB5CjuKib9KJGCaLvZwtxGC8U0s=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
Expand All @@ -182,6 +190,8 @@ github.com/spf13/afero v1.8.1 h1:izYHOT71f9iZ7iq37Uqjael60/vYC6vMtzedudZ0zEk=
github.com/spf13/afero v1.8.1/go.mod h1:CtAatgMJh6bJEIs48Ay/FOnkljP3WeGUG0MC1RfAqwo=
github.com/spf13/cast v1.4.1 h1:s0hze+J0196ZfEMTs80N7UlFt0BDuQ7Q+JDnHiMWKdA=
github.com/spf13/cast v1.4.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
github.com/spf13/jwalterweatherman v1.1.0 h1:ue6voC5bR5F8YxI5S67j9i582FU4Qvo2bmqnqMYADFk=
github.com/spf13/jwalterweatherman v1.1.0/go.mod h1:aNWZUN0dPAAO/Ljvb5BEdw96iTZ0EXowPYD95IqWIGo=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
Expand Down Expand Up @@ -516,6 +526,7 @@ gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
98 changes: 98 additions & 0 deletions internal/appUpdate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package internal

import (
"bufio"
"context"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"regexp"
"strings"
"time"
util "wowtools/pkg/utilities"

"github.com/google/go-github/v56/github"
"github.com/spf13/viper"
)

// Get the latest version of the application from the github api
func getPublishedAppVersion(githubClient *github.Client) (string, error) {
opt := &github.ListOptions{Page: 1, PerPage: 1}
releases, rsp, err := githubClient.Repositories.ListReleases(context.Background(), "lyledouglass", "wowtools", opt)
if err != nil {
util.Log.Fatalf("Error getting releases: %v", err)
}
util.Log.Debug(*releases[0].TagName)
util.Log.Tracef("\n%+v\n", rsp)
tageName := *releases[0].TagName
versionRegex := regexp.MustCompile(`^v\d+\.\d+\.\d+$`)
if !versionRegex.MatchString(tageName) {
return "", fmt.Errorf("invalid tag name: %s", tageName)
}
version := strings.TrimPrefix(tageName, "v")
return version, nil
}

// Download the latest version of the application from the github api
func downloadApp(githubClient *github.Client, version string, folderPath string) error {
release, _, err := githubClient.Repositories.GetReleaseByTag(context.Background(), "lyledouglass", "wowtools", "v"+version)
if err != nil {
return fmt.Errorf("error getting release: %s", err)
}
for _, asset := range release.Assets {
util.Log.Debug(*asset.Name)
httpClient := &http.Client{Timeout: 60 * time.Second}
resp, _, err := githubClient.Repositories.DownloadReleaseAsset(context.Background(), "lyledouglass", "wowtools", *release.Assets[0].ID, httpClient)
if err != nil {
return fmt.Errorf("error downloading asset: %s", err)
}
defer resp.Close()
tmpfile, err := os.CreateTemp("", *asset.Name)
if err != nil {
return fmt.Errorf("error creating temp file: %s", err)
}
defer tmpfile.Close()
reader := bufio.NewReader(resp)
writeFile, err := io.Copy(tmpfile, reader)
if err != nil {
return fmt.Errorf("error copying file: %s", err)
}
util.Log.Debugf("Wrote %d bytes to %s", writeFile, tmpfile.Name())

// Move the downloaded file to the specified location
destPath := filepath.Join(folderPath, filepath.Base(*asset.Name))
util.Log.Infof("Moving %s to %s", tmpfile.Name(), destPath)
err = os.Rename(tmpfile.Name(), destPath)
if err != nil {
fmt.Print(err)
return fmt.Errorf("error moving file: %s", err)
}
// update permissions on the new filepath
err = os.Chmod(destPath, 0777)
if err != nil {
return fmt.Errorf("error updating permissions: %s", err)
}
}
return nil
}

func UpdateApp(appVersion string) {
// Wowtools is a public repo, so we won't need to authenticate to access and can pass nil to the client
client := github.NewClient(nil)
latestVersion, err := getPublishedAppVersion(client)
if err != nil {
util.Log.Fatalf("Error getting latest version: %s", err)
//return
}

// Assuming that the version returned from getPublishedAppVersion is valid, we
// can compare the two versions and see if we need to download a new version
if latestVersion > appVersion {
util.Log.Info("New version available, downloading...")
downloadApp(client, latestVersion, viper.GetString("retail_dir")+"Wowtools")
} else {
util.Log.Info("You are running the latest version")
}
}
Loading