Add login option for caib and save the configuration locally#70
Conversation
….caib Signed-off-by: Bella Khizgiyaev <bkhizgiy@redhat.com>
📝 WalkthroughWalkthroughThis change introduces a local CLI configuration system for the caib tool that persists server URLs to Changes
Sequence Diagram(s)sequenceDiagram
actor User
participant CLI as caib CLI
participant Config as config.DefaultServer()
participant FS as File System
participant Auth as Auth System
User->>CLI: caib login <server-url>
CLI->>Config: Normalize and validate URL
Config->>FS: Save server URL to ~/.caib/cli.json
FS-->>Config: Config saved
Config->>Auth: Attempt OIDC/kubeconfig auth
Auth-->>CLI: Auth result
CLI-->>User: Login status message
User->>CLI: caib catalog list
CLI->>Config: DefaultServer()
Config->>FS: Read ~/.caib/cli.json (if CAIB_SERVER not set)
FS-->>Config: Return saved server URL
Config-->>CLI: Return server URL
CLI->>CLI: Execute catalog operation
CLI-->>User: Catalog results
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@cmd/caib/catalog/list.go`:
- Around line 98-100: Update the error string in the server check so it
consistently references the environment variable name without the extra "env
var" wording; locate the conditional that checks `if server == ""` in
cmd/caib/catalog/list.go and change the returned fmt.Errorf message to mention
`CAIB_SERVER` (matching other catalog commands) instead of `CAIB_SERVER env
var`.
🧹 Nitpick comments (2)
cmd/caib/main.go (1)
418-419: Consider consistency with catalog commands for server URL resolution.The main commands (
build,disk,build-dev,flash,list) useconfig.DefaultServer()as the flag default value, which is evaluated once at program startup. In contrast, the catalog commands callconfig.DefaultServer()at runtime within their run functions when the flag is empty.While this works correctly (each CLI invocation is a new process), the pattern difference could cause confusion for future maintainers. The catalog approach (checking in run function) is slightly more explicit.
This is a minor architectural observation and doesn't affect correctness.
Also applies to: 450-451, 458-459, 485-486, 512-513
cmd/caib/config/config.go (1)
53-68: Good security practices with file permissions.Using
0700for directory and0600for file properly restricts access to the owner only.Optional: Consider preserving existing config values for future extensibility.
Currently,
SaveServerURLoverwrites the entire config file. IfCLIConfigis extended with additional fields later, this function would inadvertently wipe those values. Consider reading the existing config first and merging:♻️ Suggested refactor for future extensibility
// SaveServerURL writes the given server URL to the local config file. func SaveServerURL(serverURL string) error { dir, err := configDirPath() if err != nil { return err } if err := os.MkdirAll(dir, 0700); err != nil { return err } - cfg := &CLIConfig{ServerURL: strings.TrimSpace(serverURL)} + // Read existing config to preserve other fields + cfg, _ := Read() + if cfg == nil { + cfg = &CLIConfig{} + } + cfg.ServerURL = strings.TrimSpace(serverURL) data, err := json.MarshalIndent(cfg, "", " ") if err != nil { return err } return os.WriteFile(filepath.Join(dir, configFile), data, 0600) }
This PR adds a new flag to the caib CLI that allows users to log in on the first interaction with the server. The server endpoint is saved locally under
~/.caib/cli.json, eliminating the need to set an environment variable or explicitly pass --server on each iteration.Usage:
caib login <server-endpoint>Summary by CodeRabbit
New Features
caib login <server-url>command to save and manage server configuration locally.Improvements
caib login,--serverflag, orCAIB_SERVERenvironment variable.