-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
delete and rename config (wip) (back OK)
- Loading branch information
Showing
3 changed files
with
151 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}) => <ListGroup.Item> | ||
<Stack direction="horizontal" gap ={2}> | ||
{config} | ||
<Button variant="outline-secondary" className="ms-auto" onClick={() => renameConfig(config, 'ServerConfigRenamed.toml')}>Rename</Button> | ||
<Button variant="outline-danger" onClick={() => deleteConfig(config)}>Delete</Button> | ||
</Stack> | ||
</ListGroup.Item> | ||
|
||
const ConfigList = () => { | ||
const {data: configList} = useSWR<ConfigList>('/api/config/list',fetcher) | ||
return <Container> | ||
<h2><IconAlertTriangle/> Manage configs</h2> | ||
<ListGroup> | ||
{configList && configList.files.map(config => <ConfigDisplay key={config} config={config}/>)} | ||
</ListGroup> | ||
</Container> | ||
} | ||
|
||
export default ConfigList |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<SSHExecCommandResponse | {error:any}> | ||
) { | ||
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}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<SSHExecCommandResponse | {error:any}> | ||
) { | ||
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}) | ||
} | ||
} |