1
0
mirror of https://github.com/garraflavatra/rolens.git synced 2025-01-18 21:17:59 +00:00
rolens/internal/app/database.go

63 lines
1.7 KiB
Go
Raw Normal View History

2023-01-14 19:47:29 +00:00
package app
import (
2023-02-20 20:04:01 +00:00
"github.com/ncruces/zenity"
2023-01-14 19:47:29 +00:00
"github.com/wailsapp/wails/v2/pkg/runtime"
"go.mongodb.org/mongo-driver/bson"
)
type DatabaseInfo struct {
Collections []string `json:"collections"`
Stats bson.M `json:"stats"`
}
func (a *App) OpenDatabase(hostKey, dbKey string) (info DatabaseInfo) {
2023-01-14 19:47:29 +00:00
client, ctx, close, err := a.connectToHost(hostKey)
if err != nil {
return
}
command := bson.M{"dbStats": 1}
err = client.Database(dbKey).RunCommand(ctx, command).Decode(&info.Stats)
if err != nil {
runtime.LogWarning(a.ctx, "Could not retrieve database stats for "+dbKey)
runtime.LogWarning(a.ctx, err.Error())
zenity.Error(err.Error(), zenity.Title("Could not get stats"), zenity.ErrorIcon)
return
2023-01-14 19:47:29 +00:00
}
2023-01-22 20:12:56 +00:00
info.Collections, err = client.Database(dbKey).ListCollectionNames(ctx, bson.D{})
2023-01-14 19:47:29 +00:00
if err != nil {
2023-02-11 20:57:52 +00:00
runtime.LogWarning(a.ctx, "Could not retrieve collection list for db "+dbKey)
runtime.LogWarning(a.ctx, err.Error())
2023-02-21 16:27:44 +00:00
zenity.Error(err.Error(), zenity.Title("Error while getting collections"), zenity.ErrorIcon)
return
2023-01-14 19:47:29 +00:00
}
2023-01-22 20:12:56 +00:00
2023-01-14 19:47:29 +00:00
defer close()
return
2023-01-14 19:47:29 +00:00
}
func (a *App) DropDatabase(hostKey, dbKey string) bool {
2023-02-20 20:04:01 +00:00
err := zenity.Question("Are you sure you want to drop "+dbKey+"?", zenity.Title("Confirm"), zenity.WarningIcon)
if err == zenity.ErrCanceled {
2023-01-14 19:47:29 +00:00
return false
}
client, ctx, close, err := a.connectToHost(hostKey)
if err != nil {
return false
}
2023-01-22 20:12:56 +00:00
2023-01-14 19:47:29 +00:00
err = client.Database(dbKey).Drop(ctx)
if err != nil {
2023-02-11 20:57:52 +00:00
runtime.LogWarning(a.ctx, "Could not drop db "+dbKey)
runtime.LogWarning(a.ctx, err.Error())
2023-02-21 16:27:44 +00:00
zenity.Error(err.Error(), zenity.Title("Error while dropping database"), zenity.ErrorIcon)
2023-01-14 19:47:29 +00:00
return false
}
2023-01-22 20:12:56 +00:00
2023-01-14 19:47:29 +00:00
defer close()
return true
}