Skip to content

Commit abadf00

Browse files
committed
fix backup / restore
1 parent 8bcbbd1 commit abadf00

File tree

4 files changed

+55
-92
lines changed

4 files changed

+55
-92
lines changed

cmd/keymaster/main.go

Lines changed: 10 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -804,39 +804,8 @@ Example (Integrate):
804804
805805
Example (Full Restore):
806806
keymaster restore --full ./keymaster-backup-2025-10-26.json.zst`,
807-
Args: cobra.ExactArgs(1),
808-
PreRunE: func(cmd *cobra.Command, args []string) error {
809-
// Load optional config file argument from cli
810-
optional_config_path, err := getConfigPathFromCli(cmd)
811-
if err != nil {
812-
return err
813-
}
814-
815-
// Load config
816-
type Config struct {
817-
Database struct {
818-
Type string `mapstructure:"type"`
819-
Dsn string `mapstructure:"dsn"`
820-
} `mapstructure:"database"`
821-
Language string `mapstructure:"language"`
822-
}
823-
824-
defauls := map[string]any{
825-
"database.type": "sqlite",
826-
"database.dsn": "./keymaster.db",
827-
"language": "en",
828-
}
829-
830-
config, err := config.LoadConfig[Config](cmd, defauls, optional_config_path)
831-
if err != nil {
832-
return fmt.Errorf("error loading config: %w", err)
833-
}
834-
835-
// Initialize i18n
836-
i18n.Init(config.Language)
837-
838-
return nil
839-
},
807+
Args: cobra.ExactArgs(1),
808+
PreRunE: setupDefaultServices, // This was correct, just confirming.
840809
Run: func(cmd *cobra.Command, args []string) {
841810
inputFile := args[0]
842811

@@ -904,36 +873,9 @@ Examples:
904873
905874
# Backup to a specific file
906875
keymaster backup my-backup.json`, // .zst will be appended
907-
Args: cobra.MaximumNArgs(1),
908-
PreRunE: func(cmd *cobra.Command, args []string) error {
909-
// Load optional config file argument from cli
910-
optional_config_path, err := getConfigPathFromCli(cmd)
911-
if err != nil {
912-
return err
913-
}
914-
915-
// Load config
916-
type Config struct {
917-
Language string `mapstructure:"language"`
918-
}
919-
920-
defauls := map[string]any{
921-
"language": "en",
922-
}
923-
924-
config, err := config.LoadConfig[Config](cmd, defauls, optional_config_path)
925-
if err != nil {
926-
return fmt.Errorf("error loading config: %w", err)
927-
}
928-
929-
// Initialize i18n
930-
i18n.Init(config.Language)
931-
932-
return nil
933-
},
876+
Args: cobra.MaximumNArgs(1),
877+
PreRunE: setupDefaultServices,
934878
Run: func(cmd *cobra.Command, args []string) {
935-
i18n.Init(viper.GetString("language"))
936-
937879
var outputFile string
938880
if len(args) == 0 {
939881
outputFile = fmt.Sprintf("keymaster-backup-%s.json.zst", time.Now().Format("2006-01-02"))
@@ -1001,35 +943,11 @@ This command automates the following steps:
1001943
Example:
1002944
keymaster migrate --type postgres --dsn "host=localhost user=keymaster dbname=keymaster"`,
1003945
RunE: func(cmd *cobra.Command, args []string) error {
1004-
// Load optional config file argument from cli
1005-
optional_config_path, err := getConfigPathFromCli(cmd)
1006-
if err != nil {
1007-
return err
1008-
}
1009-
1010-
// Load config
1011-
type Config struct {
1012-
Database struct {
1013-
Type string `mapstructure:"type"`
1014-
Dsn string `mapstructure:"dsn"`
1015-
} `mapstructure:"database"`
1016-
Language string `mapstructure:"language"`
1017-
}
1018-
1019-
defauls := map[string]any{
1020-
// no database defaults! migrations have to be explicit
1021-
"language": "en",
1022-
}
1023-
1024-
config, err := config.LoadConfig[Config](cmd, defauls, optional_config_path)
1025-
if err != nil {
1026-
return fmt.Errorf("error loading config: %w", err)
1027-
}
1028-
1029-
// Initialize i18n
1030-
i18n.Init(config.Language)
946+
// Viper has already been configured by PreRunE, so we can directly access the values.
947+
targetType := viper.GetString("database.type")
948+
targetDsn := viper.GetString("database.dsn")
1031949

1032-
if config.Database.Type == "" || config.Database.Dsn == "" {
950+
if targetType == "" || targetDsn == "" {
1033951
log.Fatalf("%s", i18n.T("migrate.cli_error_flags"))
1034952
}
1035953

@@ -1042,8 +960,8 @@ Example:
1042960
fmt.Println(i18n.T("migrate.cli_backup_success"))
1043961

1044962
// --- 2. Connect to target DB and run migrations ---
1045-
fmt.Println(i18n.T("migrate.cli_connecting_target", config.Database.Type))
1046-
targetStore, err := initTargetDB(config.Database.Type, config.Database.Dsn)
963+
fmt.Println(i18n.T("migrate.cli_connecting_target", targetType))
964+
targetStore, err := initTargetDB(targetType, targetDsn)
1047965
if err != nil {
1048966
log.Fatalf("%s", i18n.T("migrate.cli_error_connect", err))
1049967
}

internal/i18n/locales/active.art-x-ang.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,3 +467,15 @@ accounts.key_selection.continue: Gefaraþ forþ
467467
accounts.key_selection.help: '(↑/↓: færeld, rým/ingang: swītlian, tāb: tō cnappum,
468468
ā: geheald eall, n: geheald nān, forlæt: bæc)'
469469
audit.tui.fleet_complete: Sciphere geendod.
470+
471+
# Backup CLI command
472+
backup.cli_starting: "📦 Ġeheald ċeafiġende..."
473+
backup.cli_error_export: "Gedƿola ūtsendende dæta: %v"
474+
backup.cli_error_write: "Gedƿola ƿrītende ġehealdtruman: %v"
475+
backup.cli_success: "✅ Ġeheald ġesundfullīċe ġehealden tō %s."
476+
477+
# Restore CLI command
478+
restore.cli_starting: "🔄 Onġinnende edstaðolunge fram %s..."
479+
restore.cli_error_read: "Gedƿola rǣdende ġehealdtruman: %v"
480+
restore.cli_error_import: "Gedƿola inbringende dæta: %v"
481+
restore.cli_success: "🎉 Edstaðolung ġesundfullīċe fullfremmed."

internal/i18n/locales/active.de.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,3 +514,24 @@ migrate.cli_success: "🎉 Migration abgeschlossen! Alle Daten wurden in die neu
514514
migrate.cli_next_steps: "➡️ Nächster Schritt: Aktualisieren Sie Ihre .keymaster.yaml,
515515
um auf die neue Datenbank zu verweisen."
516516
audit.tui.fleet_complete: Audit der Flotte komplett.
517+
518+
# Backup CLI command
519+
backup.cli_starting: "📦 Erstelle Backup..."
520+
backup.cli_error_export: "Fehler beim Exportieren der Daten: %v"
521+
backup.cli_error_write: "Fehler beim Schreiben der Backup-Datei: %v"
522+
backup.cli_success: "✅ Backup erfolgreich in %s gespeichert."
523+
524+
# Restore CLI command
525+
restore.cli_starting: "🔄 Wiederherstellung von %s wird gestartet..."
526+
restore.cli_error_read: "Fehler beim Lesen der Backup-Datei: %v"
527+
restore.cli_error_import: "Fehler beim Importieren der Daten: %v"
528+
restore.cli_success: "🎉 Wiederherstellung erfolgreich abgeschlossen."
529+
530+
# Decommission CLI command
531+
decommission.cli_error_find_account: "Fehler beim Finden des Kontos: %v"
532+
decommission.cli_no_accounts_tag: "Keine Konten mit dem Tag gefunden: %s"
533+
decommission.cli_found_accounts_tag: "%d Konten mit dem Tag '%s' gefunden:"
534+
535+
# Bootstrap cleanup
536+
bootstrap.cli_starting: "Starte Bootstrap-Bereinigung..."
537+
bootstrap.cli_complete: "Bootstrap-Bereinigung abgeschlossen."

internal/i18n/locales/active.en.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,3 +404,15 @@ parallel_task.deploy_fail_message: "💥 Failed to deploy to %s: %v"
404404
parallel_task.audit_success_message: "✅ OK: %s"
405405
parallel_task.audit_fail_message: "🚨 Drift detected on %s: %v"
406406
parallel_task.complete_message: "%s complete."
407+
408+
# Backup CLI command
409+
backup.cli_starting: "📦 Creating backup..."
410+
backup.cli_error_export: "Error exporting data: %v"
411+
backup.cli_error_write: "Error writing backup file: %v"
412+
backup.cli_success: "✅ Backup successfully saved to %s."
413+
414+
# Restore CLI command
415+
restore.cli_starting: "🔄 Starting restore from %s..."
416+
restore.cli_error_read: "Error reading backup file: %v"
417+
restore.cli_error_import: "Error importing data: %v"
418+
restore.cli_success: "🎉 Restore completed successfully."

0 commit comments

Comments
 (0)