2023-01-14 20:47:29 +01:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/wailsapp/wails/v2/pkg/runtime"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
|
|
)
|
|
|
|
|
2023-06-23 17:22:47 +02:00
|
|
|
type OpenDatabaseResult struct {
|
2023-06-07 21:30:22 +02:00
|
|
|
Collections []string `json:"collections"`
|
|
|
|
Stats bson.M `json:"stats"`
|
2023-06-23 17:22:47 +02:00
|
|
|
StatsError string `json:"statsError"`
|
2023-06-07 21:30:22 +02:00
|
|
|
}
|
|
|
|
|
2023-06-23 17:22:47 +02:00
|
|
|
func (a *App) OpenDatabase(hostKey, dbKey string) (result OpenDatabaseResult) {
|
2023-01-14 20:47:29 +01:00
|
|
|
client, ctx, close, err := a.connectToHost(hostKey)
|
|
|
|
if err != nil {
|
2023-06-07 21:30:22 +02:00
|
|
|
return
|
|
|
|
}
|
2023-06-23 17:22:47 +02:00
|
|
|
defer close()
|
2023-06-07 21:30:22 +02:00
|
|
|
|
|
|
|
command := bson.M{"dbStats": 1}
|
2023-06-23 17:22:47 +02:00
|
|
|
err = client.Database(dbKey).RunCommand(ctx, command).Decode(&result.Stats)
|
2023-06-07 21:30:22 +02:00
|
|
|
if err != nil {
|
2023-06-24 20:27:48 +02:00
|
|
|
runtime.LogWarningf(a.ctx, "Could not retrieve database stats for %s: %s", dbKey, err.Error())
|
2023-06-23 17:22:47 +02:00
|
|
|
result.StatsError = err.Error()
|
2023-01-14 20:47:29 +01:00
|
|
|
}
|
2023-01-22 21:12:56 +01:00
|
|
|
|
2023-06-23 17:22:47 +02:00
|
|
|
result.Collections, err = client.Database(dbKey).ListCollectionNames(ctx, bson.D{})
|
2023-01-14 20:47:29 +01:00
|
|
|
if err != nil {
|
2023-06-24 20:27:48 +02:00
|
|
|
runtime.LogWarningf(a.ctx, "Could not retrieve collection list for db %s: %s", dbKey, err.Error())
|
|
|
|
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
|
|
|
|
Title: "Error getting collection list",
|
|
|
|
Message: err.Error(),
|
|
|
|
Type: runtime.ErrorDialog,
|
|
|
|
})
|
2023-01-14 20:47:29 +01:00
|
|
|
}
|
2023-01-22 21:12:56 +01:00
|
|
|
|
2023-06-07 21:30:22 +02:00
|
|
|
return
|
2023-01-14 20:47:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) DropDatabase(hostKey, dbKey string) bool {
|
2023-06-24 20:27:48 +02:00
|
|
|
choice, _ := runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
|
|
|
|
Title: "Confirm",
|
|
|
|
Message: "Are you sure you want to drop " + dbKey + "?",
|
|
|
|
Buttons: []string{"Yes", "Cancel"},
|
|
|
|
DefaultButton: "Yes",
|
|
|
|
CancelButton: "Cancel",
|
|
|
|
})
|
|
|
|
if choice != "Yes" {
|
2023-01-14 20:47:29 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
client, ctx, close, err := a.connectToHost(hostKey)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
2023-06-23 17:22:47 +02:00
|
|
|
defer close()
|
2023-01-22 21:12:56 +01:00
|
|
|
|
2023-01-14 20:47:29 +01:00
|
|
|
err = client.Database(dbKey).Drop(ctx)
|
|
|
|
if err != nil {
|
2023-06-24 20:27:48 +02:00
|
|
|
runtime.LogWarningf(a.ctx, "Could not drop db %s: %s", dbKey, err.Error())
|
|
|
|
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
|
|
|
|
Title: "Error dropping database",
|
|
|
|
Message: err.Error(),
|
|
|
|
Type: runtime.ErrorDialog,
|
|
|
|
})
|
2023-01-14 20:47:29 +01:00
|
|
|
return false
|
|
|
|
}
|
2023-01-22 21:12:56 +01:00
|
|
|
|
2023-01-14 20:47:29 +01:00
|
|
|
return true
|
|
|
|
}
|