refactor(cli): integrate i18n support across commands

- Centralize CLI text strings using the i18n module for localization
- Refactor `createCommand` and `createCli` to improve modularity
- Update logging and error messages to use translated strings
This commit is contained in:
2025-05-28 18:09:52 +02:00
parent 8efbee1ba9
commit 2a13ee2539
6 changed files with 73 additions and 61 deletions

View File

@@ -1,51 +1,54 @@
import { Command } from '@cliffy/command';
import { generateUnitFiles } from '../templates/unit-generator.ts';
import { TimerOptions } from '../types/options.ts';
import { t } from '../i18n/mod.ts';
export const createCommand = new Command()
.description('Erzeugt eine systemd .service und .timer Unit')
.option(
'--name <name:string>',
'Name der Unit-Dateien (optional, wird sonst aus dem Exec generiert)',
)
.option(
'--exec <cmd:string>',
'Kommando, das durch systemd ausgeführt werden soll',
{ required: true },
)
.option('--calendar <time:string>', 'OnCalendar-Ausdruck für den Timer', {
required: true,
})
.option('--description <desc:string>', 'Beschreibung des Timers')
.option('--user', 'Erstellt die Unit als User-Timer')
.option(
'--run-as <user:string>',
'Führe den systemweiten Timer als bestimmter Benutzer aus (setzt User= in der Service-Unit)',
)
.option(
'--home <path:string>',
'HOME-Variable für den Service setzen',
)
.option(
'--cwd <path:string>',
'Arbeitsverzeichnis (WorkingDirectory) für den Service-Prozess',
)
.option('--output <dir:string>', 'Zielverzeichnis der Unit-Dateien')
.option(
'--after <target:string>',
'Optionales After= für die Service-Unit',
{ collect: true },
)
.option(
'--environment <env:string>',
'Environment-Variablen im Format KEY=VALUE',
{ collect: true },
)
.option(
'--logfile <file:string>',
'Dateipfad für Log-Ausgabe (stdout/stderr)',
)
.option('--dry-run', 'Gibt die Unit-Dateien nur aus, ohne sie zu schreiben')
.action(async (options: TimerOptions) => {
await generateUnitFiles(options);
});
export function createCommand() {
return new Command()
.description(t('cli_create_description'))
.option(
'--name <name:string>',
t('option_name'),
)
.option(
'--exec <cmd:string>',
t('option_exec'),
{ required: true },
)
.option('--calendar <time:string>', t('option_calendar'), {
required: true,
})
.option('--description <desc:string>', t('option_description'))
.option('--user', t('option_user'))
.option(
'--run-as <user:string>',
t('option_run_as'),
)
.option(
'--home <path:string>',
t('option_home'),
)
.option(
'--cwd <path:string>',
t('option_cwd'),
)
.option('--output <dir:string>', t('option_output'))
.option(
'--after <target:string>',
t('option_after'),
{ collect: true },
)
.option(
'--environment <env:string>',
t('option_environment'),
{ collect: true },
)
.option(
'--logfile <file:string>',
t('option_logfile'),
)
.option('--dry-run', t('option_dry_run'))
.action(async (options: TimerOptions) => {
await generateUnitFiles(options);
});
}

View File

@@ -1,10 +1,12 @@
import { Command } from '@cliffy/command';
import { createCommand } from './create.ts';
import { getVersion } from '../utils/mod.ts';
import { t } from '../i18n/mod.ts';
import { createCommand } from './mod.ts';
await new Command()
.name('systemd-timer')
.version(await getVersion())
.description('CLI Tool zum Erzeugen von systemd .timer und .service Units')
.command('create', createCommand)
.parse(Deno.args);
export async function createCli() {
return new Command()
.name('systemd-timer')
.version(await getVersion())
.description(t('cli_description'))
.command('create', createCommand());
}

2
src/cli/mod.ts Normal file
View File

@@ -0,0 +1,2 @@
export { createCommand } from './create.ts';
export { createCli } from './main.ts';

View File

@@ -1,5 +1,8 @@
import './cli/main.ts';
import { createCli } from './cli/mod.ts';
import { initI18n } from './i18n/mod.ts';
// ────────────────────────────────────────────────
// Entry Point for CLI
// Delegates to src/cli/main.ts, which registers all CLI commands
await initI18n();
await (await createCli()).parse(Deno.args);

View File

@@ -1,3 +1,4 @@
import { t } from '../i18n/mod.ts';
import { TimerOptions } from '../types/mod.ts';
import { deriveNameFromExec, writeUnitFiles } from '../utils/mod.ts';
@@ -21,13 +22,13 @@ export async function generateUnitFiles(options: TimerOptions): Promise<void> {
if (result) {
const { servicePath, timerPath } = result;
console.log(`Service Unit geschrieben in: ${servicePath}`);
console.log(`Timer Unit geschrieben in: ${timerPath}`);
console.log(t('unit_written_service', { path: servicePath }));
console.log(t('unit_written_timer', { path: timerPath }));
} else {
return;
}
console.log(`\nℹ️ Hinweis:`);
console.log(t('hint_header'));
if (options.user) {
console.log(` systemctl --user daemon-reload`);

View File

@@ -1,6 +1,7 @@
import { ensureDir, exists } from 'https://deno.land/std@0.224.0/fs/mod.ts';
import { join } from 'https://deno.land/std@0.224.0/path/mod.ts';
import { TimerOptions } from '../types/mod.ts';
import { t } from '../i18n/mod.ts';
export async function writeUnitFiles(
name: string,
@@ -28,9 +29,9 @@ export async function writeUnitFiles(
await Deno.remove(timerPath);
}
} catch (rollbackError) {
console.error('Rollback fehlgeschlagen:', rollbackError);
console.error(t('rollback_failed'), rollbackError);
}
console.error('Fehler beim Schreiben der Units:', error);
console.error(t('error_write_units'), error);
return undefined;
}