1
0
mirror of https://github.com/garraflavatra/rolens.git synced 2025-01-19 05:27:57 +00:00
rolens/internal/app/app.go

190 lines
6.1 KiB
Go
Raw Normal View History

2023-01-14 19:47:29 +00:00
package app
2023-01-20 15:27:51 +00:00
import (
"context"
2023-02-11 10:22:02 +00:00
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
2023-01-20 15:27:51 +00:00
"runtime"
2023-02-18 14:42:51 +00:00
"github.com/garraflavatra/rolens/internal/open_file"
2023-02-18 14:54:18 +00:00
"github.com/gen2brain/beeep"
2023-02-20 20:04:01 +00:00
"github.com/ncruces/zenity"
2023-01-20 15:27:51 +00:00
"github.com/wailsapp/wails/v2/pkg/menu"
"github.com/wailsapp/wails/v2/pkg/menu/keys"
wailsRuntime "github.com/wailsapp/wails/v2/pkg/runtime"
)
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"`
HasMongoExport bool `json:"hasMongoExport"`
HasMongoDump bool `json:"hasMongoDump"`
HomeDirectory string `json:"homeDirectory"`
DataDirectory string `json:"dataDirectory"`
2023-02-11 20:57:52 +00:00
LogDirectory string `json:"logDirectory"`
2023-02-11 10:22:02 +00:00
}
2023-01-14 19:47:29 +00:00
type App struct {
ctx context.Context
2023-02-11 10:22:02 +00:00
Env EnvironmentInfo
2023-01-14 19:47:29 +00:00
}
func NewApp() *App {
2023-02-11 20:57:52 +00:00
a := &App{}
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
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")
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")
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")
2023-02-11 10:22:02 +00:00
default:
panic(errors.New("unsupported platform"))
}
2023-02-11 20:57:52 +00:00
os.MkdirAll(a.Env.DataDirectory, os.ModePerm)
os.MkdirAll(a.Env.LogDirectory, os.ModePerm)
return a
}
func (a *App) Startup(ctx context.Context) {
a.ctx = ctx
wailsRuntime.LogInfo(a.ctx, "Startup")
wailsEnv := wailsRuntime.Environment(a.ctx)
a.Env.Arch = wailsEnv.Arch
a.Env.BuildType = wailsEnv.BuildType
a.Env.Platform = wailsEnv.Platform
}
func (a *App) Shutdown(ctx context.Context) {
wailsRuntime.LogInfo(a.ctx, "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-02-20 20:04:01 +00: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 20:04:01 +00:00
err = os.RemoveAll(a.Env.LogDirectory)
if err == nil {
2023-02-20 20:04:01 +00:00
zenity.Info("Successfully purged log directory.", zenity.InfoIcon)
} else {
2023-02-20 20:04:01 +00:00
zenity.Info(err.Error(), zenity.Title("Encountered an error while purging log directory."), zenity.WarningIcon)
}
}
2023-01-23 13:12:14 +00:00
func menuEventEmitter(a *App, eventName string, data ...interface{}) func(cd *menu.CallbackData) {
return func(cd *menu.CallbackData) {
wailsRuntime.EventsEmit(a.ctx, eventName, data...)
}
}
2023-01-20 15:27:51 +00:00
func (a *App) Menu() *menu.Menu {
appMenu := menu.NewMenu()
aboutMenu := appMenu.AddSubmenu("About")
2023-01-23 13:12:14 +00:00
aboutMenu.AddText("About…", nil, menuEventEmitter(a, "OpenAboutModal"))
aboutMenu.AddText("Prefrences…", keys.CmdOrCtrl(","), menuEventEmitter(a, "OpenPrefrences"))
2023-01-20 15:27:51 +00:00
aboutMenu.AddSeparator()
aboutMenu.AddText("Open log directory…", nil, func(cd *menu.CallbackData) { open_file.Reveal(a.Env.LogDirectory) })
aboutMenu.AddText("Purge logs", nil, func(cd *menu.CallbackData) { a.PurgeLogDirectory() })
aboutMenu.AddSeparator()
2023-01-23 13:12:14 +00:00
aboutMenu.AddText("Quit", keys.CmdOrCtrl("q"), func(cd *menu.CallbackData) { wailsRuntime.Quit(a.ctx) })
2023-01-20 15:27:51 +00:00
2023-01-20 15:28:36 +00:00
if runtime.GOOS == "darwin" {
appMenu.Append(menu.EditMenu())
}
2023-01-23 13:12:14 +00:00
hostMenu := appMenu.AddSubmenu("Host")
hostMenu.AddText("New host", keys.CmdOrCtrl("y"), menuEventEmitter(a, "CreateHost"))
databaseMenu := appMenu.AddSubmenu("Database")
databaseMenu.AddText("New database", keys.CmdOrCtrl("u"), menuEventEmitter(a, "CreateDatabase"))
collectionMenu := appMenu.AddSubmenu("Collection")
collectionMenu.AddText("New collection", keys.CmdOrCtrl("i"), menuEventEmitter(a, "CreateCollection"))
collectionMenu.AddSeparator()
collectionMenu.AddText("Stats", keys.Combo("h", keys.CmdOrCtrlKey, keys.OptionOrAltKey), menuEventEmitter(a, "OpenCollectionTab", "stats"))
collectionMenu.AddText("Find", keys.Combo("f", keys.CmdOrCtrlKey, keys.OptionOrAltKey), menuEventEmitter(a, "OpenCollectionTab", "find"))
collectionMenu.AddText("Insert", keys.Combo("i", keys.CmdOrCtrlKey, keys.OptionOrAltKey), menuEventEmitter(a, "OpenCollectionTab", "insert"))
collectionMenu.AddText("Update", keys.Combo("u", keys.CmdOrCtrlKey, keys.OptionOrAltKey), menuEventEmitter(a, "OpenCollectionTab", "update"))
collectionMenu.AddText("Remove", keys.Combo("r", keys.CmdOrCtrlKey, keys.OptionOrAltKey), menuEventEmitter(a, "OpenCollectionTab", "remove"))
collectionMenu.AddText("Indexes", keys.Combo("x", keys.CmdOrCtrlKey, keys.OptionOrAltKey), menuEventEmitter(a, "OpenCollectionTab", "indexes"))
2023-01-20 15:27:51 +00:00
helpMenu := appMenu.AddSubmenu("Help")
2023-01-23 13:12:14 +00:00
helpMenu.AddText("User guide", nil, func(cd *menu.CallbackData) { wailsRuntime.BrowserOpenURL(a.ctx, "") })
2023-01-20 15:27:51 +00:00
return appMenu
}
2023-02-11 10:22:02 +00:00
func (a *App) OpenDirectory(id, title string) string {
if title == "" {
title = "Choose a directory"
}
2023-02-11 20:57:52 +00:00
wailsRuntime.LogInfo(a.ctx, fmt.Sprintf("Opening directory ('%v')", title))
2023-02-20 20:04:01 +00:00
dir, err := zenity.SelectFile(zenity.Title(title), zenity.Directory())
2023-02-11 20:57:52 +00:00
2023-02-20 20:04:01 +00:00
if err != nil && err != zenity.ErrCanceled {
2023-02-11 20:57:52 +00:00
wailsRuntime.LogWarning(a.ctx, "Encountered an error while opening directory:")
wailsRuntime.LogWarning(a.ctx, err.Error())
2023-02-11 10:22:02 +00:00
wailsRuntime.MessageDialog(a.ctx, wailsRuntime.MessageDialogOptions{
Type: wailsRuntime.ErrorDialog,
Title: "Encountered an error while opening directory",
Message: err.Error(),
})
}
2023-02-11 20:57:52 +00:00
wailsRuntime.LogInfo(a.ctx, "Chosen directory: "+dir)
2023-02-11 10:22:02 +00:00
return dir
}
2023-02-18 14:54:18 +00:00
2023-02-20 20:04:01 +00:00
func (a *App) EnterText(title, info string) string {
println("wertyuijhgfd")
input, err := zenity.Entry(info, zenity.Title(title))
if err == zenity.ErrCanceled {
return ""
} else if err != nil {
zenity.Info(err.Error(), zenity.Title("Encountered an error!"), zenity.ErrorIcon)
return ""
} else {
return input
}
}
2023-02-18 14:54:18 +00:00
func (a *App) Beep() {
2023-02-19 19:56:44 +00:00
if runtime.GOOS == "windows" {
return
}
2023-02-18 14:54:18 +00:00
beeep.Beep(beeep.DefaultFreq, beeep.DefaultDuration)
}