2026-01-30 22:11:35 +00:00
|
|
|
package cli
|
2025-10-27 05:07:22 +00:00
|
|
|
|
|
|
|
|
import (
|
2026-02-05 07:52:23 +00:00
|
|
|
"fmt"
|
2026-01-30 00:47:54 +00:00
|
|
|
"os"
|
2026-02-05 07:52:23 +00:00
|
|
|
"runtime/debug"
|
2026-01-30 00:47:54 +00:00
|
|
|
|
2026-02-05 06:55:50 +00:00
|
|
|
"github.com/host-uk/core/pkg/crypt/openpgp"
|
2026-01-30 10:55:30 +00:00
|
|
|
"github.com/host-uk/core/pkg/framework"
|
2026-01-30 22:11:35 +00:00
|
|
|
"github.com/host-uk/core/pkg/log"
|
2026-02-05 06:55:50 +00:00
|
|
|
"github.com/host-uk/core/pkg/workspace"
|
2026-01-30 00:47:54 +00:00
|
|
|
"github.com/spf13/cobra"
|
2025-10-27 05:07:22 +00:00
|
|
|
)
|
|
|
|
|
|
2026-01-30 10:55:30 +00:00
|
|
|
const (
|
2026-01-30 22:11:35 +00:00
|
|
|
// AppName is the CLI application name.
|
|
|
|
|
AppName = "core"
|
2026-01-30 10:55:30 +00:00
|
|
|
)
|
|
|
|
|
|
2026-02-01 06:21:12 +00:00
|
|
|
// AppVersion is set at build time via ldflags:
|
|
|
|
|
//
|
|
|
|
|
// go build -ldflags="-X github.com/host-uk/core/pkg/cli.AppVersion=v1.0.0"
|
|
|
|
|
var AppVersion = "dev"
|
|
|
|
|
|
2026-01-30 22:11:35 +00:00
|
|
|
// Main initialises and runs the CLI application.
|
|
|
|
|
// This is the main entry point for the CLI.
|
2026-02-05 07:52:23 +00:00
|
|
|
// Exits with code 1 on error or panic.
|
2026-01-30 22:16:34 +00:00
|
|
|
func Main() {
|
2026-02-05 07:52:23 +00:00
|
|
|
// Recovery from panics
|
|
|
|
|
defer func() {
|
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
|
log.Error("recovered from panic", "error", r, "stack", string(debug.Stack()))
|
|
|
|
|
Shutdown()
|
|
|
|
|
Fatal(fmt.Errorf("panic: %v", r))
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
2026-01-30 10:55:30 +00:00
|
|
|
// Initialise CLI runtime with services
|
2026-01-30 22:11:35 +00:00
|
|
|
if err := Init(Options{
|
|
|
|
|
AppName: AppName,
|
|
|
|
|
Version: AppVersion,
|
2026-01-30 10:55:30 +00:00
|
|
|
Services: []framework.Option{
|
2026-01-30 22:11:35 +00:00
|
|
|
framework.WithName("i18n", NewI18nService(I18nOptions{})),
|
|
|
|
|
framework.WithName("log", NewLogService(log.Options{
|
|
|
|
|
Level: log.LevelInfo,
|
2026-01-30 10:55:30 +00:00
|
|
|
})),
|
2026-02-05 06:55:50 +00:00
|
|
|
framework.WithName("crypt", openpgp.New),
|
|
|
|
|
framework.WithName("workspace", workspace.New),
|
2026-01-30 10:55:30 +00:00
|
|
|
},
|
|
|
|
|
}); err != nil {
|
Standardize CLI Error Handling (#318)
* Standardize CLI error handling and deprecate cli.Fatal
- Updated `pkg/cli/output.go` to send error and warning output to `os.Stderr`.
- Added `ErrorWrap`, `ErrorWrapVerb`, and `ErrorWrapAction` helpers to `pkg/cli/output.go`.
- Deprecated `cli.Fatal` family of functions in `pkg/cli/errors.go`.
- Introduced `cli.ExitError` and `cli.Exit` helper to allow commands to return specific exit codes.
- Updated `pkg/cli/app.go` to silence Cobra errors and handle error printing and process exit in `Main`.
- Refactored multiple commands (QA, SDK, CI, Updater) to return errors instead of exiting abruptly.
- Replaced direct `os.Stderr` writes with standardized CLI or log helpers across the codebase.
- Updated tests to accommodate changes in output destination.
* Fix CI failure: remove unused fmt import in pkg/mcp/transport_tcp.go
- Removed unused "fmt" import in `pkg/mcp/transport_tcp.go` that was causing CI failure.
- Verified build and relevant tests pass.
* Standardize CLI error handling and fix formatting issues
- Updated `pkg/cli/output.go` to send error and warning output to `os.Stderr`.
- Added `ErrorWrap`, `ErrorWrapVerb`, and `ErrorWrapAction` helpers to `pkg/cli/output.go`.
- Deprecated `cli.Fatal` family of functions in `pkg/cli/errors.go`.
- Introduced `cli.ExitError` and `cli.Exit` helper to allow commands to return specific exit codes.
- Updated `pkg/cli/app.go` to silence Cobra errors and handle error printing and process exit in `Main`.
- Refactored multiple commands (QA, SDK, CI, Updater) to return errors instead of exiting abruptly.
- Replaced direct `os.Stderr` writes with standardized CLI or log helpers across the codebase.
- Updated tests to accommodate changes in output destination.
- Fixed formatting in `pkg/io/local/client.go`.
- Removed unused `fmt` import in `pkg/mcp/transport_tcp.go`.
* Standardize CLI error handling and fix CI issues
- Updated `pkg/cli/output.go` to send error and warning output to `os.Stderr`.
- Added `ErrorWrap`, `ErrorWrapVerb`, and `ErrorWrapAction` helpers to `pkg/cli/output.go`.
- Deprecated `cli.Fatal` family of functions in `pkg/cli/errors.go`.
- Introduced `cli.ExitError` and `cli.Exit` helper to allow commands to return specific exit codes.
- Updated `pkg/cli/app.go` to silence Cobra errors and handle error printing and process exit in `Main`.
- Refactored multiple commands (QA, SDK, CI, Updater) to return errors instead of exiting abruptly.
- Replaced direct `os.Stderr` writes with standardized CLI or log helpers across the codebase.
- Updated tests to accommodate changes in output destination.
- Fixed formatting in `pkg/io/local/client.go`.
- Removed unused `fmt` import in `pkg/mcp/transport_tcp.go`.
- Fixed potential `gh` context issue in `.github/workflows/auto-merge.yml` by providing `GH_REPO`.
---------
Co-authored-by: Claude <developers@lethean.io>
2026-02-05 06:55:49 +00:00
|
|
|
Error(err.Error())
|
|
|
|
|
os.Exit(1)
|
2026-01-30 10:55:30 +00:00
|
|
|
}
|
2026-01-30 22:11:35 +00:00
|
|
|
defer Shutdown()
|
2026-01-30 10:55:30 +00:00
|
|
|
|
refactor(cli): move commands from cmd/ to pkg/ with self-registration
Implements defence in depth through build variants - only compiled code
exists in the binary. Commands now self-register via cli.RegisterCommands()
in their init() functions, mirroring the i18n.RegisterLocales() pattern.
Structure changes:
- cmd/{ai,build,ci,dev,docs,doctor,go,php,pkg,sdk,setup,test,vm}/ → pkg/*/cmd_*.go
- cmd/core_dev.go, cmd/core_ci.go → cmd/variants/{full,ci,php,minimal}.go
- Added pkg/cli/commands.go with RegisterCommands API
- Updated pkg/cli/runtime.go to attach registered commands
Build variants:
- go build → full (21MB, all 13 command groups)
- go build -tags ci → ci (18MB, build/ci/sdk/doctor)
- go build -tags php → php (14MB, php/doctor)
- go build -tags minimal → minimal (11MB, doctor only)
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-30 21:55:55 +00:00
|
|
|
// Add completion command to the CLI's root
|
2026-01-30 22:11:35 +00:00
|
|
|
RootCmd().AddCommand(completionCmd)
|
2026-01-30 00:47:54 +00:00
|
|
|
|
Standardize CLI Error Handling (#318)
* Standardize CLI error handling and deprecate cli.Fatal
- Updated `pkg/cli/output.go` to send error and warning output to `os.Stderr`.
- Added `ErrorWrap`, `ErrorWrapVerb`, and `ErrorWrapAction` helpers to `pkg/cli/output.go`.
- Deprecated `cli.Fatal` family of functions in `pkg/cli/errors.go`.
- Introduced `cli.ExitError` and `cli.Exit` helper to allow commands to return specific exit codes.
- Updated `pkg/cli/app.go` to silence Cobra errors and handle error printing and process exit in `Main`.
- Refactored multiple commands (QA, SDK, CI, Updater) to return errors instead of exiting abruptly.
- Replaced direct `os.Stderr` writes with standardized CLI or log helpers across the codebase.
- Updated tests to accommodate changes in output destination.
* Fix CI failure: remove unused fmt import in pkg/mcp/transport_tcp.go
- Removed unused "fmt" import in `pkg/mcp/transport_tcp.go` that was causing CI failure.
- Verified build and relevant tests pass.
* Standardize CLI error handling and fix formatting issues
- Updated `pkg/cli/output.go` to send error and warning output to `os.Stderr`.
- Added `ErrorWrap`, `ErrorWrapVerb`, and `ErrorWrapAction` helpers to `pkg/cli/output.go`.
- Deprecated `cli.Fatal` family of functions in `pkg/cli/errors.go`.
- Introduced `cli.ExitError` and `cli.Exit` helper to allow commands to return specific exit codes.
- Updated `pkg/cli/app.go` to silence Cobra errors and handle error printing and process exit in `Main`.
- Refactored multiple commands (QA, SDK, CI, Updater) to return errors instead of exiting abruptly.
- Replaced direct `os.Stderr` writes with standardized CLI or log helpers across the codebase.
- Updated tests to accommodate changes in output destination.
- Fixed formatting in `pkg/io/local/client.go`.
- Removed unused `fmt` import in `pkg/mcp/transport_tcp.go`.
* Standardize CLI error handling and fix CI issues
- Updated `pkg/cli/output.go` to send error and warning output to `os.Stderr`.
- Added `ErrorWrap`, `ErrorWrapVerb`, and `ErrorWrapAction` helpers to `pkg/cli/output.go`.
- Deprecated `cli.Fatal` family of functions in `pkg/cli/errors.go`.
- Introduced `cli.ExitError` and `cli.Exit` helper to allow commands to return specific exit codes.
- Updated `pkg/cli/app.go` to silence Cobra errors and handle error printing and process exit in `Main`.
- Refactored multiple commands (QA, SDK, CI, Updater) to return errors instead of exiting abruptly.
- Replaced direct `os.Stderr` writes with standardized CLI or log helpers across the codebase.
- Updated tests to accommodate changes in output destination.
- Fixed formatting in `pkg/io/local/client.go`.
- Removed unused `fmt` import in `pkg/mcp/transport_tcp.go`.
- Fixed potential `gh` context issue in `.github/workflows/auto-merge.yml` by providing `GH_REPO`.
---------
Co-authored-by: Claude <developers@lethean.io>
2026-02-05 06:55:49 +00:00
|
|
|
if err := Execute(); err != nil {
|
|
|
|
|
code := 1
|
|
|
|
|
var exitErr *ExitError
|
|
|
|
|
if As(err, &exitErr) {
|
|
|
|
|
code = exitErr.Code
|
|
|
|
|
}
|
|
|
|
|
Error(err.Error())
|
|
|
|
|
os.Exit(code)
|
|
|
|
|
}
|
2026-01-30 00:47:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// completionCmd generates shell completion scripts.
|
|
|
|
|
var completionCmd = &cobra.Command{
|
|
|
|
|
Use: "completion [bash|zsh|fish|powershell]",
|
|
|
|
|
Short: "Generate shell completion script",
|
|
|
|
|
Long: `Generate shell completion script for the specified shell.
|
|
|
|
|
|
|
|
|
|
To load completions:
|
|
|
|
|
|
|
|
|
|
Bash:
|
|
|
|
|
$ source <(core completion bash)
|
|
|
|
|
|
|
|
|
|
# To load completions for each session, execute once:
|
|
|
|
|
# Linux:
|
|
|
|
|
$ core completion bash > /etc/bash_completion.d/core
|
|
|
|
|
# macOS:
|
|
|
|
|
$ core completion bash > $(brew --prefix)/etc/bash_completion.d/core
|
|
|
|
|
|
|
|
|
|
Zsh:
|
|
|
|
|
# If shell completion is not already enabled in your environment,
|
|
|
|
|
# you will need to enable it. You can execute the following once:
|
|
|
|
|
$ echo "autoload -U compinit; compinit" >> ~/.zshrc
|
|
|
|
|
|
|
|
|
|
# To load completions for each session, execute once:
|
|
|
|
|
$ core completion zsh > "${fpath[1]}/_core"
|
|
|
|
|
|
|
|
|
|
# You will need to start a new shell for this setup to take effect.
|
|
|
|
|
|
|
|
|
|
Fish:
|
|
|
|
|
$ core completion fish | source
|
|
|
|
|
|
|
|
|
|
# To load completions for each session, execute once:
|
|
|
|
|
$ core completion fish > ~/.config/fish/completions/core.fish
|
|
|
|
|
|
|
|
|
|
PowerShell:
|
|
|
|
|
PS> core completion powershell | Out-String | Invoke-Expression
|
|
|
|
|
|
|
|
|
|
# To load completions for every new session, run:
|
|
|
|
|
PS> core completion powershell > core.ps1
|
|
|
|
|
# and source this file from your PowerShell profile.
|
|
|
|
|
`,
|
|
|
|
|
DisableFlagsInUseLine: true,
|
|
|
|
|
ValidArgs: []string{"bash", "zsh", "fish", "powershell"},
|
|
|
|
|
Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
|
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
|
switch args[0] {
|
|
|
|
|
case "bash":
|
|
|
|
|
_ = cmd.Root().GenBashCompletion(os.Stdout)
|
|
|
|
|
case "zsh":
|
|
|
|
|
_ = cmd.Root().GenZshCompletion(os.Stdout)
|
|
|
|
|
case "fish":
|
|
|
|
|
_ = cmd.Root().GenFishCompletion(os.Stdout, true)
|
|
|
|
|
case "powershell":
|
|
|
|
|
_ = cmd.Root().GenPowerShellCompletionWithDesc(os.Stdout)
|
|
|
|
|
}
|
|
|
|
|
},
|
2025-10-27 05:07:22 +00:00
|
|
|
}
|