Skip to content

Fix flags control in commands prerun phase#111

Open
lurenzsgp wants to merge 7 commits intomainfrom
fix/remove-interactive-from-global-flags
Open

Fix flags control in commands prerun phase#111
lurenzsgp wants to merge 7 commits intomainfrom
fix/remove-interactive-from-global-flags

Conversation

@lurenzsgp
Copy link
Member

@lurenzsgp lurenzsgp commented Mar 13, 2026

Summary

Removes all MarkPersistentFlagRequired() calls and moves flag requirements to individual command PreRun functions. Improves CLI stability, interactive mode support, and user experience.

Changes

12 files modified (+177 insertions, -215 deletions)

Key Improvements

  1. Removed MarkPersistentFlagRequired() - Prevents panics from nil pointer dereferences
  2. Better Interactive Mode - gateway install --interactive no longer requires --tenant-id or --gateway-id
  3. Cobra Flag Validation - Uses MarkFlagsOneRequired(), MarkFlagsRequiredTogether(), MarkFlagsMutuallyExclusive()
  4. Moved Validation to Run - Replaces os.Exit() with graceful returns
  5. Added Examples - Usage examples in command help

Files Modified

File Key Changes
agent.go Added --swarm-id requirement, improved batch/individual flag validation
gateway.go Removed duplicate PreRun, improved interactive flag validation
nexus.go Added --swarm-id requirement to all commands
node.go Added --swarm-id requirement, improved batch validation
operator.go Improved tenant/swarm flag handling
project.go Added --tenant-id requirement to all commands
user.go Added --tenant-id requirement to all commands
redundancy_class.go Added --swarm-id requirement
redundancy_class_recovery.go Added --swarm-id requirement
tenant.go Improved interactive flag validation
swarm.go Improved flag validation
policy.go Cleaned up formatting

Flag Requirements

Commands Requiring --swarm-id

  • agent (create, describe, edit, list, remove, status)
  • nexus (create, describe, edit, list, remove, deploy)
  • node (create, describe, edit, list, remove, deploy)
  • rc (create, describe, status, expand, list, recovery start/status)

Commands Requiring --tenant-id

  • gateway (create, describe, edit, install, list, remove)
  • tenant project (all subcommands)
  • tenant user (all subcommands)

Commands Supporting Interactive Mode

  • gateway install - Either --interactive OR (--tenant-id + --gateway-id)
  • tenant configure-dns - Either --interactive OR (--tenant-id + --domain)
  • tenant verify-dns - Either --interactive OR --tenant-id

Commands Requiring Either --tenant-id OR --swarm-id

  • iam user (create, list, remove, describe)

Testing

# Verify missing flag errors
./cubbit agent create         # Error: --agent-port must be explicitly provided
./cubbit agent list           # Error: either --redundancy-class-id or both --nexus-id and --node-id are required
./cubbit nexus deploy         # Error: required flag(s) "nexus-id", "swarm-id" not set
# Verify interactive mode
./cubbit gateway install --interactive  # Works without --tenant-id or --gateway-id

Impact

  • ✅ No breaking changes - All existing commands work identically
  • ✅ Better error messages - Clear indication of missing required flags
  • ✅ Stability - No more panics from persistent flag requirements
  • ✅ UX - Interactive mode commands work correctly

Remove --interactive from global flags. Ensure it is supported on the command that implement it
and update prerun check to require the correct flags
@lurenzsgp lurenzsgp self-assigned this Mar 13, 2026
@lurenzsgp lurenzsgp added the bug Something isn't working label Mar 13, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR refactors CLI flag validation by removing MarkPersistentFlagRequired() usage and moving “required flag” enforcement to per-command PreRun logic, adding richer Cobra validations (one-required / required-together / mutually-exclusive) and enabling interactive flows on select commands.

Changes:

  • Replaced persistent required flags with per-subcommand PreRun requirements across multiple command trees (tenant/user/project/gateway/agent/node/nexus/rc).
  • Introduced interactive-mode flag gating for tenant configure-dns, tenant verify-dns, and gateway install.
  • Simplified some validations by using Cobra’s flag-group helpers instead of manual os.Exit() validation blocks.

Reviewed changes

Copilot reviewed 13 out of 14 changed files in this pull request and generated 11 comments.

