Skip to content

Commit

Permalink
Merge branch 'app-fixes'
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaz492 committed Sep 29, 2024
2 parents 3e1286f + 4a47ef2 commit 039a8ae
Show file tree
Hide file tree
Showing 15 changed files with 686 additions and 202 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ build/darwin/arm64:
$(info Building debug tool for darwin arm64)
GOOS=darwin GOARCH=arm64 go build -o out/ftb-debug-darwin-arm64 -trimpath -buildvcs=false -ldflags "-s -w -X 'main.GitCommit=$(GITHUB_SHA_SHORT)' -X 'main.Version=$(GITHUB_REF_NAME)'"

build_all: build/linux/arm build/linux/arm64 build/linux/amd64 build/windows/arm64 build/windows/amd64 build/darwin/amd64 build/darwin/arm64
build_all: build/linux/arm64 build/linux/amd64 build/windows/arm64 build/windows/amd64 build/darwin/amd64 build/darwin/arm64
19 changes: 10 additions & 9 deletions app.go → dbg/app.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package main
package dbg

import (
"bytes"
"compress/gzip"
"encoding/json"
"errors"
"fmt"
"ftb-debug/v2/shared"
"github.com/hashicorp/go-version"
"github.com/pterm/pterm"
"io"
Expand Down Expand Up @@ -39,7 +40,7 @@ func loadAppSettings() error {
var appSettings []byte
var err error
appSettingsPath := filepath.Join(ftbApp.InstallLocation, "storage", "settings.json")
doesAppSettingsExist := doesPathExist(appSettingsPath)
doesAppSettingsExist := shared.DoesPathExist(appSettingsPath)
if doesAppSettingsExist {
appSettings, err = os.ReadFile(appSettingsPath)
if err != nil {
Expand All @@ -61,7 +62,7 @@ func loadAppSettings() error {
}

func getInstances() (map[string]Instances, []InstanceLogs, error) {
instancesExists := doesPathExist(ftbApp.Settings.InstanceLocation)
instancesExists := shared.DoesPathExist(ftbApp.Settings.InstanceLocation)
if instancesExists {
pterm.Info.Println("Instance Location: ", ftbApp.Settings.InstanceLocation)
instances, _ := os.ReadDir(filepath.Join(ftbApp.Settings.InstanceLocation))
Expand Down Expand Up @@ -109,7 +110,7 @@ func getInstances() (map[string]Instances, []InstanceLogs, error) {
// Check for logs
logsPath := filepath.Join(ftbApp.Settings.InstanceLocation, name, "logs")
logs := make(map[string]string)
if doesPathExist(logsPath) {
if shared.DoesPathExist(logsPath) {
logs, err = getInstanceLogs(logsPath)
if err != nil {
pterm.Error.Printfln("Error getting instance logs: %s", err.Error())
Expand All @@ -119,7 +120,7 @@ func getInstances() (map[string]Instances, []InstanceLogs, error) {
// Check for crash-reports
crashLogsPath := filepath.Join(ftbApp.Settings.InstanceLocation, name, "crash-reports")
crashLogs := make(map[string]string)
if doesPathExist(crashLogsPath) {
if shared.DoesPathExist(crashLogsPath) {
crashLogs, err = getInstanceLogs(crashLogsPath)
if err != nil {
pterm.Error.Printfln("Error getting instance crash logs: %s", err.Error())
Expand Down Expand Up @@ -177,7 +178,7 @@ func getAppVersion() (AppMeta, error) {
return versions[i].GreaterThan(versions[j])
})
pterm.Debug.Println("Found versions:", versions)
if !doesPathExist(metaPath) {
if !shared.DoesPathExist(metaPath) {
metaPath = filepath.Join(overwolfAppPath, versions[0].String(), "meta.json")
}
}
Expand All @@ -190,7 +191,7 @@ func getAppVersion() (AppMeta, error) {
return AppMeta{}, errors.New("unknown OS, could you let us know what operating system you are using so we can add our checks")
}

installExists := doesPathExist(metaPath)
installExists := shared.DoesPathExist(metaPath)
if !installExists {
return AppMeta{}, errors.New("app meta not found")
}
Expand All @@ -209,7 +210,7 @@ func getAppVersion() (AppMeta, error) {

func getProfiles() (Profiles, error) {
profilesPath := filepath.Join(ftbApp.InstallLocation, "profiles.json")
profilesExists := doesPathExist(profilesPath)
profilesExists := shared.DoesPathExist(profilesPath)
if profilesExists {
profilesRaw, err := os.ReadFile(profilesPath)
if err != nil {
Expand Down Expand Up @@ -301,7 +302,7 @@ func getInstanceLogs(path string) (map[string]string, error) {
}

func getMiscFile(path string) (string, error) {
exists := doesPathExist(path)
exists := shared.DoesPathExist(path)
if !exists {
return "", fmt.Errorf("file %s does not exist", path)
}
Expand Down
2 changes: 1 addition & 1 deletion consts.go → dbg/consts.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package dbg

import (
"net/http"
Expand Down
166 changes: 166 additions & 0 deletions dbg/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
package dbg

import (
"encoding/json"
"fmt"
"github.com/pterm/pterm"
"io"
"os"
"os/user"
"path/filepath"
"regexp"
"time"
)

var (
ftbApp FTBApp
logFile *os.File
logMw io.Writer
owUID = "cmogmmciplgmocnhikmphehmeecmpaggknkjlbag"
re = regexp.MustCompile(`(?m)[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}`)
foundOverwolfVersion = false
failedToLoadSettings = false
)

func RunDebug() {
var err error
logFile, err = os.CreateTemp("", "ftb-dbg-log")
if err != nil {
pterm.Fatal.Println(err)
}

var manifest Manifest

defer cleanup(logFile)
logMw = io.MultiWriter(os.Stdout, NewCustomWriter(logFile))
pterm.SetDefaultOutput(logMw)

pterm.DefaultHeader.Println("System Info")
getOSInfo()
usr, err := user.Current()
if err != nil {
pterm.Error.Println("Failed to get users home directory")
}
ftbApp.User = usr

pterm.DefaultHeader.Println("Running Network Checks")
nc := runNetworkChecks()
for _, n := range nc {
if n.Error {
pterm.Error.Println(n.Status)
} else if !n.Success && !n.Error {
pterm.Warning.Println(n.Status)
} else {
pterm.Success.Printfln("%s: %s", n.URL, n.Status)
}
}

pterm.DefaultHeader.Println("Running App Checks")
runAppChecks()
profiles, err := getProfiles()
hasActiveAccount := false
if err != nil {
pterm.Error.Println("Failed to get profiles:", err)
} else {
hasActiveAccount = isActiveProfileInProfiles(profiles)
}

pterm.DefaultSection.WithLevel(2).Println("App info")
pterm.Info.Println(fmt.Sprintf("Located app at %s", ftbApp.InstallLocation))
appVerData, err := getAppVersion()
if err != nil {
pterm.Error.Println("Error getting app version:", err)
} else {
pterm.Info.Println("App version:", appVerData.AppVersion)
pterm.Info.Println("App release date:", time.Unix(int64(appVerData.Released), 0))
pterm.Info.Println("Branch:", appVerData.Branch)
}

appLogs := make(map[string]string)
instances := make(map[string]Instances)
instanceLogs := make([]InstanceLogs, 0)

appLogs, err = getAppLogs()
if err != nil {
pterm.Error.Println("Failed to get app logs:", err)
return
}
if !failedToLoadSettings {
pterm.DefaultSection.Println("Check for instances")
instances, instanceLogs, err = getInstances()
if err != nil {
pterm.Error.Println("Failed to get instances:", err)
}
}

// Additional files to upload
miscFiles := []string{
filepath.Join(ftbApp.InstallLocation, "storage", "settings.json"),
filepath.Join(ftbApp.InstallLocation, "bin", "runtime", "installations.json"),
}
if foundOverwolfVersion {
miscFiles = append(miscFiles, filepath.Join(overwolfAppLogs, "index.html.log"))
miscFiles = append(miscFiles, filepath.Join(overwolfAppLogs, "background.html.log"))
miscFiles = append(miscFiles, filepath.Join(overwolfAppLogs, "chat.html.log"))
}

for _, mf := range miscFiles {
id, err := getMiscFile(mf)
if err != nil {
pterm.Error.Println("Error getting file:", err)
continue
}
appLogs[filepath.Base(mf)] = id
}

tUpload, err := os.ReadFile(logFile.Name())
if err != nil {
pterm.Error.Println("Failed to read dbg output", logFile.Name())
pterm.Error.Println(err)
} else {
if len(tUpload) > 0 {
resp, err := uploadRequest(tUpload, "")
if err != nil {
pterm.Error.Println("Failed to upload support file...")
pterm.Error.Println(err)
} else {
appLogs["dbg-tool-output"] = resp.Data.ID
}
}
}

// Compile manifest
manifest.Version = "2.0.5-go"
manifest.MetaDetails = MetaDetails{
InstanceCount: len(instances),
Today: time.Now().UTC().Format(time.DateOnly),
Time: time.Now().Unix(),
AddedAccounts: len(profiles.Profiles),
HasActiveAccounts: hasActiveAccount,
}
manifest.AppDetails = AppDetails{
App: appVerData.Commit,
SharedVersion: appVerData.AppVersion,
Meta: appVerData,
}
manifest.AppLogs = appLogs
manifest.ProviderInstanceMapping = instances
manifest.InstanceLogs = instanceLogs
manifest.NetworkChecks = nc

pterm.DefaultHeader.Println("Manifest")
jsonManifest, err := json.MarshalIndent(manifest, "", " ")
if err != nil {
pterm.Error.Println("Error marshalling manifest:", err)
return
}
if len(jsonManifest) > 0 {
request, err := uploadRequest(jsonManifest, "json")
if err != nil {
pterm.Error.Println("Failed to upload manifest:", err)
return
}
codeStyle := pterm.NewStyle(pterm.FgLightMagenta, pterm.Bold)
pterm.DefaultBasicText.Printfln("Please provide this code to support: %s", codeStyle.Sprintf("dbg:%s", request.Data.ID))
}
}
2 changes: 1 addition & 1 deletion structs.go → dbg/structs.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package dbg

import (
"os/user"
Expand Down
2 changes: 1 addition & 1 deletion unix_utils.go → dbg/unix_utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//go:build !windows

package main
package dbg

import (
"errors"
Expand Down
22 changes: 8 additions & 14 deletions utils.go → dbg/utils.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package main
package dbg

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"ftb-debug/v2/shared"
"github.com/pterm/pterm"
"github.com/shirou/gopsutil/v3/cpu"
"github.com/shirou/gopsutil/v3/mem"
Expand Down Expand Up @@ -44,7 +45,7 @@ func ByteCountIEC(b int64) string {
}

func validateJson(message string, filePath string) (bool, error) {
jsonF := doesPathExist(filePath)
jsonF := shared.DoesPathExist(filePath)
if jsonF {
jsonFile, err := os.Open(filePath)
if err != nil {
Expand Down Expand Up @@ -93,13 +94,6 @@ func getOSInfo() {
}
}

func doesPathExist(filePath string) bool {
if _, err := os.Stat(filePath); err == nil {
return true
}
return false
}

func uploadRequest(data []byte, lang string) (PsteMeResp, error) {
// http put request to https://pste.me/v1/paste

Expand Down Expand Up @@ -148,29 +142,29 @@ func sanitize(data []byte) []byte {
}

func doesBinExist() {
binExists := doesPathExist(filepath.Join(ftbApp.InstallLocation, "bin"))
binExists := shared.DoesPathExist(filepath.Join(ftbApp.InstallLocation, "bin"))
if binExists {
ftbApp.Structure.Bin.Exists = true
}
}

func locateFTBAFolder() (string, error) {
if runtime.GOOS == "windows" {
if doesPathExist(filepath.Join(os.Getenv("localappdata"), ".ftba")) {
if shared.DoesPathExist(filepath.Join(os.Getenv("localappdata"), ".ftba")) {
return filepath.Join(os.Getenv("localappdata"), ".ftba"), nil
} else if doesPathExist(filepath.Join(ftbApp.User.HomeDir, ".ftba")) {
} else if shared.DoesPathExist(filepath.Join(ftbApp.User.HomeDir, ".ftba")) {
return filepath.Join(ftbApp.User.HomeDir, ".ftba"), nil
} else {
return "", errors.New("unable to find .ftba directory")
}
} else if runtime.GOOS == "darwin" {
if doesPathExist(filepath.Join(os.Getenv("HOME"), "Library", "Application Support", ".ftba")) {
if shared.DoesPathExist(filepath.Join(os.Getenv("HOME"), "Library", "Application Support", ".ftba")) {
return filepath.Join(os.Getenv("HOME"), "Library", "Application Support", ".ftba"), nil
} else {
return "", errors.New("unable to find .ftba directory")
}
} else if runtime.GOOS == "linux" {
if doesPathExist(filepath.Join(ftbApp.User.HomeDir, ".ftba")) {
if shared.DoesPathExist(filepath.Join(ftbApp.User.HomeDir, ".ftba")) {
return filepath.Join(ftbApp.User.HomeDir, ".ftba"), nil
} else {
return "", errors.New("unable to find .ftba directory")
Expand Down
4 changes: 2 additions & 2 deletions win_utils.go → dbg/win_utils.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//go:build windows

package main
package dbg

import (
"fmt"
"github.com/yusufpapurcu/wmi"
wmi "github.com/yusufpapurcu/wmi"
)

type (
Expand Down
Loading

0 comments on commit 039a8ae

Please sign in to comment.