From 3715114232cd9ebc461d9761e06c67e3d9437247 Mon Sep 17 00:00:00 2001 From: Mohammed Date: Fri, 30 Dec 2022 01:05:19 +0100 Subject: [PATCH] add ability to export secrets with comments --- frontend/pages/dashboard/[id].tsx | 132 ++++++++++++++++++++++++++---- 1 file changed, 116 insertions(+), 16 deletions(-) diff --git a/frontend/pages/dashboard/[id].tsx b/frontend/pages/dashboard/[id].tsx index 60fcc7a00..caf1ce82a 100644 --- a/frontend/pages/dashboard/[id].tsx +++ b/frontend/pages/dashboard/[id].tsx @@ -16,6 +16,8 @@ import { faPlus, } from '@fortawesome/free-solid-svg-icons'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; +import { Menu, Transition } from '@headlessui/react'; +import { Document, YAMLSeq } from 'yaml'; import Button from '~/components/basic/buttons/Button'; import ListBox from '~/components/basic/Listbox'; @@ -382,16 +384,75 @@ export default function Dashboard() { setData(sortedData); }; + // check if there are secrets with an override + const checkOverrides = (data: SecretDataProps[]) => { + let secrets : SecretDataProps[] = data!.map((secret) => Object.create(secret)); + const overridenSecrets = data!.filter( + (secret) => secret.type === 'personal' + ); + if (overridenSecrets.length) { + overridenSecrets.forEach((secret) => { + const index = secrets!.findIndex( + (_secret) => _secret.key === secret.key && _secret.type === 'shared' + ); + secrets![index].value = secret.value; + }); + secrets = secrets!.filter((secret) => secret.type === 'shared'); + } + return secrets; + }; // This function downloads the secrets as a .env file - const download = () => { - const file = data! - .map((item: SecretDataProps) => [item.key, item.value].join('=')) + const downloadDotEnv = () => { + if (!data) return; + const secrets = checkOverrides(data) + + const file = secrets! + .map( + (item: SecretDataProps) => + `${ + item.comment + ? item.comment + .split('\n') + .map((comment) => '# '.concat(comment)) + .join('\n') + '\n' + : '' + }` + [item.key, item.value].join('=') + ) .join('\n'); + + const blob = new Blob([file]); + const fileDownloadUrl = URL.createObjectURL(blob); + const alink = document.createElement('a'); + alink.href = fileDownloadUrl; + alink.download = envMapping[env] + '.env'; + alink.click(); + }; + + // This function downloads the secrets as a .yml file + const downloadYaml = () => { + if (!data) return; + const doc = new Document(new YAMLSeq()); + const secrets = checkOverrides(data); + secrets.forEach((secret) => { + const pair = doc.createNode({ [secret.key]: secret.value }); + pair.commentBefore = secret.comment + .split('\n') + .map((line) => (line ? ' '.concat(line) : '')) + .join('\n'); + doc.add(pair); + }); + + const file = doc + .toString() + .split('\n') + .map((line) => (line.startsWith('-') ? line.replace('- ', '') : line)) + .join('\n'); + const blob = new Blob([file]); const fileDownloadUrl = URL.createObjectURL(blob); const alink = document.createElement('a'); alink.href = fileDownloadUrl; - alink.download = envMapping[env] + '.env'; + alink.download = envMapping[env] + '.yml'; alink.click(); }; @@ -541,12 +602,49 @@ export default function Dashboard() { />
-