1
0
mirror of https://github.com/garraflavatra/rolens.git synced 2025-01-18 13:07:58 +00:00

Export/dump... wip

This commit is contained in:
Romein van Buren 2023-02-11 11:22:02 +01:00
parent 2826ae4008
commit d4ce9900c8
Signed by: romein
GPG Key ID: 0EFF8478ADDF6C49
14 changed files with 323 additions and 141 deletions

View File

@ -0,0 +1,19 @@
<script>
import { OpenDirectory } from '../../wailsjs/go/app/App';
export let value = '';
export let id = '';
export let title = 'Choose a directory';
async function selectDir() {
value = await OpenDirectory(title) || value;
}
</script>
<input type="text" on:pointerdown={selectDir} readonly {id} {value} />
<style>
input {
cursor: pointer;
}
</style>

View File

@ -1,12 +1,30 @@
<script>
import { busy, connections } from '../../../stores';
import { applicationSettings, busy, connections } from '../../../stores';
import Grid from '../../../components/grid.svelte';
import Modal from '../../../components/modal.svelte';
import { OpenConnection, OpenDatabase } from '../../../../wailsjs/go/app/App';
import { OpenConnection, OpenDatabase, PerformExport } from '../../../../wailsjs/go/app/App';
import DirectoryChooser from '../../../components/directorychooser.svelte';
export let info;
export let hosts = {};
const actionLabel = {
export: 'Perform export',
dump: 'Perform dump',
};
$: if (info) {
info.outdir = info.outdir || $applicationSettings.defaultExportDirectory;
info.filename = info.filename || `Export ${new Date().getTime()}`;
if (info.filetype === 'bson') {
info.type = 'dump';
}
else {
info.type = 'export';
}
}
async function selectHost(hostKey) {
info.hostKey = hostKey;
info.dbKey = undefined;
@ -43,17 +61,35 @@
}
}
async function performExport() {
await PerformExport(JSON.stringify(info));
}
function selectCollection(collKey) {
info.collKeys = [ collKey ];
}
</script>
<Modal bind:show={info} title="Dump database data">
<div class="info">
<Modal bind:show={info} title={actionLabel[info?.type]}>
<form on:submit|preventDefault={performExport}>
<div class="meta">
<!-- svelte-ignore a11y-label-has-associated-control - input is in DirectoryChooser -->
<label class="field">
<span class="label">Output filename</span>
<input type="text">
<span class="label">Output directory</span>
<DirectoryChooser bind:value={info.outdir} />
</label>
<label class="field">
<span class="label">Filename</span>
<input type="text" bind:value={info.filename} />
<select bind:value={info.filetype} class="filetype">
<optgroup label="Dump (mongodump)">
<option value="bson">.bson</option>
</optgroup>
<optgroup label="Export (mongoexport)">
<option value="csv">.csv</option>
<option value="json">.json</option>
</optgroup>
</select>
</label>
</div>
<div class="location">
@ -106,11 +142,15 @@
/>
</div>
</div>
</div>
<div>
<button type="submit" class="btn">{actionLabel[info.type]}</button>
</div>
</form>
</Modal>
<style>
.info {
form {
display: grid;
grid-template: auto / 1fr;
}
@ -124,4 +164,15 @@
padding: 0.3rem;
overflow-y: auto;
}
.meta {
display: grid;
grid-template: 1fr / 1fr 1fr;
gap: 0.5rem;
margin-bottom: 0.5rem;
}
select.filetype {
flex: 0 1;
}
</style>

View File

@ -1,3 +0,0 @@
<script>
export let info;
</script>

View File

