MCPcopy Create free account
hub / github.com/devnullvoid/pvetui / initCLISession

Function initCLISession

internal/cli/cli_helpers.go:151–243  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

149// Returns (nil, nil) when bootstrap handled an early-exit flag (--version,
150// --list-profiles) — callers should return nil without doing further work.
151func 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

Callers 15

runStorageListFunction · 0.85
runStorageShowFunction · 0.85
runStorageContentListFunction · 0.85
runStorageContentDeleteFunction · 0.85
runStorageDownloadURLFunction · 0.85
runStorageDownloadOCIFunction · 0.85
runStorageRestoreFunction · 0.85
completeStorageNamesFunction · 0.85
completeTemplateNamesFunction · 0.85
runNodesListFunction · 0.85
runNodesShowFunction · 0.85

Calls 12

InitializeMethod · 0.95
BootstrapFunction · 0.92
InitGlobalCacheFunction · 0.92
NewLoggerAdapterFunction · 0.92
NewCacheAdapterFunction · 0.92
NewConfigAdapterFunction · 0.92
NewClientFunction · 0.92
WithLoggerFunction · 0.92
WithCacheFunction · 0.92
NewGroupClientManagerFunction · 0.92
getBootstrapOptionsFunction · 0.85

Tested by

no test coverage detected