@@ -804,39 +804,8 @@ Example (Integrate):
804804
805805Example (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:
1001943Example:
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 }
0 commit comments