1
0
mirror of https://github.com/garraflavatra/rolens.git synced 2025-07-19 22:18:03 +00:00

Layout changes. Edit collection names.

This commit is contained in:
2023-01-22 21:12:56 +01:00
parent e83a02fae8
commit 4d50d43f4d
19 changed files with 265 additions and 373 deletions

View File

@ -36,10 +36,10 @@ func (a *App) Menu() *menu.Menu {
wailsRuntime.Quit(a.ctx)
})
fileMenu := appMenu.AddSubmenu("File")
fileMenu.AddText("Hosts…", keys.CmdOrCtrl("k"), func(cd *menu.CallbackData) {
wailsRuntime.EventsEmit(a.ctx, "OpenHostsModal")
})
// fileMenu := appMenu.AddSubmenu("File")
// fileMenu.AddText("Hosts…", keys.CmdOrCtrl("k"), func(cd *menu.CallbackData) {
// wailsRuntime.EventsEmit(a.ctx, "OpenHostsModal")
// })
if runtime.GOOS == "darwin" {
appMenu.Append(menu.EditMenu())

View File

@ -13,21 +13,50 @@ func (a *App) OpenCollection(hostKey, dbKey, collKey string) (result bson.M) {
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.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: runtime.ErrorDialog,
Title: "Could not retrieve collection list for " + dbKey,
Title: "Could not retrieve collection stats for " + collKey,
Message: err.Error(),
})
return nil
}
defer close()
return result
}
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
}
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 {
fmt.Println(err.Error())
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: runtime.ErrorDialog,
Title: "Could not rename " + collKey,
Message: err.Error(),
})
return false
}
defer close()
return true
}
func (a *App) DropCollection(hostKey, dbKey, collKey string) bool {
sure, _ := runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Title: "Confirm",
@ -50,7 +79,7 @@ func (a *App) DropCollection(hostKey, dbKey, collKey string) bool {
fmt.Println(err.Error())
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: runtime.ErrorDialog,
Title: "Could not drop " + dbKey,
Title: "Could not drop " + collKey,
Message: err.Error(),
})
return false

View File

@ -1,12 +1,57 @@
package app
import (
"context"
"errors"
"fmt"
"time"
"github.com/wailsapp/wails/v2/pkg/runtime"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
mongoOptions "go.mongodb.org/mongo-driver/mongo/options"
)
func (a *App) connectToHost(hostKey string) (*mongo.Client, context.Context, func(), error) {
hosts, err := a.Hosts()
if err != nil {
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: runtime.InfoDialog,
Title: "Could not retrieve hosts",
})
return nil, nil, nil, errors.New("could not retrieve hosts")
}
h := hosts[hostKey]
if len(h.URI) == 0 {
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: runtime.InfoDialog,
Title: "Invalid uri",
Message: "You haven't specified a valid uri for the selected host.",
})
return nil, nil, nil, errors.New("invalid uri")
}
client, err := mongo.NewClient(mongoOptions.Client().ApplyURI(h.URI))
if err != nil {
fmt.Println(err.Error())
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: runtime.ErrorDialog,
Title: "Could not connect to " + h.Name,
Message: err.Error(),
})
return nil, nil, nil, errors.New("could not establish a connection with " + h.Name)
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
client.Connect(ctx)
return client, ctx, func() {
client.Disconnect(ctx)
cancel()
}, nil
}
func (a *App) OpenConnection(hostKey string) (databases []string) {
client, ctx, close, err := a.connectToHost(hostKey)
if err != nil {

View File

@ -13,6 +13,7 @@ func (a *App) OpenDatabase(hostKey, dbKey string) (collections []string) {
fmt.Println(err.Error())
return nil
}
collections, err = client.Database(dbKey).ListCollectionNames(ctx, bson.D{})
if err != nil {
fmt.Println(err.Error())
@ -23,6 +24,7 @@ func (a *App) OpenDatabase(hostKey, dbKey string) (collections []string) {
})
return nil
}
defer close()
return collections
}
@ -44,6 +46,7 @@ func (a *App) DropDatabase(hostKey, dbKey string) bool {
fmt.Println(err.Error())
return false
}
err = client.Database(dbKey).Drop(ctx)
if err != nil {
fmt.Println(err.Error())
@ -54,6 +57,7 @@ func (a *App) DropDatabase(hostKey, dbKey string) bool {
})
return false
}
defer close()
return true
}

View File

@ -1,18 +1,14 @@
package app
import (
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"time"
"github.com/google/uuid"
"github.com/wailsapp/wails/v2/pkg/runtime"
"go.mongodb.org/mongo-driver/mongo"
mongoOptions "go.mongodb.org/mongo-driver/mongo/options"
)
type Host struct {
@ -183,43 +179,3 @@ func (a *App) RemoveHost(key string) error {
}
return nil
}
func (a *App) connectToHost(hostKey string) (*mongo.Client, context.Context, func(), error) {
hosts, err := a.Hosts()
if err != nil {
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: runtime.InfoDialog,
Title: "Could not retrieve hosts",
})
return nil, nil, nil, errors.New("could not retrieve hosts")
}
h := hosts[hostKey]
if len(h.URI) == 0 {
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: runtime.InfoDialog,
Title: "Invalid uri",
Message: "You haven't specified a valid uri for the selected host.",
})
return nil, nil, nil, errors.New("invalid uri")
}
client, err := mongo.NewClient(mongoOptions.Client().ApplyURI(h.URI))
if err != nil {
fmt.Println(err.Error())
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: runtime.ErrorDialog,
Title: "Could not connect to " + h.Name,
Message: err.Error(),
})
return nil, nil, nil, errors.New("could not establish a connection with " + h.Name)
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
client.Connect(ctx)
return client, ctx, func() {
client.Disconnect(ctx)
cancel()
}, nil
}