Skip to content

Commit

Permalink
delete and rename config (wip) (back OK)
Browse files Browse the repository at this point in the history
  • Loading branch information
levg34 committed Jun 3, 2022
1 parent ee3c495 commit ef4b2b7
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 0 deletions.
85 changes: 85 additions & 0 deletions components/ConfigList.tsx
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
33 changes: 33 additions & 0 deletions pages/api/config/delete.ts
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})
}
}
33 changes: 33 additions & 0 deletions pages/api/config/rename.ts
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})
}
}

0 comments on commit ef4b2b7

Please sign in to comment.