From ef4b2b7ce5783099e6b1bec89e26ad7527c6988d Mon Sep 17 00:00:00 2001 From: levg34 Date: Fri, 3 Jun 2022 23:12:47 +0200 Subject: [PATCH] delete and rename config (wip) (back OK) --- components/ConfigList.tsx | 85 ++++++++++++++++++++++++++++++++++++++ pages/api/config/delete.ts | 33 +++++++++++++++ pages/api/config/rename.ts | 33 +++++++++++++++ 3 files changed, 151 insertions(+) create mode 100644 components/ConfigList.tsx create mode 100644 pages/api/config/delete.ts create mode 100644 pages/api/config/rename.ts diff --git a/components/ConfigList.tsx b/components/ConfigList.tsx new file mode 100644 index 0000000..fd2fc18 --- /dev/null +++ b/components/ConfigList.tsx @@ -0,0 +1,85 @@ +import { Container, ListGroup, Button, Stack } from 'react-bootstrap' +import useSWR from 'swr' +import { IconAlertTriangle } from '@tabler/icons' +import { fetcher } from '@utils/swrUtils' +import { ConfigList } from '@api/config/list' + +const deleteConfig = async (configFile: string) => { + // setFeedback(undefined) + try { + const response = await fetcher('/api/config/delete', { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + redirect: 'follow', + body: JSON.stringify({ + configFile + }) + }) + console.log(response) + // setFeedback({ + // text: `Deleted resource from server: ${file}`, + // variant: 'success' + // }) + } catch (error) { + console.error(error) + // setFeedback({ + // text: 'Failed to delete resource from server: '+String(error), + // variant: 'danger' + // }) + } finally { + // getResources() + } +} + +const renameConfig = async (configFile: string, newName: string) => { + // if (!renaming) return + try { + const response = await fetcher('/api/config/rename', { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + redirect: 'follow', + body: JSON.stringify({ + configFile, + newName + }) + }) + console.log(response) + // setFeedback({ + // text: `Renamed resource on server: ${renaming.file} --> ${renaming.newName}`, + // variant: 'success' + // }) + // setRenaming(undefined) + } catch (error) { + console.error(error) + // setFeedback({ + // text: 'Failed to delete resource from server: '+String(error), + // variant: 'danger' + // }) + } finally { + // getResources() + } +} + +const ConfigDisplay = ({config}: {config: string}) => + + {config} + + + + + +const ConfigList = () => { + const {data: configList} = useSWR('/api/config/list',fetcher) + return +

Manage configs

+ + {configList && configList.files.map(config => )} + +
+} + +export default ConfigList diff --git a/pages/api/config/delete.ts b/pages/api/config/delete.ts new file mode 100644 index 0000000..c081dec --- /dev/null +++ b/pages/api/config/delete.ts @@ -0,0 +1,33 @@ +import type { NextApiRequest, NextApiResponse } from 'next' +import { SSHExecCommandResponse } from 'node-ssh' +import { getSession } from 'next-auth/react' +import { getLogger } from '@utils/loggerUtils' +import { getSSHClient } from '@utils/sshUtils' + +import usersConfig from '@config/usersConfig.json' + +const logger = getLogger('delete-config.ts') + +export default async function handler( + req: NextApiRequest, + res: NextApiResponse +) { + try { + const session = await getSession({ req }) + if (!session) return res.status(401).json({error: 'Unauthorized'}) + if (!session.user?.email || !usersConfig.admins.includes(session.user?.email)) return res.status(403).json({error: 'Forbidden'}) + + const sshClient = await getSSHClient() + + const { configFile } = req.body + + const response = await sshClient.execCommand(`cd beammp-server; rm ${configFile}`) + + logger.info({response, configFile, user: session.user.email}, 'delete config') + + res.status(200).json(response) + } catch (error) { + logger.error(error) + res.status(500).json({error}) + } +} diff --git a/pages/api/config/rename.ts b/pages/api/config/rename.ts new file mode 100644 index 0000000..6b82203 --- /dev/null +++ b/pages/api/config/rename.ts @@ -0,0 +1,33 @@ +import type { NextApiRequest, NextApiResponse } from 'next' +import { SSHExecCommandResponse } from 'node-ssh' +import { getSession } from 'next-auth/react' +import { getLogger } from '@utils/loggerUtils' +import { getSSHClient } from '@utils/sshUtils' + +import usersConfig from '@config/usersConfig.json' + +const logger = getLogger('rename-config.ts') + +export default async function handler( + req: NextApiRequest, + res: NextApiResponse +) { + try { + const session = await getSession({ req }) + if (!session) return res.status(401).json({error: 'Unauthorized'}) + if (!session.user?.email || !usersConfig.admins.includes(session.user?.email)) return res.status(403).json({error: 'Forbidden'}) + + const sshClient = await getSSHClient() + + const { configFile, newName } = req.body + + const response = await sshClient.execCommand(`cd beammp-server; mv ${configFile} ${newName}`) + + logger.info({response, configFile, newName, user: session.user.email}, 'rename config') + + res.status(200).json(response) + } catch (error) { + logger.error(error) + res.status(500).json({error}) + } +}