Show a summary per file
File Description
src/cmd/root.go Removes global --interactive persistent flag from root.
src/cmd/agent.go Adds --swarm-id requirement + batch/single validation via Cobra helpers; adds examples.
src/cmd/node.go Adds --swarm-id/--nexus-id requirements and batch/single validation; adds examples.
src/cmd/nexus.go Makes --swarm-id required across nexus subcommands.
src/cmd/gateway.go Requires --tenant-id on non-interactive gateway commands; adds interactive install mode + examples.
src/cmd/tenant.go Moves list validation into Run; adds interactive DNS flows with flag-group validation.
src/cmd/project.go Requires --tenant-id per subcommand; moves list sort validation into Run.
src/cmd/user.go Requires --tenant-id per subcommand; adjusts list pre-run validation.
src/cmd/swarm.go Replaces manual “name/description required” check with MarkFlagsOneRequired.
src/cmd/operator.go Refactors tenant/swarm branching and moves tenant/swarm flags to persistent flags.
src/cmd/policy.go Minor formatting cleanup; keeps tenant/swarm mutual exclusion logic.
src/cmd/redundancy_class.go Requires --swarm-id per subcommand; updates create validation requirements.
src/cmd/redundancy_class_recovery.go Adds rc-id/swarm-id requirements for recovery subcommands; adjusts persistent flags.
.gitignore Ignores opencode artifacts.
Comments suppressed due to low confidence (1)

src/cmd/user.go:143

  • The invalid sort-key error message lists tenant fields (id, name, owner_id, ...) but this command’s allowedSortingKeys is id, first_name, last_name, .... This makes the guidance incorrect when users hit the validation error; update the message to match the actual allowed keys.
		allowedSortingKeys := []string{"id", "first_name", "last_name", "max_allowed_projects", "created_at", "deleted_at", "tenant_id"}
		sort, _ := cmd.Flags().GetString("sort")

		if sort != "" && !utils.Contains(allowedSortingKeys, sort) {
			fmt.Println("Error: invalid sort key provided, allowed keys are: id, name, owner_id, coupon_id, created_at, deleted_at")
			cmd.Usage()

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
DangerBlack
DangerBlack previously approved these changes Mar 17, 2026
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
DangerBlack
DangerBlack previously approved these changes Mar 18, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR refactors Cobra flag validation across the CLI by removing MarkPersistentFlagRequired() usage and instead enforcing required flags and flag relationships at the subcommand level (often via PreRun plus Cobra’s MarkFlags* helpers), with additional interactive-mode support on certain commands.

Changes:

  • Removed persistent required-flag markings and added per-command required flag validation.
  • Introduced/expanded interactive-mode flows for selected commands (e.g., gateway install, tenant DNS).
  • Converted some commands to RunE and replaced several os.Exit() validation paths with returned errors.

Reviewed changes

Copilot reviewed 13 out of 14 changed files in this pull request and generated 11 comments.

Show a summary per file
File Description
src/cmd/agent.go Adds swarm-id requirement and new flag relationship validation; introduces RunE for list and updates validation style.
src/cmd/gateway.go Improves tenant-id enforcement and interactive install validation; updates list validation to RunE.
src/cmd/nexus.go Adds swarm-id requirement to nexus subcommands and removes persistent required marking.
src/cmd/node.go Adds swarm/nexus requirements and batch vs single-create flag relationships; moves some validation into RunE.
src/cmd/operator.go Changes IAM user scoping to allow either tenant-id or swarm-id instead of globally requiring tenant-id.
src/cmd/policy.go Minor formatting cleanup in flag validation blocks.
src/cmd/project.go Adds tenant-id requirement at subcommand level; changes list to RunE.
src/cmd/redundancy_class.go Adds swarm-id requirement and updates RC create validation requirements.
src/cmd/redundancy_class_recovery.go Moves rc-id requirement to subcommands and adds swarm-id requirement.
src/cmd/root.go Removes root persistent --interactive flag.
src/cmd/swarm.go Uses MarkFlagsOneRequired(name, description) for edit validation instead of manual checks.
src/cmd/tenant.go Converts tenant list to RunE; adds interactive-mode flags/validation to DNS commands.
src/cmd/user.go Adds tenant-id required validation per user subcommand; removes persistent required marking.
.gitignore Ignores opencode-related files/directories.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 75 to +77
if err := action.ListGateways(cmd, args); err != nil {
utils.PrintError(err)
return err
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Development

Successfully merging this pull request may close these issues.

3 participants