Files
xen-orchestra/@xen-orchestra/backups-cli/_composeCommands.mjs
2022-11-17 10:44:48 +01:00

35 lines
903 B
JavaScript

import { readFileSync } from 'fs'
import getopts from 'getopts'
const { version } = JSON.parse(readFileSync(new URL('package.json', import.meta.url)))
export function composeCommands(commands) {
return async function (args, prefix) {
const opts = getopts(args, {
alias: {
help: 'h',
},
boolean: ['help'],
stopEarly: true,
})
const commandName = opts.help || args.length === 0 ? 'help' : args[0]
const command = commands[commandName]
if (command === undefined) {
process.stdout.write(`Usage:
${Object.keys(commands)
.filter(command => command !== 'help')
.map(command => ` ${prefix} ${command} ${commands[command].usage || ''}`)
.join('\n\n')}
xo-backups v${version}
`)
process.exitCode = commandName === 'help' ? 0 : 1
return
}
return (await command.default)(args.slice(1), prefix + ' ' + commandName)
}
}