feat(cli): add options for user, home, and working directory

- Add `--run-as` option to specify user for system-wide timers
- Add `--home` option to set the HOME environment variable
- Add `--cwd` option to define the working directory
- Update tests to validate new options and behavior
This commit is contained in:
2025-05-28 14:24:28 +02:00
parent f112002249
commit 113103f368
4 changed files with 86 additions and 6 deletions

View File

@@ -51,14 +51,18 @@ export function generateUnits(name: string, options: TimerOptions): {
`[Service]`,
`Type=oneshot`,
`ExecStart=${options.exec}`,
...(options.cwd ? [`WorkingDirectory=${options.cwd}`] : []),
...(options.environment?.map((e) => `Environment=${e}`) ?? []),
...(options.home ? [`Environment=HOME=${options.home}`] : []),
...(options.logfile
? [
`StandardOutput=append:${options.logfile}`,
`StandardError=append:${options.logfile}`,
]
: []),
...(options.runAs && !options.user ? [`User=${options.runAs}`] : []),
];
if (options.logfile) {
unitParts.push(`StandardOutput=append:${options.logfile}`);
unitParts.push(`StandardError=append:${options.logfile}`);
}
const serviceUnit = unitParts.join('\n');
const timerParts = [
@@ -70,7 +74,7 @@ export function generateUnits(name: string, options: TimerOptions): {
`Persistent=true`,
``,
`[Install]`,
`WantedBy=timers.target`,
`WantedBy=${options.user ? 'default.target' : 'timers.target'}`,
];
const timerUnit = timerParts.join('\n');