NewCLI creates the command-line interface (CLI) for the LedgerForge application. It sets up the root command and subcommands like serverCommands, workerCommands, and migrateCommands.
()
| 101 | // NewCLI creates the command-line interface (CLI) for the LedgerForge application. |
| 102 | // It sets up the root command and subcommands like serverCommands, workerCommands, and migrateCommands. |
| 103 | func NewCLI() *LedgerForge { |
| 104 | var configFile string // Configuration file path (defaults to ./ledgerforge.json) |
| 105 | b := &ledgerforgeInstance{} // Instance of LedgerForge to be passed into commands |
| 106 | |
| 107 | // Define the root command with usage and description. |
| 108 | rootCmd := &cobra.Command{ |
| 109 | Use: "ledgerforge", |
| 110 | Short: "Open source ledger", // Brief description for the CLI tool |
| 111 | Run: func(cmd *cobra.Command, args []string) {}, // Main function for the root command |
| 112 | } |
| 113 | |
| 114 | // Add a persistent flag to the root command for specifying the config file. |
| 115 | rootCmd.PersistentFlags().StringVar(&configFile, "config", "./ledgerforge.json", "Configuration file for LedgerForge") |
| 116 | |
| 117 | // Set the persistent pre-run hook to initialize the app and config before executing any command. |
| 118 | rootCmd.PersistentPreRunE = preRun(b, &configFile) |
| 119 | |
| 120 | // Add various subcommands to the root command. |
| 121 | rootCmd.AddCommand(serverCommands(b)) // Command for starting the server |
| 122 | rootCmd.AddCommand(workerCommands(b)) // Command for worker processes |
| 123 | rootCmd.AddCommand(migrateCommands(b)) // Command for database/schema migrations |
| 124 | |
| 125 | return &LedgerForge{cmd: rootCmd} |
| 126 | } |
| 127 | |
| 128 | // executeCLI runs the root command, handling any errors that occur during execution. |
| 129 | // It serves as the main entry point for the CLI application. |
no test coverage detected