Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

delete and rename configs #10

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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})
}
}