feat(cli): add validation for command options

This commit is contained in:
2025-05-30 12:42:37 +02:00
parent 3d95706d68
commit 1c07af402b

View File

@@ -2,6 +2,14 @@ import { Command } from '@cliffy/command';
import { generateUnitFiles } from '../templates/unit-generator.ts'; import { generateUnitFiles } from '../templates/unit-generator.ts';
import { TimerOptions } from '../types/options.ts'; import { TimerOptions } from '../types/options.ts';
import { t } from '../i18n/mod.ts'; import { t } from '../i18n/mod.ts';
import {
collectAndValidateAfter,
collectAndValidateEnv,
validateIdentifier,
validateNotEmpty,
validatePath,
validateSystemdCalendar,
} from '../utils/mod.ts';
export function createCommand() { export function createCommand() {
return new Command() return new Command()
@@ -9,43 +17,54 @@ export function createCommand() {
.option( .option(
'--name <name:string>', '--name <name:string>',
t('option_name'), t('option_name'),
{ value: (v) => validateIdentifier(v, '--name') },
) )
.option( .option(
'--exec <cmd:string>', '--exec <cmd:string>',
t('option_exec'), t('option_exec'),
{ required: true }, {
required: true,
value: (v) => validateNotEmpty(v, '--exec'),
},
) )
.option('--calendar <time:string>', t('option_calendar'), { .option('--calendar <time:string>', t('option_calendar'), {
required: true, required: true,
value: validateSystemdCalendar,
}) })
.option('--description <desc:string>', t('option_description')) .option('--description <desc:string>', t('option_description'))
.option('--user', t('option_user')) .option('--user', t('option_user'))
.option( .option(
'--run-as <user:string>', '--run-as <user:string>',
t('option_run_as'), t('option_run_as'),
{ value: (v) => validateNotEmpty(v, '--run-as') },
) )
.option( .option(
'--home <path:string>', '--home <path:string>',
t('option_home'), t('option_home'),
{ value: (v) => validatePath(v, true) },
) )
.option( .option(
'--cwd <path:string>', '--cwd <path:string>',
t('option_cwd'), t('option_cwd'),
{ value: (v) => validatePath(v, true) },
) )
.option('--output <dir:string>', t('option_output')) .option('--output <dir:string>', t('option_output'), {
value: (v) => validatePath(v, false),
})
.option( .option(
'--after <target:string>', '--after <target:string>',
t('option_after'), t('option_after'),
{ collect: true }, { collect: true, value: collectAndValidateAfter },
) )
.option( .option(
'--environment <env:string>', '--environment <env:string>',
t('option_environment'), t('option_environment'),
{ collect: true }, { collect: true, value: collectAndValidateEnv },
) )
.option( .option(
'--logfile <file:string>', '--logfile <file:string>',
t('option_logfile'), t('option_logfile'),
{ value: (v) => validatePath(v, false) },
) )
.option('--dry-run', t('option_dry_run')) .option('--dry-run', t('option_dry_run'))
.action(async (options: TimerOptions) => { .action(async (options: TimerOptions) => {