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

Removed export functionality. Only dumping for now

This commit is contained in:
Romein van Buren 2023-02-21 20:26:46 +01:00
parent 5dd9e0573d
commit b15fde11db
Signed by: romein
GPG Key ID: 0EFF8478ADDF6C49
7 changed files with 94 additions and 145 deletions

View File

@ -160,7 +160,7 @@
</label> </label>
</div> </div>
<div class="form-row three"> <div class="form-row actions">
<!-- <CodeExample {code} /> --> <!-- <CodeExample {code} /> -->
<button class="btn" type="button" on:click={loadQuery}> <button class="btn" type="button" on:click={loadQuery}>
<Icon name="upload" /> Load query… <Icon name="upload" /> Load query…
@ -168,6 +168,9 @@
<button class="btn" type="button" on:click={saveQuery}> <button class="btn" type="button" on:click={saveQuery}>
<Icon name="save" /> Save as… <Icon name="save" /> Save as…
</button> </button>
<button class="btn" type="button" on:click={saveQuery}>
<Icon name="save" /> Export results…
</button>
<button type="submit" class="btn" title="Run query"> <button type="submit" class="btn" title="Run query">
<Icon name="play" /> Run <Icon name="play" /> Run
</button> </button>
@ -276,10 +279,10 @@
.form-row.two { .form-row.two {
grid-template: 1fr / 5fr 1fr 1fr; grid-template: 1fr / 5fr 1fr 1fr;
} }
.form-row.three { .form-row.actions {
margin-bottom: 0rem; margin-bottom: 0rem;
grid-template: 1fr / repeat(3, auto); grid-template: 1fr / repeat(4, auto);
justify-content: end; justify-content: start;
} }
.result { .result {

View File

@ -5,26 +5,14 @@
import busy from '$lib/stores/busy'; import busy from '$lib/stores/busy';
import { connections } from '$lib/stores/connections'; import { connections } from '$lib/stores/connections';
import applicationSettings from '$lib/stores/settings'; import applicationSettings from '$lib/stores/settings';
import { OpenConnection, OpenDatabase, PerformExport } from '$wails/go/app/App'; import { OpenConnection, OpenDatabase, PerformDump } from '$wails/go/app/App';
export let info; export let info;
export let hosts = {}; export let hosts = {};
const actionLabel = {
export: 'Perform export',
dump: 'Perform dump',
};
$: if (info) { $: if (info) {
info.outdir = info.outdir || $applicationSettings.defaultExportDirectory; info.outdir = info.outdir || $applicationSettings.defaultExportDirectory;
info.filename = info.filename || `Export ${new Date().getTime()}`; info.filename = info.filename || `Dump ${new Date().getTime()}`;
if (info.filetype === 'bson') {
info.type = 'dump';
}
else {
info.type = 'export';
}
} }
async function selectHost(hostKey) { async function selectHost(hostKey) {
@ -63,8 +51,11 @@
} }
} }
async function performExport() { async function performDump() {
await PerformExport(JSON.stringify(info)); const ok = await PerformDump(JSON.stringify(info));
if (ok) {
info = undefined;
}
} }
function selectCollection(collKey) { function selectCollection(collKey) {
@ -72,38 +63,16 @@
} }
</script> </script>
<Modal bind:show={info} title={actionLabel[info?.type]}> <Modal bind:show={info} title="Perform dump">
<form on:submit|preventDefault={performExport}> <form on:submit|preventDefault={performDump}>
<!-- svelte-ignore a11y-label-has-associated-control - input is in DirectoryChooser --> <!-- svelte-ignore a11y-label-has-associated-control - input is in DirectoryChooser -->
<label class="field"> <label class="field">
<span class="label">Output destination:</span> <span class="label">Output destination:</span>
<DirectoryChooser bind:value={info.outdir} /> <DirectoryChooser bind:value={info.outdir} />
<span class="label">/</span> <span class="label">/</span>
<input type="text" bind:value={info.filename} /> <input type="text" bind:value={info.filename} />
<span class="label">.</span>
<select bind:value={info.filetype} class="filetype">
<optgroup label="Dump (via mongodump)">
<option value="bson">bson</option>
</optgroup>
<optgroup label="Export">
<option value="csv">csv</option>
<option value="json">json</option>
</optgroup>
</select>
</label> </label>
<div class="options">
{#if info.filetype === 'json'}
<label class="field">
<span class="label">Separate items using:</span>
<select bind:value={info.jsonType}>
<option value="newline">Newline</option>
<option value="array">JSON array</option>
</select>
</label>
{/if}
</div>
<div class="location"> <div class="location">
<div class="grid"> <div class="grid">
<Grid <Grid
@ -156,7 +125,7 @@
</div> </div>
<div> <div>
<button type="submit" class="btn">{actionLabel[info.type]}</button> <button type="submit" class="btn">Perform dump</button>
</div> </div>
</form> </form>
</Modal> </Modal>
@ -164,7 +133,7 @@
<style> <style>
form { form {
display: grid; display: grid;
grid-template: auto auto 1fr auto / 1fr; grid-template: auto 1fr auto / 1fr;
gap: 0.5rem; gap: 0.5rem;
} }
.location { .location {
@ -176,8 +145,4 @@
border: 1px solid #ccc; border: 1px solid #ccc;
overflow-y: auto; overflow-y: auto;
} }
select.filetype {
flex: 0 1;
}
</style> </style>

View File

@ -6,7 +6,7 @@
import { EventsOn } from '$wails/runtime/runtime'; import { EventsOn } from '$wails/runtime/runtime';
import { onMount } from 'svelte'; import { onMount } from 'svelte';
import CollectionDetail from './collection/index.svelte'; import CollectionDetail from './collection/index.svelte';
import Export from './export/export.svelte'; import DumpInfo from './dump.svelte';
import HostDetail from './hostdetail.svelte'; import HostDetail from './hostdetail.svelte';
import HostTree from './hosttree.svelte'; import HostTree from './hosttree.svelte';
@ -119,7 +119,7 @@
{hosts} {hosts}
/> />
<Export bind:info={exportInfo} {hosts} /> <DumpInfo bind:info={exportInfo} {hosts} />
<style> <style>
.tree { .tree {

View File

@ -37,7 +37,7 @@ export function OpenConnection(arg1:string):Promise<Array<string>>;
export function OpenDatabase(arg1:string,arg2:string):Promise<Array<string>>; export function OpenDatabase(arg1:string,arg2:string):Promise<Array<string>>;
export function PerformExport(arg1:string):Promise<boolean>; export function PerformDump(arg1:string):Promise<boolean>;
export function PurgeLogDirectory():Promise<void>; export function PurgeLogDirectory():Promise<void>;

View File

@ -62,8 +62,8 @@ export function OpenDatabase(arg1, arg2) {
return window['go']['app']['App']['OpenDatabase'](arg1, arg2); return window['go']['app']['App']['OpenDatabase'](arg1, arg2);
} }
export function PerformExport(arg1) { export function PerformDump(arg1) {
return window['go']['app']['App']['PerformExport'](arg1); return window['go']['app']['App']['PerformDump'](arg1);
} }
export function PurgeLogDirectory() { export function PurgeLogDirectory() {

View File

@ -1,90 +0,0 @@
package app
import (
"encoding/json"
"fmt"
"os/exec"
"path"
"strings"
"github.com/ncruces/zenity"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
type FileType string
type ExportInfo struct {
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 (
FileTypeJson FileType = "json"
FileTypeDump FileType = "dump"
FileTypeCsv FileType = "csv"
)
func (a *App) PerformExport(jsonData string) bool {
var info ExportInfo
err := json.Unmarshal([]byte(jsonData), &info)
if err != nil {
runtime.LogError(a.ctx, "Could not unmarshal export form")
runtime.LogError(a.ctx, err.Error())
zenity.Error(err.Error(), zenity.Title("Could not parse JSON"), zenity.ErrorIcon)
return false
}
hosts, err := a.Hosts()
if err != nil {
zenity.Error(err.Error(), zenity.Title("Error while getting hosts"), zenity.ErrorIcon)
return false
}
host := hosts[info.HostKey]
switch info.FileType {
case FileTypeCsv:
case FileTypeJson:
case FileTypeDump:
if !a.Env.HasMongoDump {
zenity.Error("You need to install mongodump to perform a dump.", zenity.Title("Additional tooling required"), zenity.ErrorIcon)
return false
}
args := make([]string, 0)
args = append(args, fmt.Sprintf(`--uri="%v"`, host.URI))
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("mongodump", args...)
var stdout strings.Builder
var stderr strings.Builder
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err = cmd.Run()
runtime.LogInfo(a.ctx, "Performing dump, executing command: mongodump "+strings.Join(args, " "))
runtime.LogInfo(a.ctx, "mongodump stdout: "+stdout.String())
runtime.LogInfo(a.ctx, "mongodump sterr: "+stderr.String())
if err != nil {
runtime.LogWarning(a.ctx, "Error while executing mongodump: "+err.Error())
}
default:
zenity.Error(fmt.Sprintf("File type '%v' is not known.", info.FileType), zenity.Title("Unrecognised file type"), zenity.ErrorIcon)
return false
}
return err == nil
}

71
internal/app/dump.go Normal file
View File

@ -0,0 +1,71 @@
package app
import (
"encoding/json"
"fmt"
"os/exec"
"path"
"strings"
"github.com/ncruces/zenity"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
type DumpInfo struct {
OutDir string `json:"outdir"`
Filename string `json:"filename"`
HostKey string `json:"hostKey"`
DbKey string `json:"dbKey"`
CollKeys []string `json:"collKeys"`
}
func (a *App) PerformDump(jsonData string) bool {
var info DumpInfo
err := json.Unmarshal([]byte(jsonData), &info)
if err != nil {
runtime.LogError(a.ctx, "Could not unmarshal dump form")
runtime.LogError(a.ctx, err.Error())
zenity.Error(err.Error(), zenity.Title("Could not parse JSON"), zenity.ErrorIcon)
return false
}
hosts, err := a.Hosts()
if err != nil {
zenity.Error(err.Error(), zenity.Title("Error while getting hosts"), zenity.ErrorIcon)
return false
}
host := hosts[info.HostKey]
if !a.Env.HasMongoDump {
zenity.Error("You need to install mongodump to perform a dump.", zenity.Title("Additional tooling required"), zenity.ErrorIcon)
return false
}
args := make([]string, 0)
args = append(args, fmt.Sprintf(`--uri="%v"`, host.URI))
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"`, path.Join(info.OutDir, info.Filename)))
cmd := exec.Command("mongodump", args...)
var stdout strings.Builder
var stderr strings.Builder
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err = cmd.Run()
runtime.LogInfo(a.ctx, "Performing dump, executing command: mongodump "+strings.Join(args, " "))
runtime.LogInfo(a.ctx, "mongodump stdout: "+stdout.String())
runtime.LogInfo(a.ctx, "mongodump sterr: "+stderr.String())
if err != nil {
runtime.LogWarning(a.ctx, "Error while executing mongodump: "+err.Error())
}
return err == nil
}