RunWithStartupVerification constructs the API client, performs connectivity verification with user feedback, and starts the TUI.
(cfg *config.Config, configPath string, opts Options)
| 24 | |
| 25 | // RunWithStartupVerification constructs the API client, performs connectivity verification with user feedback, and starts the TUI. |
| 26 | func RunWithStartupVerification(cfg *config.Config, configPath string, opts Options) error { |
| 27 | // Initialize logger first (but don't output startup messages in debug mode) |
| 28 | level := logger.LevelInfo |
| 29 | if cfg.Debug { |
| 30 | level = logger.LevelDebug |
| 31 | } |
| 32 | |
| 33 | mainLogger, err := logger.NewInternalLogger(level, cfg.CacheDir) |
| 34 | if err != nil { |
| 35 | mainLogger = logger.NewSimpleLogger(level) |
| 36 | } |
| 37 | |
| 38 | loggerAdapter := adapters.NewLoggerAdapter(cfg) |
| 39 | models.SetUILogger(loggerAdapter) |
| 40 | |
| 41 | // Create cache directory |
| 42 | if cfg.CacheDir != "" { |
| 43 | if mkdirErr := os.MkdirAll(cfg.CacheDir, 0o750); mkdirErr != nil { |
| 44 | return fmt.Errorf("create cache dir: %w", mkdirErr) |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | // Initialize cache |
| 49 | if !opts.NoCache { |
| 50 | if cacheErr := cache.InitGlobalCache(cfg.CacheDir); cacheErr != nil { |
| 51 | mainLogger.Error("failed to initialize cache: %v", cacheErr) |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | // Initialize global logger |
| 56 | if loggerErr := logger.InitGlobalLogger(level, cfg.CacheDir); loggerErr != nil { |
| 57 | mainLogger.Error("failed to init global logger: %v", loggerErr) |
| 58 | } |
| 59 | |
| 60 | // Normalize the API URL |
| 61 | cfg.Addr = strings.TrimRight(cfg.Addr, "/") + "/" + strings.TrimPrefix(cfg.ApiPath, "/") |
| 62 | |
| 63 | // Create adapters |
| 64 | configAdapter := adapters.NewConfigAdapter(cfg) |
| 65 | cacheAdapter := adapters.NewCacheAdapter() |
| 66 | |
| 67 | // Initialize API client (this just sets up the client, doesn't test connectivity) |
| 68 | fmt.Println(display.IconText("🔧", "Initializing API client...", cfg.ShowIcons)) |
| 69 | |
| 70 | var client *api.Client |
| 71 | var profilesToTry []string |
| 72 | |
| 73 | if opts.InitialGroup != "" { |
| 74 | profilesToTry = cfg.GetProfileNamesInGroup(opts.InitialGroup) |
| 75 | } else { |
| 76 | // Just try the current config (which is already set up) |
| 77 | profilesToTry = []string{""} |
| 78 | } |
| 79 | |
| 80 | var connected bool |
| 81 | var lastErr error |
| 82 | |
| 83 | for _, profileName := range profilesToTry { |
no test coverage detected