Skip to content

refactor(cli): migrate to Cobra-based command structure#429

Merged
xiaket merged 17 commits intosipeed:mainfrom
pixel365:refactor/cmd
Feb 25, 2026
Merged

refactor(cli): migrate to Cobra-based command structure#429
xiaket merged 17 commits intosipeed:mainfrom
pixel365:refactor/cmd

Conversation

@pixel365
Copy link
Contributor

📝 Description

This PR refactors the CLI implementation by replacing manual os.Args-based argument parsing with a structured command hierarchy built on Cobra.

The change introduces a root command and dedicated subcommand packages under cmd/picoclaw/internal. Existing commands (agent, auth, cron, gateway, migrate, onboard, skills, status, version) are migrated to Cobra-based command definitions.

Manual flag parsing and direct os.Args access have been removed from command handlers. All flags and argument validation are now handled through Cobra.

No business logic or runtime behavior is intended to change. This refactor focuses on improving CLI structure, maintainability, and extensibility.


🗣️ Type of Change

  • 🐞 Bug fix (non-breaking change which fixes an issue)
  • ✨ New feature (non-breaking change which adds functionality)
  • 📖 Documentation update
  • ⚡ Code refactoring (no functional changes, no api changes)

🤖 AI Code Generation

  • 🤖 Fully AI-generated (100% AI, 0% Human)
  • 🛠️ Mostly AI-generated (AI draft, Human verified/modified)
  • 👨‍💻 Mostly Human-written (Human lead, AI assisted or none)

🔗 Related Issue

N/A


📚 Technical Context (Skip for Docs)

  • Reference URL: https://github.com/spf13/cobra
  • Reasoning:
    • Improve CLI maintainability and modularity
    • Eliminate manual argument parsing
    • Enable structured subcommands and automatic help generation
    • Prepare CLI for future extensibility

🧪 Test Environment

  • Hardware: PC
  • OS: Linux (Fedora)
  • Model/Provider: OpenRouter (various models tested)
  • Channels: CLI

📸 Evidence (Optional)

Verified that all existing commands behave identically:

picoclaw agent
picoclaw auth login
picoclaw cron add
picoclaw gateway
picoclaw skills install ...

Help output and flag handling function correctly under Cobra.


☑️ Checklist

  • My code/docs follow the style of this project.
  • I have performed a self-review of my own changes.
  • I have updated the documentation accordingly.

Refactor CLI to use Cobra instead of manual os.Args parsing.

- Introduce root command and structured subcommands under cmd/picoclaw/internal
- Convert agent, auth, cron, gateway, migrate, onboard, skills, status and version to Cobra commands
- Replace manual flag parsing with Cobra flags
- Remove direct os.Args usage from command handlers
- Keep existing command behavior and output semantics

This change focuses on CLI structure and maintainability.
No business logic changes intended.
@pixel365
Copy link
Contributor Author

I will keep this branch synced with main while the PR is under review.

@xiaket
Copy link
Collaborator

xiaket commented Feb 19, 2026

Happy to be duplicated: #214

Copy link

@nikolasdehor nikolasdehor left a comment

Choose a reason for hiding this comment

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

Significant refactor moving from manual os.Args parsing to Cobra. The structure looks well-organized with each command getting its own package under cmd/picoclaw/internal/. A few observations:

  1. No tests for CLI commands — This is a large refactor touching 36 files with 1725 additions. Even basic tests verifying command registration and flag parsing would help catch regressions. Cobra makes this easy with cmd.Execute() in test contexts.

  2. LDFLAGS change — The Makefile now injects version info into github.com/sipeed/picoclaw/cmd/picoclaw/internal instead of main. Make sure goreleaser or any release automation also uses this updated path, otherwise release builds will have empty version strings.

  3. Error handling style — The helper functions (e.g., agentCmd, authLoginCmd) properly return errors via RunE, which is good. However, some functions still use fmt.Printf for warnings (e.g., 'Warning: could not update config') instead of returning them. This is inconsistent — either log all warnings or propagate them.

  4. Unused authHelp function — The auth/helpers.go defines an authHelp() function that manually prints help text, but with Cobra this should be auto-generated. If this function is called somewhere, it duplicates what Cobra provides. If not, it is dead code.

  5. interactiveMode error comparisons — In agent/helpers.go, err == readline.ErrInterrupt and err == io.EOF should use errors.Is for consistency with PR #460 which is migrating the codebase to errors.Is.

  6. Dependency addition — Cobra + pflag + mousetrap are reasonable dependencies for a CLI tool. The indirect dependency count is acceptable.

Overall this is a good direction. The main concern is the lack of tests for such a large refactor. Consider adding at least integration tests that verify the command tree is wired correctly.

@pixel365
Copy link
Contributor Author

@nikolasdehor
Thanks for the detailed review — all points are valid.

Regarding the error comparisons: I intentionally left them unchanged in this PR to preserve original behavior and avoid introducing additional noise in an already large structural refactor. The goal here was strictly CLI restructuring.

I agree that migrating remaining direct error comparisons to errors.Is would improve consistency, and I’ll address that in a focused follow-up PR.

