From 1b12910fb8cf97cc146ace4e5b3f8aef357640f5 Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Tue, 21 Mar 2023 13:52:02 +0100 Subject: [PATCH 1/5] feat: upload env variables --- package-lock.json | 9 ++ package.json | 1 + .../function-[function]/settings/+page.svelte | 16 ++-- .../function-[function]/settings/store.ts | 8 ++ .../settings/uploadVariables.svelte | 83 +++++++++---------- 5 files changed, 68 insertions(+), 49 deletions(-) create mode 100644 src/routes/console/project-[project]/functions/function-[function]/settings/store.ts diff --git a/package-lock.json b/package-lock.json index 83761e17f..020704b73 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15,6 +15,7 @@ "@sentry/svelte": "^7.36.0", "@sentry/tracing": "^7.36.0", "analytics": "^0.8.1", + "dotenv": "^16.0.3", "echarts": "^5.4.1", "logrocket": "^3.0.1", "pretty-bytes": "^6.1.0", @@ -3613,6 +3614,14 @@ "node": ">=12" } }, + "node_modules/dotenv": { + "version": "16.0.3", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.0.3.tgz", + "integrity": "sha512-7GO6HghkA5fYG9TYnNxi14/7K9f5occMlp3zXAuSxn7CKCxt9xbNWG7yF8hTCSUchlfWSe3uLmlPfigevRItzQ==", + "engines": { + "node": ">=12" + } + }, "node_modules/eastasianwidth": { "version": "0.2.0", "resolved": "https://registry.npmjs.org/eastasianwidth/-/eastasianwidth-0.2.0.tgz", diff --git a/package.json b/package.json index 0d0865856..bb39c6b4f 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,7 @@ "@sentry/svelte": "^7.36.0", "@sentry/tracing": "^7.36.0", "analytics": "^0.8.1", + "dotenv": "^16.0.3", "echarts": "^5.4.1", "logrocket": "^3.0.1", "pretty-bytes": "^6.1.0", diff --git a/src/routes/console/project-[project]/functions/function-[function]/settings/+page.svelte b/src/routes/console/project-[project]/functions/function-[function]/settings/+page.svelte index 475de53f1..09a4ea17d 100644 --- a/src/routes/console/project-[project]/functions/function-[function]/settings/+page.svelte +++ b/src/routes/console/project-[project]/functions/function-[function]/settings/+page.svelte @@ -18,7 +18,7 @@ import { onMount } from 'svelte'; import Variable from '../../createVariable.svelte'; import { execute, func } from '../store'; - // import Upload from './uploadVariables.svelte'; + import UploadVariables from './uploadVariables.svelte'; import { Table, TableBody, @@ -36,7 +36,7 @@ const functionId = $page.params.function; let showDelete = false; let selectedVar: Models.Variable = null; - // let showVariablesUpload = false; + let showVariablesUpload = false; let showVariablesModal = false; let showVariablesDropdown = []; let timeout: number = null; @@ -369,10 +369,10 @@ Download .env file - + {#if data.variables.total}
@@ -446,8 +446,9 @@
{:else} - (showVariablesModal = !showVariablesModal)} - >Create a variable to get started + (showVariablesModal = !showVariablesModal)}> + Create a variable to get started + {/if} @@ -505,3 +506,6 @@ on:created={handleVariableCreated} on:updated={handleVariableUpdated} /> {/if} +{#if showVariablesUpload} + +{/if} diff --git a/src/routes/console/project-[project]/functions/function-[function]/settings/store.ts b/src/routes/console/project-[project]/functions/function-[function]/settings/store.ts new file mode 100644 index 000000000..3f8cf0cea --- /dev/null +++ b/src/routes/console/project-[project]/functions/function-[function]/settings/store.ts @@ -0,0 +1,8 @@ +import { page } from '$app/stores'; +import { derived } from 'svelte/store'; +import type { Models } from '@aw-labs/appwrite-console'; + +export const variables = derived( + page, + ($page) => $page.data.variables.variables as Models.Variable[] +); diff --git a/src/routes/console/project-[project]/functions/function-[function]/settings/uploadVariables.svelte b/src/routes/console/project-[project]/functions/function-[function]/settings/uploadVariables.svelte index 617f40b46..d5caa07e7 100644 --- a/src/routes/console/project-[project]/functions/function-[function]/settings/uploadVariables.svelte +++ b/src/routes/console/project-[project]/functions/function-[function]/settings/uploadVariables.svelte @@ -6,69 +6,66 @@ import { Button, InputFile } from '$lib/elements/forms'; import { addNotification } from '$lib/stores/notifications'; import { sdk } from '$lib/stores/sdk'; - import { createEventDispatcher } from 'svelte'; + import { parse } from 'dotenv'; + import { variables } from './store'; export let show = false; - const dispatch = createEventDispatcher(); - const functionId = $page.params.function; + let files: FileList; + let error: string; - const handleSubmit = async () => { - if (files?.length) { - const variables = await parseFile(files[0]); - for (const variable of variables) { - try { - await sdk.forProject.functions.createVariable( - functionId, - variable.key, - variable.value - ); - invalidate(Dependencies.VARIABLES); - addNotification({ - type: 'success', - message: 'Variable uploaded' - }); - } catch (error) { - addNotification({ - type: 'error', - message: error.message - }); - } + async function handleSubmit() { + try { + if (!files?.length) { + throw new Error('No file selected'); } - dispatch('uploaded', variables); - } else { - addNotification({ - type: 'error', - message: 'No file uploaded' - }); - } - }; - async function parseFile(file: File) { - if (file) { - let variables = []; - let text = await file.text(); - text.split('\n').forEach((line) => { - const [key, value] = line.split('='); - variables.push({ key, value }); + const uploaded = parse(await files[0].text()); + + if (!Object.keys(uploaded).length) { + throw new Error('No variables found'); + } + + await Promise.all( + Object.entries(uploaded) + .filter(([, value]) => !!value) + .map(([key, value]) => { + const found = $variables.find((variable) => variable.key === key); + return found + ? sdk.forProject.functions.updateVariable( + functionId, + found.$id, + key, + value + ) + : sdk.forProject.functions.createVariable(functionId, key, value); + }) + ); + + invalidate(Dependencies.VARIABLES); + addNotification({ + type: 'success', + message: 'Variables uploaded' }); - return variables; + show = false; + } catch (e) { + error = e.message; } } - + Upload Variables

Upload multiple variables via a .env file that will be passed to your function at runtime.

- + - +
From b16b8e0a718904d6c15d3dea3e4e39a98dc558d0 Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Tue, 21 Mar 2023 14:58:21 +0100 Subject: [PATCH 2/5] fix: variables upload with more than 8192 chars --- .../settings/uploadVariables.svelte | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/routes/console/project-[project]/functions/function-[function]/settings/uploadVariables.svelte b/src/routes/console/project-[project]/functions/function-[function]/settings/uploadVariables.svelte index d5caa07e7..acec944d7 100644 --- a/src/routes/console/project-[project]/functions/function-[function]/settings/uploadVariables.svelte +++ b/src/routes/console/project-[project]/functions/function-[function]/settings/uploadVariables.svelte @@ -28,8 +28,16 @@ throw new Error('No variables found'); } + const entries = Object.entries(uploaded); + + for (const [key, value] of entries) { + if (value.length > 8192) { + throw new Error(`Variable ${key} is longer than 8192 allowed characters`); + } + } + await Promise.all( - Object.entries(uploaded) + entries .filter(([, value]) => !!value) .map(([key, value]) => { const found = $variables.find((variable) => variable.key === key); From dffe268dbf95d0e5256f6fda979701ecc0b7ff9c Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Tue, 21 Mar 2023 15:17:12 +0100 Subject: [PATCH 3/5] remove max-size --- .../function-[function]/settings/uploadVariables.svelte | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/routes/console/project-[project]/functions/function-[function]/settings/uploadVariables.svelte b/src/routes/console/project-[project]/functions/function-[function]/settings/uploadVariables.svelte index acec944d7..3da9fbf52 100644 --- a/src/routes/console/project-[project]/functions/function-[function]/settings/uploadVariables.svelte +++ b/src/routes/console/project-[project]/functions/function-[function]/settings/uploadVariables.svelte @@ -70,7 +70,7 @@ Upload multiple variables via a .env file that will be passed to your function at runtime.

- + From 9a9e8747e32565d044de4fb0eca94bb2bc9c850c Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Tue, 21 Mar 2023 15:29:04 +0100 Subject: [PATCH 4/5] fix: force pointer cursor on copy --- src/lib/components/copy.svelte | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/lib/components/copy.svelte b/src/lib/components/copy.svelte index c6cc8fe7d..33050e82e 100644 --- a/src/lib/components/copy.svelte +++ b/src/lib/components/copy.svelte @@ -33,6 +33,8 @@ setTimeout(() => (content = 'Click to copy'))} From a0b617284424a31021a7bb5a47e79d67173093e0 Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Wed, 22 Mar 2023 12:30:12 +0100 Subject: [PATCH 5/5] feat: pagination for func vars --- src/lib/components/output.svelte | 16 +++++---- .../function-[function]/settings/+page.svelte | 33 ++++++++++++------- .../settings/uploadVariables.svelte | 2 +- 3 files changed, 32 insertions(+), 19 deletions(-) diff --git a/src/lib/components/output.svelte b/src/lib/components/output.svelte index e8830076c..ff658d7d5 100644 --- a/src/lib/components/output.svelte +++ b/src/lib/components/output.svelte @@ -2,16 +2,18 @@ import Copy from './copy.svelte'; export let value: string; + export let hideCopyIcon = false; -
+
- -
- -
+ {#if !hideCopyIcon} +
+ +
+ {/if}
diff --git a/src/routes/console/project-[project]/functions/function-[function]/settings/+page.svelte b/src/routes/console/project-[project]/functions/function-[function]/settings/+page.svelte index 09a4ea17d..dc2f755b2 100644 --- a/src/routes/console/project-[project]/functions/function-[function]/settings/+page.svelte +++ b/src/routes/console/project-[project]/functions/function-[function]/settings/+page.svelte @@ -3,7 +3,16 @@ import { base } from '$app/paths'; import { page } from '$app/stores'; import { Submit, trackEvent, trackError } from '$lib/actions/analytics'; - import { Box, CardGrid, DropList, DropListItem, Empty, Output, Secret } from '$lib/components'; + import { + Box, + CardGrid, + DropList, + DropListItem, + Empty, + Output, + PaginationInline, + Secret + } from '$lib/components'; import Heading from '$lib/components/heading.svelte'; import { Roles } from '$lib/components/permissions'; import { Dependencies } from '$lib/constants'; @@ -44,6 +53,7 @@ let functionSchedule: string = null; let permissions: string[] = []; let arePermsDisabled = true; + let offset = 0; onMount(async () => { timeout ??= $func.timeout; @@ -374,19 +384,21 @@ Import .env file
- {#if data.variables.total} + {@const limit = 10} + {@const sum = data.variables.total} + {#if sum}
Key - Value + Value - {#each data.variables.variables as variable, i} + {#each data.variables.variables.slice(offset, offset + limit) as variable, i} - + {variable.key} @@ -435,15 +447,14 @@ {/each}
- +
+

Total variables: {sum}

+ +
{:else} (showVariablesModal = !showVariablesModal)}> diff --git a/src/routes/console/project-[project]/functions/function-[function]/settings/uploadVariables.svelte b/src/routes/console/project-[project]/functions/function-[function]/settings/uploadVariables.svelte index 3da9fbf52..974932a16 100644 --- a/src/routes/console/project-[project]/functions/function-[function]/settings/uploadVariables.svelte +++ b/src/routes/console/project-[project]/functions/function-[function]/settings/uploadVariables.svelte @@ -70,7 +70,7 @@ Upload multiple variables via a .env file that will be passed to your function at runtime.

- +