From 2a13ee2539d96d161a9ee398629fa79822d856f2 Mon Sep 17 00:00:00 2001 From: "Max P." Date: Wed, 28 May 2025 18:09:52 +0200 Subject: [PATCH] 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 --- src/cli/create.ts | 97 +++++++++++++++++---------------- src/cli/main.ts | 16 +++--- src/cli/mod.ts | 2 + src/mod.ts | 7 ++- src/templates/unit-generator.ts | 7 ++- src/utils/fs.ts | 5 +- 6 files changed, 73 insertions(+), 61 deletions(-) create mode 100644 src/cli/mod.ts diff --git a/src/cli/create.ts b/src/cli/create.ts index bc8fa73..1a20e5f 100644 --- a/src/cli/create.ts +++ b/src/cli/create.ts @@ -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 der Unit-Dateien (optional, wird sonst aus dem Exec generiert)', - ) - .option( - '--exec ', - 'Kommando, das durch systemd ausgeführt werden soll', - { required: true }, - ) - .option('--calendar ', 'OnCalendar-Ausdruck für den Timer', { - required: true, - }) - .option('--description ', 'Beschreibung des Timers') - .option('--user', 'Erstellt die Unit als User-Timer') - .option( - '--run-as ', - 'Führe den systemweiten Timer als bestimmter Benutzer aus (setzt User= in der Service-Unit)', - ) - .option( - '--home ', - 'HOME-Variable für den Service setzen', - ) - .option( - '--cwd ', - 'Arbeitsverzeichnis (WorkingDirectory) für den Service-Prozess', - ) - .option('--output ', 'Zielverzeichnis der Unit-Dateien') - .option( - '--after ', - 'Optionales After= für die Service-Unit', - { collect: true }, - ) - .option( - '--environment ', - 'Environment-Variablen im Format KEY=VALUE', - { collect: true }, - ) - .option( - '--logfile ', - '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 ', + t('option_name'), + ) + .option( + '--exec ', + t('option_exec'), + { required: true }, + ) + .option('--calendar ', t('option_calendar'), { + required: true, + }) + .option('--description ', t('option_description')) + .option('--user', t('option_user')) + .option( + '--run-as ', + t('option_run_as'), + ) + .option( + '--home ', + t('option_home'), + ) + .option( + '--cwd ', + t('option_cwd'), + ) + .option('--output ', t('option_output')) + .option( + '--after ', + t('option_after'), + { collect: true }, + ) + .option( + '--environment ', + t('option_environment'), + { collect: true }, + ) + .option( + '--logfile ', + t('option_logfile'), + ) + .option('--dry-run', t('option_dry_run')) + .action(async (options: TimerOptions) => { + await generateUnitFiles(options); + }); +} diff --git a/src/cli/main.ts b/src/cli/main.ts index 64f3098..f77c9b7 100644 --- a/src/cli/main.ts +++ b/src/cli/main.ts @@ -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()); +} diff --git a/src/cli/mod.ts b/src/cli/mod.ts new file mode 100644 index 0000000..cad57b6 --- /dev/null +++ b/src/cli/mod.ts @@ -0,0 +1,2 @@ +export { createCommand } from './create.ts'; +export { createCli } from './main.ts'; diff --git a/src/mod.ts b/src/mod.ts index 992cd64..bf3ad9c 100644 --- a/src/mod.ts +++ b/src/mod.ts @@ -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); diff --git a/src/templates/unit-generator.ts b/src/templates/unit-generator.ts index 4f009e4..d5b4c7e 100644 --- a/src/templates/unit-generator.ts +++ b/src/templates/unit-generator.ts @@ -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 { 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`); diff --git a/src/utils/fs.ts b/src/utils/fs.ts index 7dcc7df..21e3aa6 100644 --- a/src/utils/fs.ts +++ b/src/utils/fs.ts @@ -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; }