initCLISession bootstraps configuration and returns an authenticated session. Returns (nil, nil) when bootstrap handled an early-exit flag (--version, --list-profiles) — callers should return nil without doing further work.
(cmd *cobra.Command)
| 149 | // Returns (nil, nil) when bootstrap handled an early-exit flag (--version, |
| 150 | // --list-profiles) — callers should return nil without doing further work. |
| 151 | func initCLISession(cmd *cobra.Command) (*cliSession, error) { |
| 152 | opts := getBootstrapOptions(cmd) |
| 153 | opts.Quiet = true // suppress TUI startup banners for CLI subcommands |
| 154 | |
| 155 | result, err := bootstrap.Bootstrap(opts) |
| 156 | if err != nil { |
| 157 | return nil, fmt.Errorf("bootstrap failed: %w", err) |
| 158 | } |
| 159 | |
| 160 | if result == nil { |
| 161 | return nil, nil |
| 162 | } |
| 163 | |
| 164 | cfg := result.Config |
| 165 | |
| 166 | // Normalize the API URL the same way the TUI does. |
| 167 | cfg.Addr = strings.TrimRight(cfg.Addr, "/") + "/" + strings.TrimPrefix(cfg.ApiPath, "/") |
| 168 | |
| 169 | // Initialize global cache unless disabled. |
| 170 | if !result.NoCache { |
| 171 | if cacheErr := cache.InitGlobalCache(cfg.CacheDir); cacheErr != nil { |
| 172 | _ = cacheErr // non-fatal; continue without persistent cache |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | loggerAdapter := adapters.NewLoggerAdapter(cfg) |
| 177 | cacheAdapter := adapters.NewCacheAdapter() |
| 178 | |
| 179 | // Single-profile mode. |
| 180 | if result.InitialGroup == "" { |
| 181 | configAdapter := adapters.NewConfigAdapter(cfg) |
| 182 | |
| 183 | client, err := api.NewClient( |
| 184 | configAdapter, |
| 185 | api.WithLogger(loggerAdapter), |
| 186 | api.WithCache(cacheAdapter), |
| 187 | ) |
| 188 | if err != nil { |
| 189 | return nil, fmt.Errorf("failed to connect to Proxmox API: %w", err) |
| 190 | } |
| 191 | |
| 192 | return &cliSession{single: client, cfg: cfg}, nil |
| 193 | } |
| 194 | |
| 195 | // Group mode: build a client per profile, fan out queries. |
| 196 | manager := api.NewGroupClientManager(result.InitialGroup, loggerAdapter, cacheAdapter) |
| 197 | |
| 198 | profileNames := cfg.GetProfileNamesInGroup(result.InitialGroup) |
| 199 | if len(profileNames) == 0 { |
| 200 | return nil, fmt.Errorf("group %q has no member profiles", result.InitialGroup) |
| 201 | } |
| 202 | |
| 203 | var profiles []api.ProfileEntry |
| 204 | |
| 205 | for _, name := range profileNames { |
| 206 | p, exists := cfg.Profiles[name] |
| 207 | if !exists { |
| 208 | continue |
no test coverage detected