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

98 lines
2.7 KiB
Go
Raw Normal View History

2023-01-14 19:47:29 +00:00
package app
import (
"fmt"
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) OpenCollection(hostKey, dbKey, collKey string) (result bson.M) {
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
command := bson.M{"collStats": collKey}
err = client.Database(dbKey).RunCommand(ctx, command).Decode(&result)
if err != nil {
2023-02-11 20:57:52 +00:00
runtime.LogWarning(a.ctx, "Could not retrieve collection stats for "+collKey)
runtime.LogWarning(a.ctx, err.Error())
2023-02-20 20:04:01 +00:00
zenity.Info(err.Error(), zenity.Title("Could not get stats"), 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 result
}
2023-01-14 20:09:21 +00:00
2023-01-22 20:12:56 +00:00
func (a *App) RenameCollection(hostKey, dbKey, collKey, newCollKey string) bool {
client, ctx, close, err := a.connectToHost(hostKey)
if err != nil {
return false
}
var result bson.M
command := bson.D{
bson.E{Key: "renameCollection", Value: fmt.Sprintf("%v.%v", dbKey, collKey)},
bson.E{Key: "to", Value: fmt.Sprintf("%v.%v", dbKey, newCollKey)},
}
err = client.Database("admin").RunCommand(ctx, command).Decode(&result)
if err != nil {
2023-02-11 20:57:52 +00:00
runtime.LogWarning(a.ctx, "Could not rename collection "+collKey)
runtime.LogWarning(a.ctx, err.Error())
2023-02-20 20:04:01 +00:00
zenity.Info(err.Error(), zenity.Title("Error while renaming collection"), zenity.ErrorIcon)
2023-01-22 20:12:56 +00:00
return false
}
defer close()
return true
}
2023-01-23 12:35:31 +00:00
func (a *App) TruncateCollection(hostKey, dbKey, collKey string) bool {
2023-02-20 20:04:01 +00:00
err := zenity.Question("Are you sure you want to remove all items from "+collKey+"?", zenity.Title("Confirm"), zenity.WarningIcon)
if err == zenity.ErrCanceled {
2023-01-23 12:35:31 +00:00
return false
}
client, ctx, close, err := a.connectToHost(hostKey)
if err != nil {
return false
}
_, err = client.Database(dbKey).Collection(collKey).DeleteMany(ctx, bson.D{})
if err != nil {
2023-02-11 20:57:52 +00:00
runtime.LogWarning(a.ctx, "Could not truncate collection "+collKey)
runtime.LogWarning(a.ctx, err.Error())
2023-02-20 20:04:01 +00:00
zenity.Info(err.Error(), zenity.Title("Error while truncating collection"), zenity.ErrorIcon)
2023-01-23 12:35:31 +00:00
return false
}
defer close()
return true
}
2023-01-14 20:09:21 +00:00
func (a *App) DropCollection(hostKey, dbKey, collKey string) bool {
2023-02-20 20:04:01 +00:00
err := zenity.Question("Are you sure you want to drop "+collKey+"?", zenity.Title("Confirm"), zenity.WarningIcon)
if err == zenity.ErrCanceled {
2023-01-14 20:09:21 +00:00
return false
}
client, ctx, close, err := a.connectToHost(hostKey)
if err != nil {
return false
}
2023-01-23 12:35:31 +00:00
2023-01-14 20:09:21 +00:00
err = client.Database(dbKey).Collection(collKey).Drop(ctx)
if err != nil {
2023-02-11 20:57:52 +00:00
runtime.LogWarning(a.ctx, "Could not drop collection "+collKey)
runtime.LogWarning(a.ctx, err.Error())
2023-02-20 20:04:01 +00:00
zenity.Info(err.Error(), zenity.Title("Error while dropping collection"), zenity.ErrorIcon)
2023-01-14 20:09:21 +00:00
return false
}
2023-01-23 12:35:31 +00:00
2023-01-14 20:09:21 +00:00
defer close()
return true
}