Skip to content

Add login option for caib and save the configuration locally#70

Merged
bennyz merged 1 commit into
centos-automotive-suite:mainfrom
bkhizgiy:login
Feb 4, 2026
Merged

Add login option for caib and save the configuration locally#70
bennyz merged 1 commit into
centos-automotive-suite:mainfrom
bkhizgiy:login

Conversation

@bkhizgiy

@bkhizgiy bkhizgiy commented Feb 3, 2026

Copy link
Copy Markdown
Contributor

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

    • Added caib login <server-url> command to save and manage server configuration locally.
    • Server URL is now persisted and used as a default when not provided via flag or environment variable.
  • Improvements

    • Updated error messages across catalog commands to guide users to use caib login, --server flag, or CAIB_SERVER environment variable.

….caib

Signed-off-by: Bella Khizgiyaev <bkhizgiy@redhat.com>
@bkhizgiy bkhizgiy requested a review from bennyz February 3, 2026 09:22
@bkhizgiy bkhizgiy changed the title Add login option for caib and save the configuration locally under ~/… Add login option for caib and save the configuration locally Feb 3, 2026
@coderabbitai

coderabbitai Bot commented Feb 3, 2026

Copy link
Copy Markdown
📝 Walkthrough

Walkthrough

This change introduces a local CLI configuration system for the caib tool that persists server URLs to ~/.caib/cli.json. A new caib login command allows users to set credentials, and the catalog commands (add, get, list, publish, remove, verify) now use a centralized config.DefaultServer() instead of directly reading the CAIB_SERVER environment variable.

Changes

Cohort / File(s) Summary
Catalog Command Updates
cmd/caib/catalog/add.go, cmd/caib/catalog/get.go, cmd/caib/catalog/list.go, cmd/caib/catalog/publish.go, cmd/caib/catalog/remove.go, cmd/caib/catalog/verify.go
Added config package import. Replaced environment variable server retrieval with config.DefaultServer(). Updated error messages to include guidance for --server, CAIB_SERVER, or running caib login <server-url>.
Configuration Module
cmd/caib/config/config.go
New CLI configuration module that manages server URL persistence. Provides DefaultServer() (returns env var or saved config), Read() (loads from ~/.caib/cli.json), SaveServerURL() (persists to config file), and helper functions for config directory management.
Main CLI Command
cmd/caib/main.go
Added config import and new loginCmd command. Introduced runLogin() function to normalize and persist server URLs, and attempt authentication. Replaced hard-coded environment-based defaults with config.DefaultServer() across multiple commands. Updated error guidance to reference the new login workflow.

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
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 A login command hops into the CLI,
Server URLs now saved, no env vars nearby,
Config files nestled in dotted homes,
The catalog commands roam new config domes,
Persistence and auth now in harmony!

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 41.18% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: adding a login option and saving configuration locally, which aligns with the PR objectives and all modified files.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) use config.DefaultServer() as the flag default value, which is evaluated once at program startup. In contrast, the catalog commands call config.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 0700 for directory and 0600 for file properly restricts access to the owner only.

Optional: Consider preserving existing config values for future extensibility.

Currently, SaveServerURL overwrites the entire config file. If CLIConfig is 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)
 }

Comment thread cmd/caib/catalog/list.go
@bennyz bennyz merged commit 921aa0c into centos-automotive-suite:main Feb 4, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants