1
0
mirror of https://github.com/garraflavatra/rolens.git synced 2024-12-01 06:20:40 +01:00
rolens/internal/app/app.go

125 lines
3.5 KiB
Go
Raw Normal View History

2023-01-14 20:47:29 +01:00
package app
2023-01-20 16:27:51 +01:00
import (
"context"
2023-05-28 22:05:06 +02:00
"encoding/json"
2023-02-11 11:22:02 +01:00
"errors"
2023-05-27 21:18:47 +02:00
"fmt"
2023-02-11 11:22:02 +01:00
"os"
"os/exec"
2023-05-28 22:05:06 +02:00
"path"
2023-02-11 11:22:02 +01:00
"path/filepath"
2023-01-20 16:27:51 +01:00
"runtime"
2023-02-21 17:47:21 +01:00
"github.com/garraflavatra/rolens/internal/ui"
2023-02-20 21:04:01 +01:00
"github.com/ncruces/zenity"
2023-01-20 16:27:51 +01:00
wailsRuntime "github.com/wailsapp/wails/v2/pkg/runtime"
)
2023-01-14 20:47:29 +01:00
2023-02-11 11:22:02 +01:00
type EnvironmentInfo struct {
Arch string `json:"arch"`
BuildType string `json:"buildType"`
Platform string `json:"platform"`
HasMongoExport bool `json:"hasMongoExport"`
HasMongoDump bool `json:"hasMongoDump"`
HomeDirectory string `json:"homeDirectory"`
DataDirectory string `json:"dataDirectory"`
LogDirectory string `json:"logDirectory"`
DownloadDirectory string `json:"downloadDirectory"`
2023-02-11 11:22:02 +01:00
}
2023-01-14 20:47:29 +01:00
type App struct {
2023-05-27 21:18:47 +02:00
Env EnvironmentInfo
State map[string]string
ctx context.Context
ui *ui.UI
2023-01-14 20:47:29 +01:00
}
func NewApp() *App {
2023-05-27 21:18:47 +02:00
a := &App{
State: make(map[string]string),
}
2023-02-11 11:22:02 +01:00
_, err := exec.LookPath("mongodump")
a.Env.HasMongoDump = err == nil
_, err = exec.LookPath("mongoexport")
a.Env.HasMongoExport = err == nil
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 21:57:52 +01: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 11:22:02 +01:00
case "darwin":
a.Env.DataDirectory = filepath.Join(a.Env.HomeDirectory, "/Library/Application Support/Rolens")
2023-02-11 21:57:52 +01:00
a.Env.LogDirectory = filepath.Join(a.Env.HomeDirectory, "/Library/Logs/Rolens")
a.Env.DownloadDirectory = filepath.Join(a.Env.HomeDirectory, "/Downloads")
2023-02-11 11:22:02 +01:00
case "linux":
2023-02-11 21:57:52 +01: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 11:22:02 +01:00
default:
panic(errors.New("unsupported platform"))
}
2023-02-11 21:57:52 +01:00
os.MkdirAll(a.Env.DataDirectory, os.ModePerm)
os.MkdirAll(a.Env.LogDirectory, os.ModePerm)
return a
}
2023-02-21 17:47:21 +01:00
func (a *App) Startup(ctx context.Context, ui *ui.UI) {
2023-02-11 21:57:52 +01:00
a.ctx = ctx
2023-02-21 17:47:21 +01:00
a.ui = ui
2023-05-27 21:18:47 +02:00
wailsRuntime.LogInfo(a.ctx, "Runcycle: Startup")
2023-02-11 21:57:52 +01:00
wailsEnv := wailsRuntime.Environment(a.ctx)
a.Env.Arch = wailsEnv.Arch
a.Env.BuildType = wailsEnv.BuildType
a.Env.Platform = wailsEnv.Platform
2023-05-28 22:05:06 +02: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 21:57:52 +01:00
}
func (a *App) Shutdown(ctx context.Context) {
2023-05-27 21:18:47 +02:00
wailsRuntime.LogInfo(a.ctx, "Runcycle: Shutdown")
2023-02-11 11:22:02 +01:00
}
func (a *App) Environment() EnvironmentInfo {
return a.Env
2023-01-14 20:47:29 +01:00
}
2023-01-20 16:27:51 +01:00
func (a *App) PurgeLogDirectory() {
2023-02-20 21:04:01 +01:00
err := zenity.Question("Are you sure you want to remove all logfiles?", zenity.Title("Confirm"), zenity.WarningIcon)
if err == zenity.ErrCanceled {
return
}
2023-02-20 21:04:01 +01:00
err = os.RemoveAll(a.Env.LogDirectory)
if err == nil {
2023-02-20 21:04:01 +01:00
zenity.Info("Successfully purged log directory.", zenity.InfoIcon)
} else {
2023-02-21 17:27:44 +01:00
zenity.Error(err.Error(), zenity.Title("Encountered an error while purging log directory."), zenity.WarningIcon)
}
}
2023-05-27 21:18:47 +02:00
func (a *App) ReportSharedStateVariable(key, value string) {
a.State[key] = value
2023-05-28 22:05:06 +02:00
wailsRuntime.LogDebug(a.ctx, fmt.Sprintf("State: %s=\"%s\"", key, value))
2023-01-20 16:27:51 +01:00
}