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

49 lines
1.2 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"
)
func (a *App) OpenDatabase(hostKey, dbKey string) (collections []string) {
client, ctx, close, err := a.connectToHost(hostKey)
if err != nil {
return nil
}
2023-01-22 20:12:56 +00:00
2023-01-14 19:47:29 +00:00
collections, err = client.Database(dbKey).ListCollectionNames(ctx, bson.D{})
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)
2023-01-14 19:47:29 +00:00
return nil
}
2023-01-22 20:12:56 +00:00
2023-01-14 19:47:29 +00:00
defer close()
return collections
}
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
}