diff --git a/frontend/src/lib/stores/hosttree.js b/frontend/src/lib/stores/hosttree.js index 5806cab..082902f 100644 --- a/frontend/src/lib/stores/hosttree.js +++ b/frontend/src/lib/stores/hosttree.js @@ -283,8 +283,18 @@ async function refresh() { await database.open(); } }; + + database.executeShellScript = async function(script) { + const result = await ExecuteShellScript(hostKey, dbKey, '', script); + return result; + }; } + host.executeShellScript = async function(script) { + const result = await ExecuteShellScript(hostKey, '', '', script); + return result; + }; + 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.', ''); if (name) { diff --git a/frontend/src/organisms/connection/collection/index.svelte b/frontend/src/organisms/connection/collection/index.svelte index db6165f..5b0f282 100644 --- a/frontend/src/organisms/connection/collection/index.svelte +++ b/frontend/src/organisms/connection/collection/index.svelte @@ -9,7 +9,7 @@ import Indexes from './indexes.svelte'; import Insert from './insert.svelte'; import Remove from './remove.svelte'; - import Shell from './shell.svelte'; + import Shell from '../shell.svelte'; import Stats from './stats.svelte'; import Update from './update.svelte'; diff --git a/frontend/src/organisms/connection/database/index.svelte b/frontend/src/organisms/connection/database/index.svelte index 982ba12..39cf905 100644 --- a/frontend/src/organisms/connection/database/index.svelte +++ b/frontend/src/organisms/connection/database/index.svelte @@ -2,6 +2,8 @@ import BlankState from '$components/blankstate.svelte'; import TabBar from '$components/tabbar.svelte'; import { EventsOn } from '$wails/runtime/runtime'; + + import Shell from '../shell.svelte'; import Stats from './stats.svelte'; export let database; @@ -25,9 +27,15 @@
{#if database} {#key database} - +
{#if tab === 'stats'} + {:else if tab === 'shell'} {/if}
{/key} diff --git a/frontend/src/organisms/connection/host/index.svelte b/frontend/src/organisms/connection/host/index.svelte index de71c01..db2203d 100644 --- a/frontend/src/organisms/connection/host/index.svelte +++ b/frontend/src/organisms/connection/host/index.svelte @@ -2,6 +2,8 @@ import BlankState from '$components/blankstate.svelte'; import TabBar from '$components/tabbar.svelte'; import { EventsOn } from '$wails/runtime/runtime'; + + import Shell from '../shell.svelte'; import Status from './status.svelte'; import SystemInfo from './systeminfo.svelte'; @@ -24,15 +26,19 @@
{#if host} {#key host} - +
{#if tab === 'status'} {:else if tab === 'systemInfo'} + {:else if tab === 'shell'} {/if}
{/key} diff --git a/frontend/src/organisms/connection/collection/shell.svelte b/frontend/src/organisms/connection/shell.svelte similarity index 73% rename from frontend/src/organisms/connection/collection/shell.svelte rename to frontend/src/organisms/connection/shell.svelte index b611e70..3e4d62f 100644 --- a/frontend/src/organisms/connection/collection/shell.svelte +++ b/frontend/src/organisms/connection/shell.svelte @@ -3,20 +3,34 @@ import CodeEditor from '$components/codeeditor.svelte'; import Icon from '$components/icon.svelte'; import { javascript } from '@codemirror/lang-javascript'; - import { onDestroy } from 'svelte'; + import { onDestroy, onMount } from 'svelte'; - export let collection; + 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; - result = await collection.executeShellScript(script); + + 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; } @@ -26,6 +40,22 @@ 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)); @@ -33,7 +63,7 @@
diff --git a/internal/app/collection_shell.go b/internal/app/host_shell.go similarity index 76% rename from internal/app/collection_shell.go rename to internal/app/host_shell.go index 10a62cb..88d10dc 100644 --- a/internal/app/collection_shell.go +++ b/internal/app/host_shell.go @@ -56,17 +56,27 @@ func (a *App) ExecuteShellScript(hostKey, dbKey, collKey, script string) (result return } - 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 + 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()) } - url.Path = "/" + dbKey - connstr := url.String() - script = fmt.Sprintf("db = connect('%s');\ncoll = db.getCollection('%s');\n\n%s", connstr, collKey, script) + 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()) @@ -75,7 +85,7 @@ func (a *App) ExecuteShellScript(hostKey, dbKey, collKey, script string) (result return } - cmd := exec.Command("mongosh", "--file", fname, connstr) + cmd := exec.Command("mongosh", "--file", fname, host.URI) stdout, err := cmd.Output() if exiterr, ok := err.(*exec.ExitError); ok {