1
0
mirror of https://github.com/garraflavatra/rolens.git synced 2025-01-18 13:07:58 +00:00
rolens/internal/app/app.go

191 lines
5.0 KiB
Go
Raw Permalink Normal View History

2023-01-14 19:47:29 +00:00
package app
2023-01-20 15:27:51 +00:00
import (
"context"
2023-05-28 20:05:06 +00:00
"encoding/json"
2023-02-11 10:22:02 +00:00
"errors"
2023-05-27 19:18:47 +00:00
"fmt"
2023-02-11 10:22:02 +00:00
"os"
"os/exec"
2023-05-28 20:05:06 +00:00
"path"
2023-02-11 10:22:02 +00:00
"path/filepath"
2023-01-20 15:27:51 +00:00
"runtime"
"strings"
2023-01-20 15:27:51 +00:00
2023-02-21 16:47:21 +00:00
"github.com/garraflavatra/rolens/internal/ui"
2023-01-20 15:27:51 +00:00
wailsRuntime "github.com/wailsapp/wails/v2/pkg/runtime"
"golang.org/x/sync/syncmap"
2023-01-20 15:27:51 +00:00
)
2023-01-14 19:47:29 +00:00
2023-02-11 10:22:02 +00:00
type EnvironmentInfo struct {
Arch string `json:"arch"`
BuildType string `json:"buildType"`
Platform string `json:"platform"`
Version string `json:"version"`
2023-02-11 10:22:02 +00:00
HasMongoExport bool `json:"hasMongoExport"`
HasMongoDump bool `json:"hasMongoDump"`
HasMongoShell bool `json:"hasMongoShell"`
2023-02-11 10:22:02 +00:00
HomeDirectory string `json:"homeDirectory"`
DataDirectory string `json:"dataDirectory"`
LogDirectory string `json:"logDirectory"`
DownloadDirectory string `json:"downloadDirectory"`
2023-02-11 10:22:02 +00:00
}
2023-01-14 19:47:29 +00:00
type App struct {
2023-05-27 19:18:47 +00:00
Env EnvironmentInfo
State syncmap.Map
2023-05-27 19:18:47 +00:00
ctx context.Context
ui *ui.UI
2023-01-14 19:47:29 +00:00
}
func NewApp(version string) *App {
a := &App{}
a.Env.Version = strings.TrimSpace(version)
2023-02-11 10:22:02 +00:00
_, err := exec.LookPath("mongodump")
a.Env.HasMongoDump = err == nil
_, err = exec.LookPath("mongoexport")
a.Env.HasMongoExport = err == nil
_, err = exec.LookPath("mongosh")
a.Env.HasMongoShell = err == nil
2023-02-11 10:22:02 +00:00
a.Env.HomeDirectory, err = os.UserHomeDir()
if err != nil {
panic(errors.New("encountered an error while getting home directory"))
}
switch runtime.GOOS {
case "windows":
a.Env.DataDirectory = filepath.Join(a.Env.HomeDirectory, "/AppData/Local/Rolens")
2023-02-11 20:57:52 +00:00
a.Env.LogDirectory = filepath.Join(a.Env.HomeDirectory, "/AppData/Local/Rolens/Logs")
a.Env.DownloadDirectory = filepath.Join(a.Env.HomeDirectory, "/Downloads")
2023-02-11 10:22:02 +00:00
case "darwin":
a.Env.DataDirectory = filepath.Join(a.Env.HomeDirectory, "/Library/Application Support/Rolens")
2023-02-11 20:57:52 +00:00
a.Env.LogDirectory = filepath.Join(a.Env.HomeDirectory, "/Library/Logs/Rolens")
a.Env.DownloadDirectory = filepath.Join(a.Env.HomeDirectory, "/Downloads")
2023-02-11 10:22:02 +00:00
case "linux":
2023-02-11 20:57:52 +00:00
a.Env.DataDirectory = filepath.Join(a.Env.HomeDirectory, "/.config/rolens")
a.Env.LogDirectory = filepath.Join(a.Env.HomeDirectory, "/.config/rolens/logs")
a.Env.DownloadDirectory = filepath.Join(a.Env.HomeDirectory, "/Downloads")
2023-02-11 10:22:02 +00:00
default:
panic(errors.New("unsupported platform"))
}
os.MkdirAll(a.Env.DataDirectory, 0755)
os.MkdirAll(a.Env.LogDirectory, 0755)
2023-02-11 20:57:52 +00:00
return a
}
2023-02-21 16:47:21 +00:00
func (a *App) Startup(ctx context.Context, ui *ui.UI) {
2023-02-11 20:57:52 +00:00
a.ctx = ctx
2023-02-21 16:47:21 +00:00
a.ui = ui
2023-05-27 19:18:47 +00:00
wailsRuntime.LogInfo(a.ctx, "Runcycle: Startup")
2023-02-11 20:57:52 +00:00
wailsEnv := wailsRuntime.Environment(a.ctx)
a.Env.Arch = wailsEnv.Arch
a.Env.BuildType = wailsEnv.BuildType
a.Env.Platform = wailsEnv.Platform
2023-05-28 20:05:06 +00:00
jsonEnv, err := json.MarshalIndent(a.Env, "", " ")
if err != nil {
wailsRuntime.LogWarningf(a.ctx, "Could not marshal environment info: %s", err.Error())
}
err = os.WriteFile(path.Join(a.Env.LogDirectory, "environment.json"), jsonEnv, 0644)
if err != nil {
wailsRuntime.LogWarningf(a.ctx, "Could not save environment.json: %s", err.Error())
}
2023-02-11 20:57:52 +00:00
}
func (a *App) Shutdown(ctx context.Context) {
2023-05-27 19:18:47 +00:00
wailsRuntime.LogInfo(a.ctx, "Runcycle: Shutdown")
2023-02-11 10:22:02 +00:00
}
func (a *App) Environment() EnvironmentInfo {
return a.Env
2023-01-14 19:47:29 +00:00
}
2023-01-20 15:27:51 +00:00
func (a *App) PurgeLogDirectory() {
2023-06-24 18:27:48 +00:00
choice, _ := wailsRuntime.MessageDialog(a.ctx, wailsRuntime.MessageDialogOptions{
Title: "Confirm",
Message: "Are you sure you want to remove all logfiles?",
Buttons: []string{"Yes", "Cancel"},
DefaultButton: "Yes",
CancelButton: "Cancel",
})
if choice != "Yes" {
return
}
2023-06-24 18:27:48 +00:00
err := os.RemoveAll(a.Env.LogDirectory)
if err == nil {
2023-06-24 18:27:48 +00:00
wailsRuntime.MessageDialog(a.ctx, wailsRuntime.MessageDialogOptions{
Title: "Success",
Message: "Successfully purged log directory",
Type: wailsRuntime.InfoDialog,
})
} else {
2023-06-24 18:27:48 +00:00
wailsRuntime.MessageDialog(a.ctx, wailsRuntime.MessageDialogOptions{
Title: "Error while purging log directory",
Message: err.Error(),
Type: wailsRuntime.ErrorDialog,
})
}
}
2023-05-27 19:18:47 +00:00
func (a *App) ReportSharedStateVariable(key, value string) {
a.State.Store(key, value)
2023-05-28 20:05:06 +00:00
wailsRuntime.LogDebug(a.ctx, fmt.Sprintf("State: %s=\"%s\"", key, value))
2023-01-20 15:27:51 +00:00
}
func (a *App) AskConfirmation(message string) bool {
var title string = ""
if runtime.GOOS == "darwin" {
title = message
message = ""
} else {
title = "Confirm"
}
button, err := wailsRuntime.MessageDialog(a.ctx, wailsRuntime.MessageDialogOptions{
Type: wailsRuntime.QuestionDialog,
Title: title,
Message: message,
Buttons: []string{"Yes", "No"},
DefaultButton: "Yes",
CancelButton: "No",
})
if err == nil {
return button == "Yes"
} else {
return false
}
}
2023-12-22 14:26:29 +00:00
func (a *App) ChooseDirectory(title string) string {
if title == "" {
title = "Choose a directory"
}
dir, err := wailsRuntime.OpenDirectoryDialog(a.ctx, wailsRuntime.OpenDialogOptions{
Title: title,
DefaultDirectory: a.Env.DownloadDirectory,
2023-12-22 14:26:29 +00:00
CanCreateDirectories: true,
})
if err != nil {
wailsRuntime.MessageDialog(a.ctx, wailsRuntime.MessageDialogOptions{
Title: "Error while opening directory",
Message: err.Error(),
Type: wailsRuntime.ErrorDialog,
})
}
return dir
}