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

View File

@@ -1,10 +1,12 @@
import { Command } from '@cliffy/command'; import { Command } from '@cliffy/command';
import { createCommand } from './create.ts';
import { getVersion } from '../utils/mod.ts'; import { getVersion } from '../utils/mod.ts';
import { t } from '../i18n/mod.ts';
import { createCommand } from './mod.ts';
await new Command() export async function createCli() {
.name('systemd-timer') return new Command()
.version(await getVersion()) .name('systemd-timer')
.description('CLI Tool zum Erzeugen von systemd .timer und .service Units') .version(await getVersion())
.command('create', createCommand) .description(t('cli_description'))
.parse(Deno.args); .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 // 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 { TimerOptions } from '../types/mod.ts';
import { deriveNameFromExec, writeUnitFiles } from '../utils/mod.ts'; import { deriveNameFromExec, writeUnitFiles } from '../utils/mod.ts';
@@ -21,13 +22,13 @@ export async function generateUnitFiles(options: TimerOptions): Promise<void> {
if (result) { if (result) {
const { servicePath, timerPath } = result; const { servicePath, timerPath } = result;
console.log(`Service Unit geschrieben in: ${servicePath}`); console.log(t('unit_written_service', { path: servicePath }));
console.log(`Timer Unit geschrieben in: ${timerPath}`); console.log(t('unit_written_timer', { path: timerPath }));
} else { } else {
return; return;
} }
console.log(`\nℹ️ Hinweis:`); console.log(t('hint_header'));
if (options.user) { if (options.user) {
console.log(` systemctl --user daemon-reload`); 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 { 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 { join } from 'https://deno.land/std@0.224.0/path/mod.ts';
import { TimerOptions } from '../types/mod.ts'; import { TimerOptions } from '../types/mod.ts';
import { t } from '../i18n/mod.ts';
export async function writeUnitFiles( export async function writeUnitFiles(
name: string, name: string,
@@ -28,9 +29,9 @@ export async function writeUnitFiles(
await Deno.remove(timerPath); await Deno.remove(timerPath);
} }
} catch (rollbackError) { } 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; return undefined;
} }