I’ll also take care of the other noted items (CLI tests, LDFLAGS verification, help cleanup, and consistency improvements) to keep this refactor solid and maintainable.

Appreciate the thorough feedback.

- Add tests for CLI command tree and flag parsing
- Align LDFLAGS injection path for version info
- Remove unused manual help function
@pixel365
Copy link
Contributor Author

@nikolasdehor

Thanks again for the detailed review.

I’ve pushed updates addressing the comments (CLI tests, LDFLAGS alignment, help cleanup, etc.) and synced the branch with the latest main to resolve conflicts.

Would appreciate another look when you have time.

Replace standard library testing error checks (t.Error*, t.Fatalf)
with assert/require from stretchr/testify across all cobra command tests
for improved readability and consistency.
@nikolasdehor
Copy link

Note: #513 also migrates the CLI to Cobra. Both PRs address the same refactoring goal. Suggesting the maintainer pick one to avoid conflicting implementations.

- Move subcommand registration out of PersistentPreRunE
- Ensure `picoclaw skills <subcommand>` resolves correctly
- Minor install command and test cleanups
Copy link
Collaborator

@xiaket xiaket left a comment

Choose a reason for hiding this comment

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

cmd/picoclaw/internal/auth/status.go need to be updated, but others are nits and for your consideration only.

Thanks for bearing with me! I have not been very swift in the review of this PR.

@pixel365
Copy link
Contributor Author

cmd/picoclaw/internal/auth/status.go need to be updated, but others are nits and for your consideration only.

Thanks for bearing with me! I have not been very swift in the review of this PR.

Thanks for the thorough review - really appreciate the detailed feedback.

I’ll update cmd/picoclaw/internal/auth/status.go accordingly.
As for the other points, I’ll take them into consideration and adjust where it improves clarity without expanding the scope unnecessarily.

No worries at all about the timing - I appreciate you taking the time to review such a large PR.

@pixel365 pixel365 requested a review from xiaket February 25, 2026 07:09
Copy link
Collaborator

@xiaket xiaket left a comment

Choose a reason for hiding this comment

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

LGTM

@xiaket xiaket merged commit 73f2780 into sipeed:main Feb 25, 2026
2 checks passed
@pixel365 pixel365 deleted the refactor/cmd branch February 25, 2026 09:18
liangzhang-keepmoving pushed a commit to liangzhang-keepmoving/picoclaw that referenced this pull request Mar 2, 2026
* refactor(cli): migrate to Cobra-based command structure

Refactor CLI to use Cobra instead of manual os.Args parsing.

- Introduce root command and structured subcommands under cmd/picoclaw/internal
- Convert agent, auth, cron, gateway, migrate, onboard, skills, status and version to Cobra commands
- Replace manual flag parsing with Cobra flags
- Remove direct os.Args usage from command handlers
- Keep existing command behavior and output semantics

This change focuses on CLI structure and maintainability.
No business logic changes intended.

* chore(cli): remove version2 alias and make cobra a direct dependency

* test(cli): add basic command tests

- Add tests for CLI command tree and flag parsing
- Align LDFLAGS injection path for version info
- Remove unused manual help function

* test: migrate command tests to testify assertions

Replace standard library testing error checks (t.Error*, t.Fatalf)
with assert/require from stretchr/testify across all cobra command tests
for improved readability and consistency.

* fix(cli): make linter happy

* test: avoid duplication in windows config path test

* test: simplify allowed command checks using slices.Contains

* fix(skills): register subcommands during command construction

- Move subcommand registration out of PersistentPreRunE
- Ensure `picoclaw skills <subcommand>` resolves correctly
- Minor install command and test cleanups

* refactor(cli): address review feedback and improve command clarity

* fix(authLogoutCmd): rm os.Exit
hyperwd pushed a commit to hyperwd/picoclaw that referenced this pull request Mar 5, 2026
* refactor(cli): migrate to Cobra-based command structure

Refactor CLI to use Cobra instead of manual os.Args parsing.

- Introduce root command and structured subcommands under cmd/picoclaw/internal
- Convert agent, auth, cron, gateway, migrate, onboard, skills, status and version to Cobra commands
- Replace manual flag parsing with Cobra flags
- Remove direct os.Args usage from command handlers
- Keep existing command behavior and output semantics

This change focuses on CLI structure and maintainability.
No business logic changes intended.

* chore(cli): remove version2 alias and make cobra a direct dependency

* test(cli): add basic command tests

- Add tests for CLI command tree and flag parsing
- Align LDFLAGS injection path for version info
- Remove unused manual help function

* test: migrate command tests to testify assertions

Replace standard library testing error checks (t.Error*, t.Fatalf)
with assert/require from stretchr/testify across all cobra command tests
for improved readability and consistency.

* fix(cli): make linter happy

* test: avoid duplication in windows config path test

* test: simplify allowed command checks using slices.Contains

* fix(skills): register subcommands during command construction

- Move subcommand registration out of PersistentPreRunE
- Ensure `picoclaw skills <subcommand>` resolves correctly
- Minor install command and test cleanups

* refactor(cli): address review feedback and improve command clarity

* fix(authLogoutCmd): rm os.Exit
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.

3 participants