mirror of
https://github.com/garraflavatra/rolens.git
synced 2025-01-18 04:57:59 +00:00
Merge branch 'shell_queries' of https://github.com/garraflavatra/mongodup into shell_queries
This commit is contained in:
commit
0965344f35
57
frontend/src/components/codeeditor.svelte
Normal file
57
frontend/src/components/codeeditor.svelte
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
<script>
|
||||||
|
import { indentWithTab } from '@codemirror/commands';
|
||||||
|
import { indentOnInput } from '@codemirror/language';
|
||||||
|
import { EditorState } from '@codemirror/state';
|
||||||
|
import { EditorView, keymap } from '@codemirror/view';
|
||||||
|
import { basicSetup } from 'codemirror';
|
||||||
|
import { createEventDispatcher, onMount } from 'svelte';
|
||||||
|
|
||||||
|
export let text = '';
|
||||||
|
export let editor = undefined;
|
||||||
|
export let extensions = [];
|
||||||
|
|
||||||
|
const dispatch = createEventDispatcher();
|
||||||
|
let editorParent;
|
||||||
|
|
||||||
|
const editorState = EditorState.create({
|
||||||
|
doc: '',
|
||||||
|
extensions: [
|
||||||
|
basicSetup,
|
||||||
|
keymap.of([ indentWithTab, indentOnInput ]),
|
||||||
|
EditorState.tabSize.of(4),
|
||||||
|
EditorView.updateListener.of(e => {
|
||||||
|
if (!e.docChanged) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
text = e.state.doc.toString();
|
||||||
|
dispatch('updated', { text });
|
||||||
|
}),
|
||||||
|
...extensions,
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
onMount(() => {
|
||||||
|
editor = new EditorView({
|
||||||
|
parent: editorParent,
|
||||||
|
state: editorState,
|
||||||
|
});
|
||||||
|
|
||||||
|
dispatch('inited', { editor });
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<div bind:this={editorParent} class="editor"></div>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
.editor {
|
||||||
|
width: 100%;
|
||||||
|
background-color: #fff;
|
||||||
|
border-radius: var(--radius);
|
||||||
|
overflow: hidden;
|
||||||
|
}
|
||||||
|
|
||||||
|
.editor :global(.cm-editor) {
|
||||||
|
overflow: auto;
|
||||||
|
height: 100%;
|
||||||
|
}
|
||||||
|
</style>
|
2
frontend/src/components/icon.svelte
vendored
2
frontend/src/components/icon.svelte
vendored
@ -147,6 +147,8 @@
|
|||||||
<path d="M10.29 3.86 1.82 18a2 2 0 0 0 1.71 3h16.94a2 2 0 0 0 1.71-3L13.71 3.86a2 2 0 0 0-3.42 0zM12 9v4M12 17h.01" />
|
<path d="M10.29 3.86 1.82 18a2 2 0 0 0 1.71 3h16.94a2 2 0 0 0 1.71-3L13.71 3.86a2 2 0 0 0-3.42 0zM12 9v4M12 17h.01" />
|
||||||
{:else if name === 'loading'}
|
{:else if name === 'loading'}
|
||||||
<path d="M12 2v4M12 18v4M4.93 4.93l2.83 2.83M16.24 16.24l2.83 2.83M2 12h4M18 12h4M4.93 19.07l2.83-2.83M16.24 7.76l2.83-2.83" />
|
<path d="M12 2v4M12 18v4M4.93 4.93l2.83 2.83M16.24 16.24l2.83 2.83M2 12h4M18 12h4M4.93 19.07l2.83-2.83M16.24 7.76l2.83-2.83" />
|
||||||
|
{:else if name === 'shell'}
|
||||||
|
<path d="m4 17 6-6-6-6M12 19h8" />
|
||||||
{:else if name === 'doc'}
|
{:else if name === 'doc'}
|
||||||
<path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z" /><path d="M14 2v6h6M16 13H8M16 17H8M10 9H8" />
|
<path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z" /><path d="M14 2v6h6M16 13H8M16 17H8M10 9H8" />
|
||||||
{/if}
|
{/if}
|
||||||
|
@ -1,43 +1,17 @@
|
|||||||
<script>
|
<script>
|
||||||
import { indentWithTab } from '@codemirror/commands';
|
|
||||||
import { javascript } from '@codemirror/lang-javascript';
|
import { javascript } from '@codemirror/lang-javascript';
|
||||||
import { indentOnInput } from '@codemirror/language';
|
import { onMount } from 'svelte';
|
||||||
import { EditorState } from '@codemirror/state';
|
import CodeEditor from './codeeditor.svelte';
|
||||||
import { EditorView, keymap } from '@codemirror/view';
|
|
||||||
import { basicSetup } from 'codemirror';
|
|
||||||
import { createEventDispatcher, onMount } from 'svelte';
|
|
||||||
|
|
||||||
export let text = '';
|
export let text = '';
|
||||||
export let editor = undefined;
|
export let editor = undefined;
|
||||||
export let readonly = false;
|
export let readonly = false;
|
||||||
|
|
||||||
const dispatch = createEventDispatcher();
|
const extensions = [
|
||||||
let editorParent;
|
javascript(),
|
||||||
|
];
|
||||||
const editorState = EditorState.create({
|
|
||||||
doc: '',
|
|
||||||
extensions: [
|
|
||||||
basicSetup,
|
|
||||||
keymap.of([ indentWithTab, indentOnInput ]),
|
|
||||||
javascript(),
|
|
||||||
EditorState.tabSize.of(4),
|
|
||||||
EditorState.readOnly.of(readonly),
|
|
||||||
EditorView.updateListener.of(e => {
|
|
||||||
if (!e.docChanged) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
text = e.state.doc.toString();
|
|
||||||
dispatch('updated', { text });
|
|
||||||
}),
|
|
||||||
],
|
|
||||||
});
|
|
||||||
|
|
||||||
onMount(() => {
|
onMount(() => {
|
||||||
editor = new EditorView({
|
|
||||||
parent: editorParent,
|
|
||||||
state: editorState,
|
|
||||||
});
|
|
||||||
|
|
||||||
editor.dispatch({
|
editor.dispatch({
|
||||||
changes: {
|
changes: {
|
||||||
from: 0,
|
from: 0,
|
||||||
@ -45,23 +19,13 @@
|
|||||||
insert: text,
|
insert: text,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
dispatch('inited', { editor });
|
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div bind:this={editorParent} class="editor"></div>
|
<CodeEditor bind:editor
|
||||||
|
bind:text
|
||||||
<style>
|
on:inited
|
||||||
.editor {
|
on:updated
|
||||||
width: 100%;
|
{extensions}
|
||||||
background-color: #fff;
|
{readonly}
|
||||||
border-radius: var(--radius);
|
/>
|
||||||
overflow: hidden;
|
|
||||||
}
|
|
||||||
|
|
||||||
.editor :global(.cm-editor) {
|
|
||||||
overflow: auto;
|
|
||||||
height: 100%;
|
|
||||||
}
|
|
||||||
</style>
|
|
||||||
|
@ -16,6 +16,7 @@ import {
|
|||||||
DropCollection,
|
DropCollection,
|
||||||
DropDatabase,
|
DropDatabase,
|
||||||
DropIndex,
|
DropIndex,
|
||||||
|
ExecuteShellScript,
|
||||||
GetIndexes,
|
GetIndexes,
|
||||||
HostLogs,
|
HostLogs,
|
||||||
Hosts,
|
Hosts,
|
||||||
@ -224,6 +225,11 @@ async function refresh() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
collection.executeShellScript = async function(script) {
|
||||||
|
const result = await ExecuteShellScript(hostKey, dbKey, collKey, script);
|
||||||
|
return result;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
await refresh();
|
await refresh();
|
||||||
@ -262,11 +268,21 @@ async function refresh() {
|
|||||||
await database.open();
|
await database.open();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
database.executeShellScript = async function(script) {
|
||||||
|
const result = await ExecuteShellScript(hostKey, dbKey, '', script);
|
||||||
|
return result;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
await refresh();
|
await refresh();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
host.executeShellScript = async function(script) {
|
||||||
|
const result = await ExecuteShellScript(hostKey, '', '', script);
|
||||||
|
return result;
|
||||||
|
};
|
||||||
|
|
||||||
host.newDatabase = async function() {
|
host.newDatabase = async function() {
|
||||||
const name = await dialogs.enterText('Create a database', 'Enter the database name. Note: databases in MongoDB do not exist until they have a collection and an item. Your new database will not persist on the server; fill it to have it created.', '');
|
const name = await dialogs.enterText('Create a database', 'Enter the database name. Note: databases in MongoDB do not exist until they have a collection and an item. Your new database will not persist on the server; fill it to have it created.', '');
|
||||||
if (name) {
|
if (name) {
|
||||||
|
@ -9,6 +9,7 @@
|
|||||||
import Indexes from './indexes.svelte';
|
import Indexes from './indexes.svelte';
|
||||||
import Insert from './insert.svelte';
|
import Insert from './insert.svelte';
|
||||||
import Remove from './remove.svelte';
|
import Remove from './remove.svelte';
|
||||||
|
import Shell from '../shell.svelte';
|
||||||
import Stats from './stats.svelte';
|
import Stats from './stats.svelte';
|
||||||
import Update from './update.svelte';
|
import Update from './update.svelte';
|
||||||
|
|
||||||
@ -42,16 +43,19 @@
|
|||||||
<div class="view" class:empty={!collection}>
|
<div class="view" class:empty={!collection}>
|
||||||
{#if collection}
|
{#if collection}
|
||||||
{#key collection}
|
{#key collection}
|
||||||
<TabBar tabs={[
|
<TabBar
|
||||||
{ key: 'stats', icon: 'chart', title: 'Stats' },
|
tabs={[
|
||||||
{ key: 'find', icon: 'db', title: 'Find' },
|
{ key: 'stats', icon: 'chart', title: 'Stats' },
|
||||||
{ key: 'insert', icon: '+', title: 'Insert' },
|
{ key: 'find', icon: 'db', title: 'Find' },
|
||||||
{ key: 'update', icon: 'edit', title: 'Update' },
|
{ key: 'insert', icon: '+', title: 'Insert' },
|
||||||
{ key: 'remove', icon: 'trash', title: 'Remove' },
|
{ key: 'update', icon: 'edit', title: 'Update' },
|
||||||
{ key: 'indexes', icon: 'list', title: 'Indexes' },
|
{ key: 'remove', icon: 'trash', title: 'Remove' },
|
||||||
{ key: 'aggregate', icon: 're', title: 'Aggregate' },
|
{ key: 'indexes', icon: 'list', title: 'Indexes' },
|
||||||
]}
|
{ key: 'aggregate', icon: 're', title: 'Aggregate' },
|
||||||
bind:selectedKey={tab} />
|
{ key: 'shell', icon: 'shell', title: 'Shell' },
|
||||||
|
]}
|
||||||
|
bind:selectedKey={tab}
|
||||||
|
/>
|
||||||
|
|
||||||
<div class="container">
|
<div class="container">
|
||||||
{#if tab === 'stats'} <Stats {collection} />
|
{#if tab === 'stats'} <Stats {collection} />
|
||||||
@ -61,6 +65,7 @@
|
|||||||
{:else if tab === 'remove'} <Remove {collection} />
|
{:else if tab === 'remove'} <Remove {collection} />
|
||||||
{:else if tab === 'indexes'} <Indexes {collection} />
|
{:else if tab === 'indexes'} <Indexes {collection} />
|
||||||
{:else if tab === 'aggregate'} <Aggregate {collection} />
|
{:else if tab === 'aggregate'} <Aggregate {collection} />
|
||||||
|
{:else if tab === 'shell'} <Shell {collection} />
|
||||||
{/if}
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
{/key}
|
{/key}
|
||||||
|
@ -2,6 +2,8 @@
|
|||||||
import BlankState from '$components/blankstate.svelte';
|
import BlankState from '$components/blankstate.svelte';
|
||||||
import TabBar from '$components/tabbar.svelte';
|
import TabBar from '$components/tabbar.svelte';
|
||||||
import { EventsOn } from '$wails/runtime/runtime';
|
import { EventsOn } from '$wails/runtime/runtime';
|
||||||
|
|
||||||
|
import Shell from '../shell.svelte';
|
||||||
import Stats from './stats.svelte';
|
import Stats from './stats.svelte';
|
||||||
|
|
||||||
export let database;
|
export let database;
|
||||||
@ -24,9 +26,15 @@
|
|||||||
<div class="view" class:empty={!database}>
|
<div class="view" class:empty={!database}>
|
||||||
{#if database}
|
{#if database}
|
||||||
{#key database}
|
{#key database}
|
||||||
<TabBar tabs={[ { key: 'stats', icon: 'chart', title: 'Database stats' } ]} bind:selectedKey={tab} />
|
<TabBar
|
||||||
|
tabs={[
|
||||||
|
{ key: 'stats', icon: 'chart', title: 'Database stats' },
|
||||||
|
{ key: 'shell', icon: 'shell', title: 'Shell' },
|
||||||
|
]}
|
||||||
|
bind:selectedKey={tab} />
|
||||||
<div class="container">
|
<div class="container">
|
||||||
{#if tab === 'stats'} <Stats {database} />
|
{#if tab === 'stats'} <Stats {database} />
|
||||||
|
{:else if tab === 'shell'} <Shell {database} />
|
||||||
{/if}
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
{/key}
|
{/key}
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
import { EventsOn } from '$wails/runtime/runtime';
|
import { EventsOn } from '$wails/runtime/runtime';
|
||||||
|
|
||||||
import Logs from './logs.svelte';
|
import Logs from './logs.svelte';
|
||||||
|
import Shell from '../shell.svelte';
|
||||||
import Status from './status.svelte';
|
import Status from './status.svelte';
|
||||||
import SystemInfo from './systeminfo.svelte';
|
import SystemInfo from './systeminfo.svelte';
|
||||||
|
|
||||||
@ -28,6 +29,7 @@
|
|||||||
<TabBar
|
<TabBar
|
||||||
tabs={[
|
tabs={[
|
||||||
{ key: 'status', icon: 'chart', title: 'Host status' },
|
{ key: 'status', icon: 'chart', title: 'Host status' },
|
||||||
|
{ key: 'shell', icon: 'shell', title: 'Shell' },
|
||||||
{ key: 'logs', icon: 'doc', title: 'Logs' },
|
{ key: 'logs', icon: 'doc', title: 'Logs' },
|
||||||
{ key: 'systemInfo', icon: 'server', title: 'System info' },
|
{ key: 'systemInfo', icon: 'server', title: 'System info' },
|
||||||
]}
|
]}
|
||||||
@ -38,6 +40,7 @@
|
|||||||
{#if tab === 'status'} <Status {host} />
|
{#if tab === 'status'} <Status {host} />
|
||||||
{:else if tab === 'logs'} <Logs {host} />
|
{:else if tab === 'logs'} <Logs {host} />
|
||||||
{:else if tab === 'systemInfo'} <SystemInfo {host} />
|
{:else if tab === 'systemInfo'} <SystemInfo {host} />
|
||||||
|
{:else if tab === 'shell'} <Shell {host} />
|
||||||
{/if}
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
{/key}
|
{/key}
|
||||||
|
146
frontend/src/organisms/connection/shell.svelte
Normal file
146
frontend/src/organisms/connection/shell.svelte
Normal file
@ -0,0 +1,146 @@
|
|||||||
|
<script>
|
||||||
|
import BlankState from '$components/blankstate.svelte';
|
||||||
|
import CodeEditor from '$components/codeeditor.svelte';
|
||||||
|
import Icon from '$components/icon.svelte';
|
||||||
|
import { javascript } from '@codemirror/lang-javascript';
|
||||||
|
import { onDestroy, onMount } from 'svelte';
|
||||||
|
|
||||||
|
export let host = undefined;
|
||||||
|
export let database = undefined;
|
||||||
|
export let collection = undefined;
|
||||||
|
|
||||||
|
const placeholder = '// Write your script here...';
|
||||||
|
const extensions = [ javascript() ];
|
||||||
|
let script = '';
|
||||||
|
let result = {};
|
||||||
|
let copySucceeded = false;
|
||||||
|
let timeout;
|
||||||
|
let busy = false;
|
||||||
|
let editor;
|
||||||
|
|
||||||
|
async function run() {
|
||||||
|
busy = true;
|
||||||
|
|
||||||
|
if (collection) {
|
||||||
|
result = await collection.executeShellScript(script);
|
||||||
|
}
|
||||||
|
else if (database) {
|
||||||
|
result = await database.executeShellScript(script);
|
||||||
|
}
|
||||||
|
else if (host) {
|
||||||
|
result = await host.executeShellScript(script);
|
||||||
|
}
|
||||||
|
|
||||||
|
busy = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function copyErrorDescription() {
|
||||||
|
await navigator.clipboard.writeText(result.errorDescription);
|
||||||
|
copySucceeded = true;
|
||||||
|
timeout = setTimeout(() => copySucceeded = false, 1500);
|
||||||
|
}
|
||||||
|
|
||||||
|
onMount(() => {
|
||||||
|
editor.dispatch({
|
||||||
|
changes: {
|
||||||
|
from: 0,
|
||||||
|
to: editor.state.doc.length,
|
||||||
|
insert: placeholder,
|
||||||
|
},
|
||||||
|
selection: {
|
||||||
|
from: 0,
|
||||||
|
anchor: 0,
|
||||||
|
to: placeholder.length,
|
||||||
|
head: placeholder.length,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
editor.focus();
|
||||||
|
});
|
||||||
|
onDestroy(() => clearTimeout(timeout));
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<div class="shell">
|
||||||
|
<div class="overflow">
|
||||||
|
<!-- svelte-ignore a11y-label-has-associated-control -->
|
||||||
|
<label class="field">
|
||||||
|
<CodeEditor bind:editor bind:text={script} {extensions} />
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="output">
|
||||||
|
{#if busy}
|
||||||
|
<BlankState icon="loading" label="Executing…" />
|
||||||
|
{:else if result.errorTitle || result.errorDescription}
|
||||||
|
<BlankState title={result.errorTitle} label={result.errorDescription} icon="!">
|
||||||
|
<button class="button-small" on:click={copyErrorDescription}>
|
||||||
|
<Icon name={copySucceeded ? 'check' : 'clipboard'} /> Copy error message
|
||||||
|
</button>
|
||||||
|
</BlankState>
|
||||||
|
{:else}
|
||||||
|
<pre>{result.output || ''}</pre>
|
||||||
|
{/if}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="controls">
|
||||||
|
{#key result}
|
||||||
|
<div class="status flash-green">
|
||||||
|
{#if result?.status}
|
||||||
|
Exit code: {result.status}
|
||||||
|
{/if}
|
||||||
|
</div>
|
||||||
|
{/key}
|
||||||
|
|
||||||
|
<button class="btn" on:click={run}>
|
||||||
|
<Icon name="play" /> Run
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
.shell {
|
||||||
|
display: grid;
|
||||||
|
grid-template: 1fr auto / 1fr 1fr;
|
||||||
|
}
|
||||||
|
|
||||||
|
.overflow {
|
||||||
|
overflow: auto;
|
||||||
|
}
|
||||||
|
|
||||||
|
.field {
|
||||||
|
height: 100%;
|
||||||
|
}
|
||||||
|
.field :global(.editor) {
|
||||||
|
border-radius: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.output {
|
||||||
|
background-color: #111;
|
||||||
|
color: #fff;
|
||||||
|
overflow: auto;
|
||||||
|
display: flex;
|
||||||
|
}
|
||||||
|
.output :global(*) {
|
||||||
|
color: #fff;
|
||||||
|
}
|
||||||
|
.output pre {
|
||||||
|
font-family: monospace;
|
||||||
|
padding: 0.5rem;
|
||||||
|
user-select: text;
|
||||||
|
-webkit-user-select: text;
|
||||||
|
cursor: text;
|
||||||
|
}
|
||||||
|
.output :global(.blankstate) {
|
||||||
|
margin: auto;
|
||||||
|
padding: 0.5rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.controls {
|
||||||
|
margin-top: 0.5rem;
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
grid-column: 1 / 3;
|
||||||
|
}
|
||||||
|
.controls .status {
|
||||||
|
margin-right: auto;
|
||||||
|
}
|
||||||
|
</style>
|
2
frontend/wailsjs/go/app/App.d.ts
generated
vendored
2
frontend/wailsjs/go/app/App.d.ts
generated
vendored
@ -20,6 +20,8 @@ export function DropIndex(arg1:string,arg2:string,arg3:string,arg4:string):Promi
|
|||||||
|
|
||||||
export function Environment():Promise<app.EnvironmentInfo>;
|
export function Environment():Promise<app.EnvironmentInfo>;
|
||||||
|
|
||||||
|
export function ExecuteShellScript(arg1:string,arg2:string,arg3:string,arg4:string):Promise<app.ExecuteShellScriptResult>;
|
||||||
|
|
||||||
export function FindItems(arg1:string,arg2:string,arg3:string,arg4:string):Promise<app.FindItemsResult>;
|
export function FindItems(arg1:string,arg2:string,arg3:string,arg4:string):Promise<app.FindItemsResult>;
|
||||||
|
|
||||||
export function GetIndexes(arg1:string,arg2:string,arg3:string):Promise<app.GetIndexesResult>;
|
export function GetIndexes(arg1:string,arg2:string,arg3:string):Promise<app.GetIndexesResult>;
|
||||||
|
4
frontend/wailsjs/go/app/App.js
generated
4
frontend/wailsjs/go/app/App.js
generated
@ -30,6 +30,10 @@ export function Environment() {
|
|||||||
return window['go']['app']['App']['Environment']();
|
return window['go']['app']['App']['Environment']();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function ExecuteShellScript(arg1, arg2, arg3, arg4) {
|
||||||
|
return window['go']['app']['App']['ExecuteShellScript'](arg1, arg2, arg3, arg4);
|
||||||
|
}
|
||||||
|
|
||||||
export function FindItems(arg1, arg2, arg3, arg4) {
|
export function FindItems(arg1, arg2, arg3, arg4) {
|
||||||
return window['go']['app']['App']['FindItems'](arg1, arg2, arg3, arg4);
|
return window['go']['app']['App']['FindItems'](arg1, arg2, arg3, arg4);
|
||||||
}
|
}
|
||||||
|
@ -25,6 +25,7 @@ type EnvironmentInfo struct {
|
|||||||
|
|
||||||
HasMongoExport bool `json:"hasMongoExport"`
|
HasMongoExport bool `json:"hasMongoExport"`
|
||||||
HasMongoDump bool `json:"hasMongoDump"`
|
HasMongoDump bool `json:"hasMongoDump"`
|
||||||
|
HasMongoShell bool `json:"hasMongoShell"`
|
||||||
|
|
||||||
HomeDirectory string `json:"homeDirectory"`
|
HomeDirectory string `json:"homeDirectory"`
|
||||||
DataDirectory string `json:"dataDirectory"`
|
DataDirectory string `json:"dataDirectory"`
|
||||||
@ -49,6 +50,9 @@ func NewApp(version string) *App {
|
|||||||
_, err = exec.LookPath("mongoexport")
|
_, err = exec.LookPath("mongoexport")
|
||||||
a.Env.HasMongoExport = err == nil
|
a.Env.HasMongoExport = err == nil
|
||||||
|
|
||||||
|
_, err = exec.LookPath("mongosh")
|
||||||
|
a.Env.HasMongoShell = err == nil
|
||||||
|
|
||||||
a.Env.HomeDirectory, err = os.UserHomeDir()
|
a.Env.HomeDirectory, err = os.UserHomeDir()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(errors.New("encountered an error while getting home directory"))
|
panic(errors.New("encountered an error while getting home directory"))
|
||||||
|
105
internal/app/host_shell.go
Normal file
105
internal/app/host_shell.go
Normal file
@ -0,0 +1,105 @@
|
|||||||
|
package app
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/url"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"path"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"github.com/wailsapp/wails/v2/pkg/runtime"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ExecuteShellScriptResult struct {
|
||||||
|
Output string `json:"output"`
|
||||||
|
Status int `json:"status"`
|
||||||
|
ErrorTitle string `json:"errorTitle"`
|
||||||
|
ErrorDescription string `json:"errorDescription"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *App) ExecuteShellScript(hostKey, dbKey, collKey, script string) (result ExecuteShellScriptResult) {
|
||||||
|
if !a.Env.HasMongoShell {
|
||||||
|
result.ErrorTitle = "mongosh not found"
|
||||||
|
result.ErrorDescription = "The mongosh executable is required to run a shell script. Please see https://www.mongodb.com/docs/mongodb-shell/install/"
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
hosts, err := a.Hosts()
|
||||||
|
if err != nil {
|
||||||
|
result.ErrorTitle = "Could not get hosts"
|
||||||
|
result.ErrorDescription = err.Error()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
host, hostFound := hosts[hostKey]
|
||||||
|
if !hostFound {
|
||||||
|
result.ErrorTitle = "The specified host does not seem to exist"
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
id, err := uuid.NewRandom()
|
||||||
|
if err != nil {
|
||||||
|
runtime.LogErrorf(a.ctx, "Shell: failed to generate a UUID: %s", err.Error())
|
||||||
|
result.ErrorTitle = "Could not generate UUID"
|
||||||
|
result.ErrorDescription = err.Error()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
dirname := path.Join(a.Env.DataDirectory, "Shell Scripts")
|
||||||
|
fname := path.Join(dirname, fmt.Sprintf("%s.mongosh.js", id.String()))
|
||||||
|
|
||||||
|
if err := os.MkdirAll(dirname, os.ModePerm); err != nil {
|
||||||
|
runtime.LogWarningf(a.ctx, "Shell: failed to mkdir %s", err.Error())
|
||||||
|
result.ErrorTitle = "Could not create temporary directory"
|
||||||
|
result.ErrorDescription = err.Error()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
scriptHeader := fmt.Sprintf("// Namespace: %s.%s\n", dbKey, collKey)
|
||||||
|
|
||||||
|
if dbKey != "" {
|
||||||
|
url, err := url.Parse(host.URI)
|
||||||
|
if err != nil {
|
||||||
|
runtime.LogWarningf(a.ctx, "Shell: failed to parse host URI %s: %s", host.URI, err.Error())
|
||||||
|
result.ErrorTitle = "Could parse host URI"
|
||||||
|
result.ErrorDescription = err.Error()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
url.Path = "/" + dbKey
|
||||||
|
scriptHeader = scriptHeader + fmt.Sprintf("db = connect('%s');\n", url.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
if collKey != "" {
|
||||||
|
scriptHeader = scriptHeader + fmt.Sprintf("coll = db.getCollection('%s');\n", collKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
scriptHeader = scriptHeader + "\n// Start of user script\n"
|
||||||
|
script = scriptHeader + script
|
||||||
|
|
||||||
|
if err := os.WriteFile(fname, []byte(script), os.ModePerm); err != nil {
|
||||||
|
runtime.LogWarningf(a.ctx, "Shell: failed to write script to %s", err.Error())
|
||||||
|
result.ErrorTitle = "Could not create temporary script file"
|
||||||
|
result.ErrorDescription = err.Error()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd := exec.Command("mongosh", "--file", fname, host.URI)
|
||||||
|
stdout, err := cmd.Output()
|
||||||
|
|
||||||
|
if exiterr, ok := err.(*exec.ExitError); ok {
|
||||||
|
result.Status = exiterr.ExitCode()
|
||||||
|
} else if err != nil {
|
||||||
|
runtime.LogWarningf(a.ctx, "Shell: failed to execute: mongosh --file %s: %s", fname, err.Error())
|
||||||
|
result.ErrorTitle = "Could not execute script"
|
||||||
|
result.ErrorDescription = err.Error()
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
result.Status = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
os.Remove(fname)
|
||||||
|
result.Output = string(stdout)
|
||||||
|
return
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user