diff --git a/frontend/src/main.js b/frontend/src/main.js index 3cadb62..8f2861f 100644 --- a/frontend/src/main.js +++ b/frontend/src/main.js @@ -1,6 +1,12 @@ import './reset.css'; import './style.css'; import App from './app.svelte'; +import { LogError } from '../wailsjs/runtime/runtime'; + +window.addEventListener('unhandledrejection', event => { + LogError('Unhandled Rejection in JS! Reason:'); + LogError(String(event.reason)); +}); const app = new App({ target: document.getElementById('app') }); diff --git a/frontend/wailsjs/go/models.ts b/frontend/wailsjs/go/models.ts index bcae1b1..98d34b4 100755 --- a/frontend/wailsjs/go/models.ts +++ b/frontend/wailsjs/go/models.ts @@ -8,6 +8,7 @@ export namespace app { hasMongoDump: boolean; homeDirectory: string; dataDirectory: string; + logDirectory: string; static createFrom(source: any = {}) { return new EnvironmentInfo(source); @@ -22,6 +23,7 @@ export namespace app { this.hasMongoDump = source["hasMongoDump"]; this.homeDirectory = source["homeDirectory"]; this.dataDirectory = source["dataDirectory"]; + this.logDirectory = source["logDirectory"]; } } export class Settings { diff --git a/internal/app/app.go b/internal/app/app.go index f3c2b02..b509d0e 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -9,6 +9,7 @@ import ( "path/filepath" "runtime" + "github.com/garraflavatra/rolens/internal/utils/open_file" "github.com/wailsapp/wails/v2/pkg/menu" "github.com/wailsapp/wails/v2/pkg/menu/keys" wailsRuntime "github.com/wailsapp/wails/v2/pkg/runtime" @@ -24,6 +25,7 @@ type EnvironmentInfo struct { HomeDirectory string `json:"homeDirectory"` DataDirectory string `json:"dataDirectory"` + LogDirectory string `json:"logDirectory"` } type App struct { @@ -32,16 +34,7 @@ type App struct { } func NewApp() *App { - return &App{} -} - -func (a *App) Startup(ctx context.Context) { - a.ctx = ctx - wailsEnv := wailsRuntime.Environment(a.ctx) - - a.Env.Arch = wailsEnv.Arch - a.Env.BuildType = wailsEnv.BuildType - a.Env.Platform = wailsEnv.Platform + a := &App{} _, err := exec.LookPath("mongodump") a.Env.HasMongoDump = err == nil @@ -57,15 +50,35 @@ func (a *App) Startup(ctx context.Context) { switch runtime.GOOS { case "windows": a.Env.DataDirectory = filepath.Join(a.Env.HomeDirectory, "/AppData/Local/Rolens") + a.Env.LogDirectory = filepath.Join(a.Env.HomeDirectory, "/AppData/Local/Rolens/Logs") case "darwin": a.Env.DataDirectory = filepath.Join(a.Env.HomeDirectory, "/Library/Application Support/Rolens") + a.Env.LogDirectory = filepath.Join(a.Env.HomeDirectory, "/Library/Logs/Rolens") case "linux": - a.Env.DataDirectory = filepath.Join(a.Env.HomeDirectory, "/.config/Rolens") + a.Env.DataDirectory = filepath.Join(a.Env.HomeDirectory, "/.config/rolens") + a.Env.LogDirectory = filepath.Join(a.Env.HomeDirectory, "/.config/rolens/logs") default: panic(errors.New("unsupported platform")) } - _ = os.MkdirAll(a.Env.DataDirectory, os.ModePerm) + 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") } func (a *App) Environment() EnvironmentInfo { @@ -109,6 +122,8 @@ func (a *App) Menu() *menu.Menu { helpMenu := appMenu.AddSubmenu("Help") helpMenu.AddText("User guide", nil, func(cd *menu.CallbackData) { wailsRuntime.BrowserOpenURL(a.ctx, "") }) + helpMenu.AddSeparator() + helpMenu.AddText("Open log directory", nil, func(cd *menu.CallbackData) { open_file.Reveal(a.Env.LogDirectory) }) return appMenu } @@ -118,6 +133,8 @@ func (a *App) OpenDirectory(id, title string) string { title = "Choose a directory" } + wailsRuntime.LogInfo(a.ctx, fmt.Sprintf("Opening directory ('%v')", title)) + dir, err := wailsRuntime.OpenDirectoryDialog(a.ctx, wailsRuntime.OpenDialogOptions{ Title: title, CanCreateDirectories: true, @@ -125,7 +142,8 @@ func (a *App) OpenDirectory(id, title string) string { }) if err != nil { - fmt.Println(err.Error()) + wailsRuntime.LogWarning(a.ctx, "Encountered an error while opening directory:") + wailsRuntime.LogWarning(a.ctx, err.Error()) wailsRuntime.MessageDialog(a.ctx, wailsRuntime.MessageDialogOptions{ Type: wailsRuntime.ErrorDialog, Title: "Encountered an error while opening directory", @@ -133,5 +151,6 @@ func (a *App) OpenDirectory(id, title string) string { }) } + wailsRuntime.LogInfo(a.ctx, "Chosen directory: "+dir) return dir } diff --git a/internal/app/app_settings.go b/internal/app/app_settings.go index 5fcd4e3..77c3091 100644 --- a/internal/app/app_settings.go +++ b/internal/app/app_settings.go @@ -2,7 +2,6 @@ package app import ( "encoding/json" - "fmt" "io/ioutil" "os" "path" @@ -34,7 +33,8 @@ func (a *App) Settings() Settings { if err != nil { // It's ok if the file cannot be opened, for example if it is not accessible. // Therefore no error is returned. - fmt.Println(err.Error()) + runtime.LogInfo(a.ctx, "Cannot open settings.json:") + runtime.LogInfo(a.ctx, err.Error()) return s } @@ -44,7 +44,8 @@ func (a *App) Settings() Settings { err = json.Unmarshal(jsonData, &s) if err != nil { - fmt.Println(err.Error()) + runtime.LogWarning(a.ctx, "Cannot unmarshal settings.json:") + runtime.LogWarning(a.ctx, err.Error()) runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{ Type: runtime.ErrorDialog, Title: "Could not retrieve application settings, using defaults!", @@ -59,6 +60,8 @@ func (a *App) UpdateSettings(jsonData string) Settings { s := a.Settings() err := json.Unmarshal([]byte(jsonData), &s) if err != nil { + runtime.LogError(a.ctx, "Malformed JSON for settings file:") + runtime.LogError(a.ctx, err.Error()) runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{ Type: runtime.InfoDialog, Title: "Malformed JSON", @@ -69,6 +72,8 @@ func (a *App) UpdateSettings(jsonData string) Settings { newJson, err := json.MarshalIndent(s, "", "\t") if err != nil { + runtime.LogError(a.ctx, "Could not marshal settings into JSON:") + runtime.LogError(a.ctx, err.Error()) runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{ Type: runtime.InfoDialog, Title: "Could not marshal settings into JSON", @@ -80,6 +85,8 @@ func (a *App) UpdateSettings(jsonData string) Settings { filePath := path.Join(a.Env.DataDirectory, "settings.json") err = ioutil.WriteFile(filePath, newJson, os.ModePerm) if err != nil { + runtime.LogError(a.ctx, "Could not update host list:") + runtime.LogError(a.ctx, err.Error()) runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{ Type: runtime.InfoDialog, Title: "Could not update host list", diff --git a/internal/app/collection.go b/internal/app/collection.go index d85991a..e36af37 100644 --- a/internal/app/collection.go +++ b/internal/app/collection.go @@ -10,14 +10,14 @@ import ( func (a *App) OpenCollection(hostKey, dbKey, collKey string) (result bson.M) { client, ctx, close, err := a.connectToHost(hostKey) if err != nil { - fmt.Println(err.Error()) return nil } command := bson.M{"collStats": collKey} err = client.Database(dbKey).RunCommand(ctx, command).Decode(&result) if err != nil { - fmt.Println(err.Error()) + runtime.LogWarning(a.ctx, "Could not retrieve collection stats for "+collKey) + runtime.LogWarning(a.ctx, err.Error()) runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{ Type: runtime.ErrorDialog, Title: "Could not retrieve collection stats for " + collKey, @@ -33,7 +33,6 @@ func (a *App) OpenCollection(hostKey, dbKey, collKey string) (result bson.M) { func (a *App) RenameCollection(hostKey, dbKey, collKey, newCollKey string) bool { client, ctx, close, err := a.connectToHost(hostKey) if err != nil { - fmt.Println(err.Error()) return false } @@ -44,7 +43,8 @@ func (a *App) RenameCollection(hostKey, dbKey, collKey, newCollKey string) bool } err = client.Database("admin").RunCommand(ctx, command).Decode(&result) if err != nil { - fmt.Println(err.Error()) + runtime.LogWarning(a.ctx, "Could not rename collection "+collKey) + runtime.LogWarning(a.ctx, err.Error()) runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{ Type: runtime.ErrorDialog, Title: "Could not rename " + collKey, @@ -71,13 +71,13 @@ func (a *App) TruncateCollection(hostKey, dbKey, collKey string) bool { client, ctx, close, err := a.connectToHost(hostKey) if err != nil { - fmt.Println(err.Error()) return false } _, err = client.Database(dbKey).Collection(collKey).DeleteMany(ctx, bson.D{}) if err != nil { - fmt.Println(err.Error()) + runtime.LogWarning(a.ctx, "Could not truncate collection "+collKey) + runtime.LogWarning(a.ctx, err.Error()) runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{ Type: runtime.ErrorDialog, Title: "Could not truncate " + collKey, @@ -104,13 +104,13 @@ func (a *App) DropCollection(hostKey, dbKey, collKey string) bool { client, ctx, close, err := a.connectToHost(hostKey) if err != nil { - fmt.Println(err.Error()) return false } err = client.Database(dbKey).Collection(collKey).Drop(ctx) if err != nil { - fmt.Println(err.Error()) + runtime.LogWarning(a.ctx, "Could not drop collection "+collKey) + runtime.LogWarning(a.ctx, err.Error()) runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{ Type: runtime.ErrorDialog, Title: "Could not drop " + collKey, diff --git a/internal/app/collection_find.go b/internal/app/collection_find.go index aceb00c..19c80c7 100644 --- a/internal/app/collection_find.go +++ b/internal/app/collection_find.go @@ -2,7 +2,6 @@ package app import ( "encoding/json" - "fmt" "github.com/wailsapp/wails/v2/pkg/runtime" @@ -27,7 +26,8 @@ func (a *App) FindItems(hostKey, dbKey, collKey string, formJson string) findRes err := json.Unmarshal([]byte(formJson), &form) if err != nil { - fmt.Println(err.Error()) + runtime.LogError(a.ctx, "Could not parse find form:") + runtime.LogError(a.ctx, err.Error()) runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{ Type: runtime.ErrorDialog, Title: "Couldn't parse form", @@ -38,7 +38,6 @@ func (a *App) FindItems(hostKey, dbKey, collKey string, formJson string) findRes client, ctx, close, err := a.connectToHost(hostKey) if err != nil { - fmt.Println(err.Error()) return out } @@ -49,7 +48,8 @@ func (a *App) FindItems(hostKey, dbKey, collKey string, formJson string) findRes err = bson.UnmarshalExtJSON([]byte(form.Query), true, &query) if err != nil { - fmt.Println(err.Error()) + runtime.LogInfo(a.ctx, "Invalid find query:") + runtime.LogInfo(a.ctx, err.Error()) runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{ Type: runtime.ErrorDialog, Title: "Invalid query", @@ -60,7 +60,8 @@ func (a *App) FindItems(hostKey, dbKey, collKey string, formJson string) findRes err = json.Unmarshal([]byte(form.Fields), &projection) if err != nil { - fmt.Println(err.Error()) + runtime.LogInfo(a.ctx, "Invalid find projection:") + runtime.LogInfo(a.ctx, err.Error()) runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{ Type: runtime.ErrorDialog, Title: "Invalid projection", @@ -71,7 +72,8 @@ func (a *App) FindItems(hostKey, dbKey, collKey string, formJson string) findRes err = json.Unmarshal([]byte(form.Sort), &sort) if err != nil { - fmt.Println(err.Error()) + runtime.LogInfo(a.ctx, "Invalid find sort:") + runtime.LogInfo(a.ctx, err.Error()) runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{ Type: runtime.ErrorDialog, Title: "Invalid sort", @@ -89,7 +91,8 @@ func (a *App) FindItems(hostKey, dbKey, collKey string, formJson string) findRes total, err := client.Database(dbKey).Collection(collKey).CountDocuments(ctx, query, nil) if err != nil { - fmt.Println(err.Error()) + runtime.LogWarning(a.ctx, "Encountered an error while counting documents:") + runtime.LogWarning(a.ctx, err.Error()) runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{ Type: runtime.ErrorDialog, Title: "Encountered an error while counting documents", @@ -100,7 +103,8 @@ func (a *App) FindItems(hostKey, dbKey, collKey string, formJson string) findRes cur, err := client.Database(dbKey).Collection(collKey).Find(ctx, query, &opt) if err != nil { - fmt.Println(err.Error()) + runtime.LogWarning(a.ctx, "Encountered an error while performing query:") + runtime.LogWarning(a.ctx, err.Error()) runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{ Type: runtime.ErrorDialog, Title: "Encountered an error while performing query", @@ -114,7 +118,8 @@ func (a *App) FindItems(hostKey, dbKey, collKey string, formJson string) findRes err = cur.All(ctx, &results) if err != nil { - fmt.Println(err.Error()) + runtime.LogWarning(a.ctx, "Encountered an error while performing query:") + runtime.LogWarning(a.ctx, err.Error()) runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{ Type: runtime.ErrorDialog, Title: "Encountered an error while performing query", @@ -128,7 +133,8 @@ func (a *App) FindItems(hostKey, dbKey, collKey string, formJson string) findRes for _, r := range results { marshalled, err := bson.MarshalExtJSON(r, true, true) if err != nil { - fmt.Println(err.Error()) + runtime.LogError(a.ctx, "Failed to marshal find BSON:") + runtime.LogError(a.ctx, err.Error()) runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{ Type: runtime.ErrorDialog, Title: "Failed to marshal BSON", diff --git a/internal/app/collection_indexes.go b/internal/app/collection_indexes.go index c5f5ef7..77ad3b0 100644 --- a/internal/app/collection_indexes.go +++ b/internal/app/collection_indexes.go @@ -2,7 +2,6 @@ package app import ( "encoding/json" - "fmt" "math" "github.com/wailsapp/wails/v2/pkg/runtime" @@ -14,14 +13,14 @@ import ( func (a *App) GetIndexes(hostKey, dbKey, collKey string) []bson.M { client, ctx, close, err := a.connectToHost(hostKey) if err != nil { - fmt.Println(err.Error()) return nil } defer close() cur, err := client.Database(dbKey).Collection(collKey).Indexes().List(ctx) if err != nil { - fmt.Println(err.Error()) + runtime.LogWarning(a.ctx, "Encountered an error while creating index cursor:") + runtime.LogWarning(a.ctx, err.Error()) runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{ Type: runtime.ErrorDialog, Title: "Encountered an error while creating index cursor", @@ -33,7 +32,8 @@ func (a *App) GetIndexes(hostKey, dbKey, collKey string) []bson.M { var results []bson.M err = cur.All(ctx, &results) if err != nil { - fmt.Println(err.Error()) + runtime.LogWarning(a.ctx, "Encountered an error while executing index cursor:") + runtime.LogWarning(a.ctx, err.Error()) runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{ Type: runtime.ErrorDialog, Title: "Encountered an error while executing index cursor", @@ -48,7 +48,6 @@ func (a *App) GetIndexes(hostKey, dbKey, collKey string) []bson.M { func (a *App) CreateIndex(hostKey, dbKey, collKey, jsonData string) string { client, ctx, close, err := a.connectToHost(hostKey) if err != nil { - fmt.Println(err.Error()) return "" } defer close() @@ -69,7 +68,8 @@ func (a *App) CreateIndex(hostKey, dbKey, collKey, jsonData string) string { err = json.Unmarshal([]byte(jsonData), &form) if err != nil { - fmt.Println(err.Error()) + runtime.LogError(a.ctx, "Could not parse index JSON:") + runtime.LogError(a.ctx, err.Error()) runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{ Type: runtime.ErrorDialog, Title: "Couldn't parse JSON", @@ -102,7 +102,8 @@ func (a *App) CreateIndex(hostKey, dbKey, collKey, jsonData string) string { name, err := client.Database(dbKey).Collection(collKey).Indexes().CreateOne(ctx, indexModel) if err != nil { - fmt.Println(err.Error()) + runtime.LogWarning(a.ctx, "Encountered an error while creating index:") + runtime.LogWarning(a.ctx, err.Error()) runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{ Type: runtime.ErrorDialog, Title: "Error while creating index", @@ -117,14 +118,14 @@ func (a *App) CreateIndex(hostKey, dbKey, collKey, jsonData string) string { func (a *App) DropIndex(hostKey, dbKey, collKey, indexName string) bool { client, ctx, close, err := a.connectToHost(hostKey) if err != nil { - fmt.Println(err.Error()) return false } defer close() _, err = client.Database(dbKey).Collection(collKey).Indexes().DropOne(ctx, indexName, &options.DropIndexesOptions{}) if err != nil { - fmt.Println(err.Error()) + runtime.LogError(a.ctx, "Encountered an error while creating index drop cursor:") + runtime.LogError(a.ctx, err.Error()) runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{ Type: runtime.ErrorDialog, Title: "Encountered an error while creating index cursor", diff --git a/internal/app/collection_insert.go b/internal/app/collection_insert.go index 72765e1..f66c06a 100644 --- a/internal/app/collection_insert.go +++ b/internal/app/collection_insert.go @@ -1,7 +1,6 @@ package app import ( - "fmt" "strings" "github.com/wailsapp/wails/v2/pkg/runtime" @@ -18,7 +17,8 @@ func (a *App) InsertItems(hostKey, dbKey, collKey, jsonData string) interface{} err := bson.UnmarshalExtJSON([]byte(jsonData), true, &data) if err != nil { - fmt.Println(err.Error()) + runtime.LogError(a.ctx, "Could not parse insert JSON:") + runtime.LogError(a.ctx, err.Error()) runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{ Type: runtime.ErrorDialog, Title: "Couldn't parse JSON", @@ -29,14 +29,14 @@ func (a *App) InsertItems(hostKey, dbKey, collKey, jsonData string) interface{} client, ctx, close, err := a.connectToHost(hostKey) if err != nil { - fmt.Println(err.Error()) return nil } defer close() res, err := client.Database(dbKey).Collection(collKey).InsertMany(ctx, data) if err != nil { - fmt.Println(err.Error()) + runtime.LogWarning(a.ctx, "Encountered an error while performing insert:") + runtime.LogWarning(a.ctx, err.Error()) runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{ Type: runtime.ErrorDialog, Title: "Encountered an error while performing query", diff --git a/internal/app/collection_remove.go b/internal/app/collection_remove.go index 56f36db..7715add 100644 --- a/internal/app/collection_remove.go +++ b/internal/app/collection_remove.go @@ -1,7 +1,6 @@ package app import ( - "fmt" "strings" "github.com/wailsapp/wails/v2/pkg/runtime" @@ -28,7 +27,8 @@ func (a *App) RemoveItems(hostKey, dbKey, collKey, jsonData string, many bool) i } else { err = bson.UnmarshalExtJSON([]byte(jsonData), true, &filter) if err != nil { - fmt.Println(err.Error()) + runtime.LogError(a.ctx, "Could not parse remove query:") + runtime.LogError(a.ctx, err.Error()) runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{ Type: runtime.ErrorDialog, Title: "Couldn't parse JSON", @@ -40,7 +40,6 @@ func (a *App) RemoveItems(hostKey, dbKey, collKey, jsonData string, many bool) i client, ctx, close, err := a.connectToHost(hostKey) if err != nil { - fmt.Println(err.Error()) return 0 } @@ -55,7 +54,8 @@ func (a *App) RemoveItems(hostKey, dbKey, collKey, jsonData string, many bool) i } if err != nil { - fmt.Println(err.Error()) + runtime.LogWarning(a.ctx, "Encountered an error while performing remove:") + runtime.LogWarning(a.ctx, err.Error()) runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{ Type: runtime.ErrorDialog, Title: "Encountered an error while removing items", @@ -70,7 +70,6 @@ func (a *App) RemoveItems(hostKey, dbKey, collKey, jsonData string, many bool) i func (a *App) RemoveItemById(hostKey, dbKey, collKey, itemId string) bool { client, ctx, close, err := a.connectToHost(hostKey) if err != nil { - fmt.Println(err.Error()) return false } @@ -80,7 +79,8 @@ func (a *App) RemoveItemById(hostKey, dbKey, collKey, itemId string) bool { err = client.Database(dbKey).Collection(collKey).FindOneAndDelete(ctx, filter).Err() if err != nil && err != mongo.ErrNoDocuments { - fmt.Println(err.Error()) + runtime.LogWarning(a.ctx, "Encountered an error while performing remove by id:") + runtime.LogWarning(a.ctx, err.Error()) runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{ Type: runtime.ErrorDialog, Title: "Encountered an error while removing item" + itemId, diff --git a/internal/app/collection_update.go b/internal/app/collection_update.go index 3e5a67d..2d167aa 100644 --- a/internal/app/collection_update.go +++ b/internal/app/collection_update.go @@ -2,7 +2,6 @@ package app import ( "encoding/json" - "fmt" "github.com/wailsapp/wails/v2/pkg/runtime" @@ -24,7 +23,8 @@ func (a *App) UpdateItems(hostKey, dbKey, collKey string, formJson string) int64 err := json.Unmarshal([]byte(formJson), &form) if err != nil { - fmt.Println(err.Error()) + runtime.LogError(a.ctx, "Could not parse update form:") + runtime.LogError(a.ctx, err.Error()) runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{ Type: runtime.ErrorDialog, Title: "Couldn't parse form", @@ -35,7 +35,6 @@ func (a *App) UpdateItems(hostKey, dbKey, collKey string, formJson string) int64 client, ctx, close, err := a.connectToHost(hostKey) if err != nil { - fmt.Println(err.Error()) return 0 } @@ -45,7 +44,8 @@ func (a *App) UpdateItems(hostKey, dbKey, collKey string, formJson string) int64 err = bson.UnmarshalExtJSON([]byte(form.Query), true, &query) if err != nil { - fmt.Println(err.Error()) + runtime.LogError(a.ctx, "Invalid update query:") + runtime.LogError(a.ctx, err.Error()) runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{ Type: runtime.ErrorDialog, Title: "Invalid query", @@ -60,7 +60,8 @@ func (a *App) UpdateItems(hostKey, dbKey, collKey string, formJson string) int64 if err == nil { update[param.Type] = unmarshalled } else { - fmt.Println(err.Error()) + runtime.LogError(a.ctx, "Invalid update query:") + runtime.LogError(a.ctx, err.Error()) runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{ Type: runtime.ErrorDialog, Title: "Invalid query", @@ -80,7 +81,8 @@ func (a *App) UpdateItems(hostKey, dbKey, collKey string, formJson string) int64 } if err != nil { - fmt.Println(err.Error()) + runtime.LogWarning(a.ctx, "Encountered an error while performing update:") + runtime.LogWarning(a.ctx, err.Error()) runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{ Type: runtime.ErrorDialog, Title: "Encountered an error while updating items", diff --git a/internal/app/connection.go b/internal/app/connection.go index d993fca..3a87a45 100644 --- a/internal/app/connection.go +++ b/internal/app/connection.go @@ -3,7 +3,6 @@ package app import ( "context" "errors" - "fmt" "time" "github.com/wailsapp/wails/v2/pkg/runtime" @@ -24,6 +23,7 @@ func (a *App) connectToHost(hostKey string) (*mongo.Client, context.Context, fun h := hosts[hostKey] if len(h.URI) == 0 { + runtime.LogInfo(a.ctx, "Invalid URI (len == 0) for host "+hostKey) runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{ Type: runtime.InfoDialog, Title: "Invalid uri", @@ -35,7 +35,8 @@ func (a *App) connectToHost(hostKey string) (*mongo.Client, context.Context, fun client, err := mongo.NewClient(mongoOptions.Client().ApplyURI(h.URI)) if err != nil { - fmt.Println(err.Error()) + runtime.LogWarning(a.ctx, "Could not connect to host "+hostKey) + runtime.LogWarning(a.ctx, err.Error()) runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{ Type: runtime.ErrorDialog, Title: "Could not connect to " + h.Name, @@ -55,12 +56,12 @@ func (a *App) connectToHost(hostKey string) (*mongo.Client, context.Context, fun func (a *App) OpenConnection(hostKey string) (databases []string) { client, ctx, close, err := a.connectToHost(hostKey) if err != nil { - fmt.Println(err.Error()) return nil } databases, err = client.ListDatabaseNames(ctx, bson.M{}) if err != nil { - fmt.Println(err.Error()) + runtime.LogWarning(a.ctx, "Could not retrieve database names for host "+hostKey) + runtime.LogWarning(a.ctx, err.Error()) runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{ Type: runtime.ErrorDialog, Title: "Could not retrieve database list", diff --git a/internal/app/database.go b/internal/app/database.go index 1267956..a23ae05 100644 --- a/internal/app/database.go +++ b/internal/app/database.go @@ -1,8 +1,6 @@ package app import ( - "fmt" - "github.com/wailsapp/wails/v2/pkg/runtime" "go.mongodb.org/mongo-driver/bson" ) @@ -10,13 +8,13 @@ import ( func (a *App) OpenDatabase(hostKey, dbKey string) (collections []string) { client, ctx, close, err := a.connectToHost(hostKey) if err != nil { - fmt.Println(err.Error()) return nil } collections, err = client.Database(dbKey).ListCollectionNames(ctx, bson.D{}) if err != nil { - fmt.Println(err.Error()) + runtime.LogWarning(a.ctx, "Could not retrieve collection list for db "+dbKey) + runtime.LogWarning(a.ctx, err.Error()) runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{ Type: runtime.ErrorDialog, Title: "Could not retrieve collection list for " + dbKey, @@ -43,13 +41,13 @@ func (a *App) DropDatabase(hostKey, dbKey string) bool { client, ctx, close, err := a.connectToHost(hostKey) if err != nil { - fmt.Println(err.Error()) return false } err = client.Database(dbKey).Drop(ctx) if err != nil { - fmt.Println(err.Error()) + runtime.LogWarning(a.ctx, "Could not drop db "+dbKey) + runtime.LogWarning(a.ctx, err.Error()) runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{ Type: runtime.ErrorDialog, Title: "Could not drop " + dbKey, diff --git a/internal/app/database_export.go b/internal/app/database_export.go index 8e71d9d..f229949 100644 --- a/internal/app/database_export.go +++ b/internal/app/database_export.go @@ -35,7 +35,8 @@ func (a *App) PerformExport(jsonData string) bool { var info ExportInfo err := json.Unmarshal([]byte(jsonData), &info) if err != nil { - fmt.Println(err.Error()) + runtime.LogError(a.ctx, "Could not unmarshal export form") + runtime.LogError(a.ctx, err.Error()) runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{ Type: runtime.ErrorDialog, Title: "Could not unmarshal JSON", @@ -46,7 +47,6 @@ func (a *App) PerformExport(jsonData string) bool { hosts, err := a.Hosts() if err != nil { - fmt.Println(err.Error()) runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{ Type: runtime.ErrorDialog, Title: "Could not retrieve hosts", @@ -86,6 +86,8 @@ func (a *App) PerformExport(jsonData string) bool { cmd.Stderr = &stderr err = cmd.Run() + runtime.LogInfo(a.ctx, "Performing export with args: "+strings.Join(args, " ")) + fmt.Println(args) fmt.Println(stdout.String()) fmt.Println(stderr.String()) diff --git a/internal/app/hosts.go b/internal/app/hosts.go index a8f9a04..0d99a88 100644 --- a/internal/app/hosts.go +++ b/internal/app/hosts.go @@ -3,7 +3,6 @@ package app import ( "encoding/json" "errors" - "fmt" "io/ioutil" "os" "path" @@ -34,7 +33,8 @@ func (a *App) Hosts() (map[string]Host, error) { if err != nil { // It's ok if the file cannot be opened, for example if it is not accessible. // Therefore no error is returned. - fmt.Println(err.Error()) + runtime.LogInfo(a.ctx, "Could not open hosts.json") + runtime.LogInfo(a.ctx, err.Error()) return make(map[string]Host, 0), nil } @@ -45,7 +45,8 @@ func (a *App) Hosts() (map[string]Host, error) { err = json.Unmarshal(jsonData, &hosts) if err != nil { - fmt.Println(err.Error()) + runtime.LogInfo(a.ctx, "host.json file contains malformatted JSON data") + runtime.LogInfo(a.ctx, err.Error()) return nil, errors.New("host.json file contains malformatted JSON data") } return hosts, nil @@ -65,6 +66,8 @@ func (a *App) AddHost(jsonData string) error { var newHost Host err = json.Unmarshal([]byte(jsonData), &newHost) if err != nil { + runtime.LogError(a.ctx, "Add host: malformed form") + runtime.LogError(a.ctx, err.Error()) runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{ Type: runtime.InfoDialog, Title: "Malformed JSON", @@ -74,6 +77,8 @@ func (a *App) AddHost(jsonData string) error { id, err := uuid.NewRandom() if err != nil { + runtime.LogError(a.ctx, "Add host: failed to generate a UUID") + runtime.LogError(a.ctx, err.Error()) runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{ Type: runtime.InfoDialog, Title: "Failed to generate a UUID", @@ -107,6 +112,8 @@ func (a *App) UpdateHost(hostKey string, jsonData string) error { var host Host err = json.Unmarshal([]byte(jsonData), &host) if err != nil { + runtime.LogError(a.ctx, "Could not parse update host JSON") + runtime.LogError(a.ctx, err.Error()) runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{ Type: runtime.InfoDialog, Title: "Malformed JSON", diff --git a/internal/app/views.go b/internal/app/views.go index 0c1d7c5..ff1e0cd 100644 --- a/internal/app/views.go +++ b/internal/app/views.go @@ -3,7 +3,6 @@ package app import ( "encoding/json" "errors" - "fmt" "io/ioutil" "os" "path" @@ -75,14 +74,16 @@ func (a *App) Views() (ViewStore, error) { if err != nil { // It's ok if the file cannot be opened, for example if it is not accessible. // Therefore no error is returned. - fmt.Println(err.Error()) + runtime.LogInfo(a.ctx, "views.json file cannot be opened") + runtime.LogInfo(a.ctx, err.Error()) return views, nil } if len(jsonData) > 0 { err = json.Unmarshal(jsonData, &views) if err != nil { - fmt.Println(err.Error()) + runtime.LogInfo(a.ctx, "views.json file contains malformatted JSON data") + runtime.LogInfo(a.ctx, err.Error()) return nil, errors.New("views.json file contains malformatted JSON data") } } diff --git a/internal/utils/open_file/open.go b/internal/utils/open_file/open.go new file mode 100644 index 0000000..156ed68 --- /dev/null +++ b/internal/utils/open_file/open.go @@ -0,0 +1,6 @@ +package open_file + +// Reveal reveals the specified file in the Finder. +func Reveal(fname string) { + reveal(fname) +} diff --git a/internal/utils/open_file/open_darwin.go b/internal/utils/open_file/open_darwin.go new file mode 100644 index 0000000..45e91d7 --- /dev/null +++ b/internal/utils/open_file/open_darwin.go @@ -0,0 +1,9 @@ +//go:build darwin + +package open_file + +import "os/exec" + +func reveal(fname string) { + exec.Command("open", "--reveal", fname).Run() +} diff --git a/internal/utils/open_file/open_windows.go b/internal/utils/open_file/open_windows.go new file mode 100644 index 0000000..02c53ce --- /dev/null +++ b/internal/utils/open_file/open_windows.go @@ -0,0 +1,9 @@ +//go:build windows + +package open_file + +import "os/exec" + +func reveal(fname string) { + exec.Command("explorer", fname).Run() +} diff --git a/main.go b/main.go index 195e1ce..84dc3ce 100644 --- a/main.go +++ b/main.go @@ -2,9 +2,11 @@ package main import ( "embed" + "path" "github.com/garraflavatra/rolens/internal/app" "github.com/wailsapp/wails/v2" + "github.com/wailsapp/wails/v2/pkg/logger" "github.com/wailsapp/wails/v2/pkg/options" "github.com/wailsapp/wails/v2/pkg/options/assetserver" "github.com/wailsapp/wails/v2/pkg/options/mac" @@ -20,17 +22,23 @@ var ( func main() { app := app.NewApp() - err := wails.Run(&options.App{ - Title: "Rolens", - Width: 1000, - Height: 600, - MinWidth: 1000, - MinHeight: 600, + Title: "Rolens", + + Width: 1000, + Height: 600, + MinWidth: 1000, + MinHeight: 600, + BackgroundColour: &options.RGBA{R: 0, G: 0, B: 139, A: 1}, - OnStartup: app.Startup, Menu: app.Menu(), + OnStartup: app.Startup, + OnShutdown: app.Shutdown, + Logger: logger.NewFileLogger(path.Join(app.Env.LogDirectory, "rolens.log")), + LogLevel: logger.TRACE, + LogLevelProduction: logger.INFO, + AssetServer: &assetserver.Options{ Assets: assets, },