Merge pull request #351 from appwrite/fix-env-upload

feat: upload env variables
This commit is contained in:
Torsten Dittmann authored and GitHub committed 2023-03-23 13:57:06 +01:00
commit 8fbdd25b40
7 files changed
+109 -66

No files matched your search

+9
View File
@@ -15,6 +15,7 @@
"@sentry/svelte": "^7.44.2",
"@sentry/tracing": "^7.44.2",
"analytics": "^0.8.1",
"dotenv": "^16.0.3",
"echarts": "^5.4.1",
"logrocket": "^3.0.1",
"pretty-bytes": "^6.1.0",
@@ -3658,6 +3659,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",
+1
View File
@@ -26,6 +26,7 @@
"@sentry/svelte": "^7.44.2",
"@sentry/tracing": "^7.44.2",
"analytics": "^0.8.1",
"dotenv": "^16.0.3",
"echarts": "^5.4.1",
"logrocket": "^3.0.1",
"pretty-bytes": "^6.1.0",
+2
View File
@@ -33,6 +33,8 @@
</script>
<span
style:display="inline-block"
style:cursor="pointer"
on:click|preventDefault={handleClick}
on:keyup={clickOnEnter}
on:mouseenter={() => setTimeout(() => (content = 'Click to copy'))}
+9 -7
View File
@@ -2,16 +2,18 @@
import Copy from './copy.svelte';
export let value: string;
export let hideCopyIcon = false;
</script>
<Copy {value}>
<div class="interactive-text-output is-textarea">
<div class="interactive-text-output is-textarea" style:min-inline-size="0">
<span class="text u-line-height-1-5 u-break-all"><slot /></span>
<div class="u-flex u-cross-child-start u-gap-8">
<button class="interactive-text-output-button" aria-label="copy text">
<span class="icon-duplicate" aria-hidden="true" />
</button>
</div>
{#if !hideCopyIcon}
<div class="u-flex u-cross-child-start u-gap-8">
<button class="interactive-text-output-button" aria-label="copy text">
<span class="icon-duplicate" aria-hidden="true" />
</button>
</div>
{/if}
</div>
</Copy>
@@ -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';
@@ -18,6 +27,7 @@
import { onMount } from 'svelte';
import Variable from '../../createVariable.svelte';
import { execute, func } from '../store';
import UploadVariables from './uploadVariables.svelte';
import {
Table,
TableBody,
@@ -35,7 +45,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;
@@ -43,6 +53,7 @@
let functionSchedule: string = null;
let permissions: string[] = [];
let arePermsDisabled = true;
let offset = 0;
onMount(async () => {
timeout ??= $func.timeout;
@@ -368,24 +379,26 @@
<span class="icon-download" />
<span class="text">Download .env file</span>
</Button>
<!-- <Button secondary on:click={() => (showVariablesUpload = true)}>
<Button secondary on:click={() => (showVariablesUpload = true)}>
<span class="icon-upload" />
<span class="text">Import .env file</span>
</Button> -->
</Button>
</div>
{#if data.variables.total}
{@const limit = 10}
{@const sum = data.variables.total}
{#if sum}
<div class="u-flex u-flex-vertical u-gap-16">
<Table noMargin noStyles>
<TableHeader>
<TableCellHead>Key</TableCellHead>
<TableCellHead>Value</TableCellHead>
<TableCellHead width={180}>Value</TableCellHead>
<TableCellHead width={30} />
</TableHeader>
<TableBody>
{#each data.variables.variables as variable, i}
{#each data.variables.variables.slice(offset, offset + limit) as variable, i}
<TableRow>
<TableCell title="key">
<Output value={variable.key}>
<Output value={variable.key} hideCopyIcon>
{variable.key}
</Output>
</TableCell>
@@ -434,19 +447,19 @@
{/each}
</TableBody>
</Table>
<Button
text
noMargin
on:click={() => {
showVariablesModal = true;
}}>
<Button text noMargin on:click={() => (showVariablesModal = true)}>
<span class="icon-plus" aria-hidden="true" />
<span class="text">Create variable</span>
</Button>
<div class="u-flex u-main-space-between">
<p class="text">Total variables: {sum}</p>
<PaginationInline {sum} {limit} bind:offset hidePages />
</div>
</div>
{:else}
<Empty on:click={() => (showVariablesModal = !showVariablesModal)}
>Create a variable to get started</Empty>
<Empty on:click={() => (showVariablesModal = !showVariablesModal)}>
Create a variable to get started
</Empty>
{/if}
</svelte:fragment>
</CardGrid>
@@ -504,3 +517,6 @@
on:created={handleVariableCreated}
on:updated={handleVariableUpdated} />
{/if}
{#if showVariablesUpload}
<UploadVariables bind:show={showVariablesUpload} />
{/if}
@@ -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[]
);
@@ -6,69 +6,74 @@
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;
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
});
let files: FileList;
let error: string;
async function handleSubmit() {
try {
if (!files?.length) {
throw new Error('No file selected');
}
const uploaded = parse(await files[0].text());
if (!Object.keys(uploaded).length) {
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`);
}
}
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 });
await Promise.all(
entries
.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;
}
}
</script>
<Modal bind:show onSubmit={handleSubmit}>
<Modal bind:show onSubmit={handleSubmit} bind:error>
<svelte:fragment slot="header">Upload Variables</svelte:fragment>
<p>
Upload multiple variables via a .env file that will be passed to your function at runtime.
</p>
<InputFile bind:files label="Attach a file" />
<InputFile bind:files />
<svelte:fragment slot="footer">
<Button text on:click={() => (show = false)}>Cancel</Button>
<Button submit>Create</Button>
<Button submit>Upload</Button>
</svelte:fragment>
</Modal>