preRun sets up the configuration and initializes the LedgerForge instance before running any command. It ensures that the configuration is loaded, and the LedgerForge instance is initialized properly.
(app *ledgerforgeInstance, configFile *string)
| 52 | // preRun sets up the configuration and initializes the LedgerForge instance before running any command. |
| 53 | // It ensures that the configuration is loaded, and the LedgerForge instance is initialized properly. |
| 54 | func preRun(app *ledgerforgeInstance, configFile *string) func(cmd *cobra.Command, args []string) error { |
| 55 | return func(cmd *cobra.Command, args []string) error { |
| 56 | // Initialize configuration from the specified configuration file. |
| 57 | err := config.InitConfig(*configFile) |
| 58 | if err != nil { |
| 59 | log.Fatal("error loading config", err) |
| 60 | } |
| 61 | |
| 62 | // Fetch the configuration settings. |
| 63 | cnf, err := config.Fetch() |
| 64 | if err != nil { |
| 65 | return err |
| 66 | } |
| 67 | |
| 68 | // Initialize the LedgerForge instance using the fetched configuration. |
| 69 | newLedgerForge, err := setupLedgerForge(cnf) |
| 70 | if err != nil { |
| 71 | notification.NotifyError(err) // Notify via the internal notification system |
| 72 | log.Fatal(err) // Log the fatal error |
| 73 | } |
| 74 | |
| 75 | // Assign the new LedgerForge instance and configuration to the app struct. |
| 76 | app.ledgerforge = newLedgerForge |
| 77 | app.cnf = cnf |
| 78 | |
| 79 | return nil |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | // setupLedgerForge creates and initializes a new LedgerForge instance based on the provided configuration. |
| 84 | // It connects to the data source (such as a database) using the configuration settings. |
no test coverage detected