@ -9,8 +9,7 @@
import HostDetail from './hostdetail.svelte';
import Icon from '../../components/icon.svelte';
import { EventsOn } from '../../../wailsjs/runtime/runtime';
import ExportInfo from './export/exportinfo.svelte';
import DumpInfo from './export/dumpinfo.svelte';
import Export from './export/export.svelte';
import Hint from '../../components/hint.svelte';
export let hosts = {};
@ -29,7 +28,6 @@
let newCollKey = '';
let exportInfo;
let dumpInfo;
async function getHosts() {
hosts = await Hosts();
@ -74,6 +72,8 @@
function exportCollection(collKey) {
exportInfo = {
type: 'export',
filetype: 'json',
hostKey: activeHostKey,
dbKey: activeDbKey,
collKeys: [ collKey ],
@ -81,7 +81,9 @@
}
function dumpCollection(collKey) {
dumpInfo = {
exportInfo = {
type: 'dump',
filetype: 'bson',
hostKey: activeHostKey,
dbKey: activeDbKey,
collKeys: [ collKey ],
@ -125,8 +127,7 @@
{hosts}
/>
<ExportInfo bind:info={exportInfo} {hosts} />
<DumpInfo bind:info={dumpInfo} {hosts} />
<Export bind:info={exportInfo} {hosts} />
{#if newDb}
<Modal bind:show={newDb}>

View File

@ -1,4 +1,5 @@
<script>
import DirectoryChooser from '../../components/directorychooser.svelte';
import { input } from '../../actions';
import Modal from '../../components/modal.svelte';
import { applicationSettings as settings } from '../../stores';
@ -24,6 +25,12 @@
<input type="checkbox" id="autosubmitQuery" bind:checked={$settings.autosubmitQuery} />
<label for="autosubmitQuery">Query items automatically after opening a collection</label>
</span>
<label for="defaultExportDirectory">Default export directory</label>
<!-- svelte-ignore a11y-label-has-associated-control - input is in DirectoryChooser -->
<label class="field">
<DirectoryChooser id="defaultExportDirectory" bind:value={$settings.defaultExportDirectory} />
</label>
</div>
</Modal>

View File

@ -33,6 +33,10 @@ export function OpenConnection(arg1:string):Promise<Array<string>>;
export function OpenDatabase(arg1:string,arg2:string):Promise<Array<string>>;
export function OpenDirectory(arg1:string,arg2:string):Promise<string>;
export function PerformExport(arg1:string):Promise<boolean>;
export function RemoveHost(arg1:string):Promise<void>;
export function RemoveItemById(arg1:string,arg2:string,arg3:string,arg4:string):Promise<boolean>;

View File

@ -58,6 +58,14 @@ export function OpenDatabase(arg1, arg2) {
return window['go']['app']['App']['OpenDatabase'](arg1, arg2);
}
export function OpenDirectory(arg1, arg2) {
return window['go']['app']['App']['OpenDirectory'](arg1, arg2);
}
export function PerformExport(arg1) {
return window['go']['app']['App']['PerformExport'](arg1);
}
export function RemoveHost(arg1) {
return window['go']['app']['App']['RemoveHost'](arg1);
}

View File

@ -6,6 +6,8 @@ export namespace app {
platform: string;
hasMongoExport: boolean;
hasMongoDump: boolean;
homeDirectory: string;
dataDirectory: string;
static createFrom(source: any = {}) {
return new EnvironmentInfo(source);
@ -18,12 +20,15 @@ export namespace app {
this.platform = source["platform"];
this.hasMongoExport = source["hasMongoExport"];
this.hasMongoDump = source["hasMongoDump"];
this.homeDirectory = source["homeDirectory"];
this.dataDirectory = source["dataDirectory"];
}
}
export class Settings {
defaultLimit: number;
defaultSort: string;
autosubmitQuery: boolean;
defaultExportDirectory: string;
static createFrom(source: any = {}) {
return new Settings(source);
@ -34,6 +39,7 @@ export namespace app {
this.defaultLimit = source["defaultLimit"];
this.defaultSort = source["defaultSort"];
this.autosubmitQuery = source["autosubmitQuery"];
this.defaultExportDirectory = source["defaultExportDirectory"];
}
}
export class findResult {

View File

@ -2,6 +2,11 @@ package app
import (
"context"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"github.com/wailsapp/wails/v2/pkg/menu"
@ -9,8 +14,21 @@ import (
wailsRuntime "github.com/wailsapp/wails/v2/pkg/runtime"
)
type EnvironmentInfo struct {
Arch string `json:"arch"`
BuildType string `json:"buildType"`
Platform string `json:"platform"`
HasMongoExport bool `json:"hasMongoExport"`
HasMongoDump bool `json:"hasMongoDump"`
HomeDirectory string `json:"homeDirectory"`
DataDirectory string `json:"dataDirectory"`
}
type App struct {
ctx context.Context
Env EnvironmentInfo
}
func NewApp() *App {
@ -19,6 +37,39 @@ func NewApp() *App {
func (a *App) Startup(ctx context.Context) {
a.ctx = ctx
wailsEnv := wailsRuntime.Environment(a.ctx)
a.Env.Arch = wailsEnv.Arch
a.Env.BuildType = wailsEnv.BuildType
a.Env.Platform = wailsEnv.Platform
_, err := exec.LookPath("mongodump")
a.Env.HasMongoDump = err == nil
_, err = exec.LookPath("mongoexport")
a.Env.HasMongoExport = err == nil
a.Env.HomeDirectory, err = os.UserHomeDir()
if err != nil {
panic(errors.New("encountered an error while getting home directory"))
}
switch runtime.GOOS {
case "windows":
a.Env.DataDirectory = filepath.Join(a.Env.HomeDirectory, "/AppData/Local/Rolens")
case "darwin":
a.Env.DataDirectory = filepath.Join(a.Env.HomeDirectory, "/Library/Application Support/Rolens")
case "linux":
a.Env.DataDirectory = filepath.Join(a.Env.HomeDirectory, "/.config/Rolens")
default:
panic(errors.New("unsupported platform"))
}
_ = os.MkdirAll(a.Env.DataDirectory, os.ModePerm)
}
func (a *App) Environment() EnvironmentInfo {
return a.Env
}
func menuEventEmitter(a *App, eventName string, data ...interface{}) func(cd *menu.CallbackData) {
@ -61,3 +112,26 @@ func (a *App) Menu() *menu.Menu {
return appMenu
}
func (a *App) OpenDirectory(id, title string) string {
if title == "" {
title = "Choose a directory"
}
dir, err := wailsRuntime.OpenDirectoryDialog(a.ctx, wailsRuntime.OpenDialogOptions{
Title: title,
CanCreateDirectories: true,
TreatPackagesAsDirectories: false,
})
if err != nil {
fmt.Println(err.Error())
wailsRuntime.MessageDialog(a.ctx, wailsRuntime.MessageDialogOptions{
Type: wailsRuntime.ErrorDialog,
Title: "Encountered an error while opening directory",
Message: err.Error(),
})
}
return dir
}

View File

@ -1,37 +0,0 @@
package app
import (
"os/exec"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
type EnvironmentInfo struct {
Arch string `json:"arch"`
BuildType string `json:"buildType"`
Platform string `json:"platform"`
HasMongoExport bool `json:"hasMongoExport"`
HasMongoDump bool `json:"hasMongoDump"`
}
var env EnvironmentInfo
var envKnown = false
func (a *App) Environment() EnvironmentInfo {
if !envKnown {
wailsEnv := runtime.Environment(a.ctx)
env.Arch = wailsEnv.Arch
env.BuildType = wailsEnv.BuildType
env.Platform = wailsEnv.Platform
_, err := exec.LookPath("mongodump")
env.HasMongoDump = err == nil
_, err = exec.LookPath("mongoexport")
env.HasMongoExport = err == nil
envKnown = true
}
return env
}

View File

@ -0,0 +1,113 @@
package app
import (
"encoding/json"
"fmt"
"os/exec"
"path"
"strings"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
type ExportType string
type FileType string
type ExportInfo struct {
Type ExportType `json:"type"`
FileType FileType `json:"fileType"`
OutDir string `json:"outdir"`
Filename string `json:"filename"`
HostKey string `json:"hostKey"`
DbKey string `json:"dbKey"`
CollKeys []string `json:"collKeys"`
}
const (
ExportTypeExport ExportType = "export"
ExportTypeDump ExportType = "dump"
FileTypeJson FileType = "json"
FileTypeBson FileType = "bson"
FileTypeCsv FileType = "csv"
)
func (a *App) PerformExport(jsonData string) bool {
var info ExportInfo
err := json.Unmarshal([]byte(jsonData), &info)
if err != nil {
fmt.Println(err.Error())
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: runtime.ErrorDialog,
Title: "Could not unmarshal JSON",
Message: err.Error(),
})
return false
}
hosts, err := a.Hosts()
if err != nil {
fmt.Println(err.Error())
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: runtime.ErrorDialog,
Title: "Could not retrieve hosts",
Message: err.Error(),
})
return false
}
host := hosts[info.HostKey]
switch info.Type {
case ExportTypeExport:
if !a.Env.HasMongoExport {
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: runtime.ErrorDialog,
Title: "You need to install mongoexport to perform an export.",
})
return false
}
args := make([]string, 0)
args = append(args, fmt.Sprintf(`--uri="%v"`, host.URI))
args = append(args, fmt.Sprintf(`--type="%v"`, info.FileType))
if info.DbKey != "" {
args = append(args, fmt.Sprintf(`--db="%v"`, info.DbKey))
if info.CollKeys != nil {
args = append(args, fmt.Sprintf(`--collection="%v"`, info.CollKeys[0]))
}
}
args = append(args, fmt.Sprintf(`--out="%v.%v"`, path.Join(info.OutDir, info.Filename), info.FileType))
cmd := exec.Command("mongoexport", args...)
var stdout strings.Builder
var stderr strings.Builder
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err = cmd.Run()
fmt.Println(args)
fmt.Println(stdout.String())
fmt.Println(stderr.String())
fmt.Println(err)
case ExportTypeDump:
if !a.Env.HasMongoDump {
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: runtime.ErrorDialog,
Title: "You need to install mongodump to perform a dump.",
})
return false
}
default:
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: runtime.ErrorDialog,
Title: "Unrecognised export type",
Message: string(info.Type),
})
return false
}
return err == nil
}

View File

@ -6,6 +6,7 @@ import (
"fmt"
"io/ioutil"
"os"
"path"
"github.com/google/uuid"
"github.com/wailsapp/wails/v2/pkg/runtime"
@ -16,12 +17,8 @@ type Host struct {
URI string `json:"uri"`
}
func updateHostsFile(newData map[string]Host) error {
filePath, err := appDataFilePath("hosts.json")
if err != nil {
return err
}
func updateHostsFile(a *App, newData map[string]Host) error {
filePath := path.Join(a.Env.DataDirectory, "hosts.json")
jsonData, err := json.MarshalIndent(newData, "", "\t")
if err != nil {
return err
@ -32,12 +29,7 @@ func updateHostsFile(newData map[string]Host) error {
}
func (a *App) Hosts() (map[string]Host, error) {
filePath, err := appDataFilePath("hosts.json")
if err != nil {
fmt.Println(err.Error())
return nil, err
}
filePath := path.Join(a.Env.DataDirectory, "hosts.json")
jsonData, err := ioutil.ReadFile(filePath)
if err != nil {
// It's ok if the file cannot be opened, for example if it is not accessible.
@ -90,7 +82,7 @@ func (a *App) AddHost(jsonData string) error {
}
hosts[id.String()] = newHost
err = updateHostsFile(hosts)
err = updateHostsFile(a, hosts)
if err != nil {
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: runtime.InfoDialog,
@ -123,7 +115,7 @@ func (a *App) UpdateHost(hostKey string, jsonData string) error {
}
hosts[hostKey] = host
err = updateHostsFile(hosts)
err = updateHostsFile(a, hosts)
if err != nil {
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: runtime.InfoDialog,
@ -157,7 +149,7 @@ func (a *App) RemoveHost(key string) error {
}
delete(hosts, key)
err = updateHostsFile(hosts)
err = updateHostsFile(a, hosts)
if err != nil {
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{

View File

@ -2,71 +2,33 @@ package app
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
goRuntime "runtime"
"path"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
type Settings struct {
DefaultLimit int64 `json:"defaultLimit"`
DefaultSort string `json:"defaultSort"`
AutosubmitQuery bool `json:"autosubmitQuery"`
DefaultLimit int64 `json:"defaultLimit"`
DefaultSort string `json:"defaultSort"`
AutosubmitQuery bool `json:"autosubmitQuery"`
DefaultExportDirectory string `json:"defaultExportDirectory"`
}
func NewSettings() Settings {
func NewSettings(a *App) Settings {
return Settings{
DefaultLimit: 20,
DefaultSort: `{ "_id": 1 }`,
AutosubmitQuery: true,
DefaultLimit: 20,
DefaultSort: `{ "_id": 1 }`,
AutosubmitQuery: true,
DefaultExportDirectory: path.Join(a.Env.HomeDirectory, "Downloads"),
}
}
func appDataDirectory() (string, error) {
var err error
homeDir, err := os.UserHomeDir()
prefDir := ""
switch goRuntime.GOOS {
case "windows":
prefDir = filepath.Join(homeDir, "/AppData/Local/Rolens")
case "darwin":
prefDir = filepath.Join(homeDir, "/Library/Application Support/Rolens")
case "linux":
prefDir = filepath.Join(homeDir, "/.config/Rolens")
default:
err = errors.New("unsupported platform")
}
_ = os.MkdirAll(prefDir, os.ModePerm)
return prefDir, err
}
func appDataFilePath(filename string) (string, error) {
dir, err := appDataDirectory()
if err != nil {
return "", err
}
path := filepath.Join(dir, filename)
return path, nil
}
func (a *App) Settings() Settings {
s := NewSettings()
filePath, err := appDataFilePath("settings.json")
if err != nil {
fmt.Println(err.Error())
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: runtime.ErrorDialog,
Title: "Could not retrieve application settings, using defaults!",
Message: err.Error(),
})
return s
}
s := NewSettings(a)
filePath := path.Join(a.Env.DataDirectory, "settings.json")
jsonData, err := ioutil.ReadFile(filePath)
if err != nil {
@ -105,16 +67,6 @@ func (a *App) UpdateSettings(jsonData string) Settings {
return s
}
filePath, err := appDataFilePath("settings.json")
if err != nil {
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: runtime.InfoDialog,
Title: "Could not update settings.json",
Message: err.Error(),
})
return s
}
newJson, err := json.MarshalIndent(s, "", "\t")
if err != nil {
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
@ -125,6 +77,7 @@ func (a *App) UpdateSettings(jsonData string) Settings {
return s
}
filePath := path.Join(a.Env.DataDirectory, "settings.json")
err = ioutil.WriteFile(filePath, newJson, os.ModePerm)
if err != nil {
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{

View File

@ -6,6 +6,7 @@ import (
"fmt"
"io/ioutil"
"os"
"path"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
@ -54,11 +55,8 @@ var BuiltInListView = View{
type ViewStore map[string]View
func updateViewStore(newData ViewStore) error {
filePath, err := appDataFilePath("views.json")
if err != nil {
return err
}
func updateViewStore(a *App, newData ViewStore) error {
filePath := path.Join(a.Env.DataDirectory, "settings.json")
jsonData, err := json.MarshalIndent(newData, "", "\t")
if err != nil {
@ -71,11 +69,7 @@ func updateViewStore(newData ViewStore) error {
func (a *App) Views() (ViewStore, error) {
views := make(ViewStore, 0)
filePath, err := appDataFilePath("views.json")
if err != nil {
fmt.Println(err.Error())
return views, err
}
filePath := path.Join(a.Env.DataDirectory, "views.json")
jsonData, err := ioutil.ReadFile(filePath)
if err != nil {
@ -108,7 +102,7 @@ func (a *App) UpdateViewStore(jsonData string) error {
return errors.New("invalid JSON")
}
err = updateViewStore(viewStore)
err = updateViewStore(a, viewStore)
if err != nil {
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: runtime.InfoDialog,
@ -142,7 +136,7 @@ func (a *App) RemoveView(viewKey string) error {
}
delete(views, viewKey)
err = updateViewStore(views)
err = updateViewStore(a, views)
if err != nil {
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{