2026-01-30 10:32:05 +00:00
|
|
|
// Package cli provides the CLI runtime and utilities.
|
|
|
|
|
//
|
2026-01-30 10:41:35 +00:00
|
|
|
// The CLI uses the Core framework for its own runtime. Usage is simple:
|
|
|
|
|
//
|
|
|
|
|
// cli.Init(cli.Options{AppName: "core"})
|
|
|
|
|
// defer cli.Shutdown()
|
|
|
|
|
//
|
|
|
|
|
// cli.Success("Done!")
|
|
|
|
|
// cli.Error("Failed")
|
|
|
|
|
// if cli.Confirm("Proceed?") { ... }
|
|
|
|
|
//
|
|
|
|
|
// // When you need the Core instance
|
|
|
|
|
// c := cli.Core()
|
2026-01-30 10:32:05 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"os"
|
|
|
|
|
"os/signal"
|
|
|
|
|
"sync"
|
|
|
|
|
"syscall"
|
|
|
|
|
|
|
|
|
|
"github.com/host-uk/core/pkg/framework"
|
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
|
|
|
"github.com/spf13/cobra"
|
2026-01-30 10:32:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
2026-01-30 10:41:35 +00:00
|
|
|
instance *runtime
|
2026-01-30 10:32:05 +00:00
|
|
|
once sync.Once
|
|
|
|
|
)
|
|
|
|
|
|
2026-01-30 10:41:35 +00:00
|
|
|
// runtime is the CLI's internal Core runtime.
|
|
|
|
|
type runtime struct {
|
|
|
|
|
core *framework.Core
|
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
|
|
|
root *cobra.Command
|
2026-01-30 10:32:05 +00:00
|
|
|
ctx context.Context
|
|
|
|
|
cancel context.CancelFunc
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-30 10:41:35 +00:00
|
|
|
// Options configures the CLI runtime.
|
|
|
|
|
type Options struct {
|
2026-01-30 10:55:30 +00:00
|
|
|
AppName string
|
|
|
|
|
Version string
|
|
|
|
|
Services []framework.Option // Additional services to register
|
2026-01-30 19:28:50 +00:00
|
|
|
|
|
|
|
|
// OnReload is called when SIGHUP is received (daemon mode).
|
|
|
|
|
// Use for configuration reloading. Leave nil to ignore SIGHUP.
|
|
|
|
|
OnReload func() error
|
2026-01-30 10:32:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Init initialises the global CLI runtime.
|
2026-01-30 10:55:30 +00:00
|
|
|
// Call this once at startup (typically in main.go or cmd.Execute).
|
2026-01-30 10:41:35 +00:00
|
|
|
func Init(opts Options) error {
|
2026-01-30 10:32:05 +00:00
|
|
|
var initErr error
|
|
|
|
|
once.Do(func() {
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
|
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
|
|
|
// Create root command
|
|
|
|
|
rootCmd := &cobra.Command{
|
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
|
|
|
Use: opts.AppName,
|
|
|
|
|
Version: opts.Version,
|
|
|
|
|
SilenceErrors: true,
|
|
|
|
|
SilenceUsage: true,
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Attach all registered commands
|
|
|
|
|
attachRegisteredCommands(rootCmd)
|
|
|
|
|
|
2026-01-30 19:28:50 +00:00
|
|
|
// Build signal service options
|
|
|
|
|
var signalOpts []SignalOption
|
|
|
|
|
if opts.OnReload != nil {
|
|
|
|
|
signalOpts = append(signalOpts, WithReloadHandler(opts.OnReload))
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
// Build options: app, signal service + any additional services
|
2026-01-30 10:55:30 +00:00
|
|
|
coreOpts := []framework.Option{
|
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
|
|
|
framework.WithApp(rootCmd),
|
2026-01-30 19:28:50 +00:00
|
|
|
framework.WithName("signal", newSignalService(cancel, signalOpts...)),
|
2026-01-30 10:55:30 +00:00
|
|
|
}
|
|
|
|
|
coreOpts = append(coreOpts, opts.Services...)
|
|
|
|
|
coreOpts = append(coreOpts, framework.WithServiceLock())
|
|
|
|
|
|
|
|
|
|
c, err := framework.New(coreOpts...)
|
2026-01-30 10:32:05 +00:00
|
|
|
if err != nil {
|
|
|
|
|
initErr = err
|
|
|
|
|
cancel()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-30 10:41:35 +00:00
|
|
|
instance = &runtime{
|
|
|
|
|
core: c,
|
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
|
|
|
root: rootCmd,
|
2026-01-30 10:32:05 +00:00
|
|
|
ctx: ctx,
|
|
|
|
|
cancel: cancel,
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-30 10:41:35 +00:00
|
|
|
if err := c.ServiceStartup(ctx, nil); err != nil {
|
2026-01-30 10:32:05 +00:00
|
|
|
initErr = err
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
return initErr
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-30 10:41:35 +00:00
|
|
|
func mustInit() {
|
2026-01-30 10:32:05 +00:00
|
|
|
if instance == nil {
|
2026-01-30 10:41:35 +00:00
|
|
|
panic("cli not initialised - call cli.Init() first")
|
2026-01-30 10:32:05 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-30 10:41:35 +00:00
|
|
|
// --- Core Access ---
|
2026-01-30 10:32:05 +00:00
|
|
|
|
2026-01-30 10:41:35 +00:00
|
|
|
// Core returns the CLI's framework Core instance.
|
|
|
|
|
func Core() *framework.Core {
|
|
|
|
|
mustInit()
|
|
|
|
|
return instance.core
|
2026-01-30 10:32:05 +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
|
|
|
// RootCmd returns the CLI's root cobra command.
|
|
|
|
|
func RootCmd() *cobra.Command {
|
|
|
|
|
mustInit()
|
|
|
|
|
return instance.root
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Execute runs the CLI root command.
|
|
|
|
|
// Returns an error if the command fails.
|
|
|
|
|
func Execute() error {
|
|
|
|
|
mustInit()
|
|
|
|
|
return instance.root.Execute()
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-30 10:41:35 +00:00
|
|
|
// Context returns the CLI's root context.
|
|
|
|
|
// Cancelled on SIGINT/SIGTERM.
|
|
|
|
|
func Context() context.Context {
|
|
|
|
|
mustInit()
|
|
|
|
|
return instance.ctx
|
2026-01-30 10:32:05 +00:00
|
|
|
}
|
|
|
|
|
|
2026-01-30 10:41:35 +00:00
|
|
|
// Shutdown gracefully shuts down the CLI.
|
|
|
|
|
func Shutdown() {
|
|
|
|
|
if instance == nil {
|
|
|
|
|
return
|
2026-01-30 10:32:05 +00:00
|
|
|
}
|
2026-01-30 10:41:35 +00:00
|
|
|
instance.cancel()
|
feat: git command, build improvements, and go fmt git-aware (#74)
* feat(go): make go fmt git-aware by default
- By default, only check changed Go files (modified, staged, untracked)
- Add --all flag to check all files (previous behaviour)
- Reduces noise when running fmt on large codebases
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* feat(build): minimal output by default, add missing i18n
- Default output now shows single line: "Success Built N artifacts (dir)"
- Add --verbose/-v flag to show full detailed output
- Add all missing i18n translations for build commands
- Errors still show failure reason in minimal mode
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* feat: add root-level `core git` command
- Create pkg/gitcmd with git workflow commands as root menu
- Export command builders from pkg/dev (AddCommitCommand, etc.)
- Commands available under both `core git` and `core dev` for compatibility
- Git commands: health, commit, push, pull, work, sync, apply
- GitHub orchestration stays in dev: issues, reviews, ci, impact
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* feat(qa): add docblock coverage checking
Implement docblock/docstring coverage analysis for Go code:
- New `core qa docblock` command to check coverage
- Shows compact file:line list when under threshold
- Integrate with `core go qa` as a default check
- Add --docblock-threshold flag (default 80%)
The checker uses Go AST parsing to find exported symbols
(functions, types, consts, vars) without documentation.
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* fix: address CodeRabbit review feedback
- Fix doc comment: "status" → "health" in gitcmd package
- Implement --check flag for `core go fmt` (exits non-zero if files need formatting)
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* docs: add docstrings for 100% coverage
Add documentation comments to all exported symbols:
- pkg/build: ProjectType constants
- pkg/cli: LogLevel, RenderStyle, TableStyle
- pkg/framework: ServiceFor, MustServiceFor, Core.Core
- pkg/git: GitError.Error, GitError.Unwrap
- pkg/i18n: Handler Match/Handle methods
- pkg/log: Level constants
- pkg/mcp: Tool input/output types
- pkg/php: Service constants, QA types, service methods
- pkg/process: ServiceError.Error
- pkg/repos: RepoType constants
- pkg/setup: ChangeType, ChangeCategory constants
- pkg/workspace: AddWorkspaceCommands
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* chore: standardize line endings to LF
Add .gitattributes to enforce LF line endings for all text files.
Normalize all existing files to use Unix-style line endings.
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* fix: address CodeRabbit review feedback
- cmd_format.go: validate --check/--fix mutual exclusivity, capture stderr
- cmd_docblock.go: return error instead of os.Exit(1) for proper error handling
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* fix: address CodeRabbit review feedback (round 2)
- linuxkit.go: propagate state update errors, handle cmd.Wait() errors in waitForExit
- mcp.go: guard against empty old_string in editDiff to prevent runaway edits
- cmd_docblock.go: log parse errors instead of silently skipping
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-01 10:48:44 +00:00
|
|
|
_ = instance.core.ServiceShutdown(instance.ctx)
|
2026-01-30 10:32:05 +00:00
|
|
|
}
|
|
|
|
|
|
2026-01-30 10:41:35 +00:00
|
|
|
// --- Signal Service (internal) ---
|
2026-01-30 10:32:05 +00:00
|
|
|
|
2026-01-30 10:41:35 +00:00
|
|
|
type signalService struct {
|
2026-02-05 07:52:23 +00:00
|
|
|
cancel context.CancelFunc
|
|
|
|
|
sigChan chan os.Signal
|
|
|
|
|
onReload func() error
|
|
|
|
|
shutdownOnce sync.Once
|
2026-01-30 19:28:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SignalOption configures signal handling.
|
|
|
|
|
type SignalOption func(*signalService)
|
|
|
|
|
|
|
|
|
|
// WithReloadHandler sets a callback for SIGHUP.
|
|
|
|
|
func WithReloadHandler(fn func() error) SignalOption {
|
|
|
|
|
return func(s *signalService) {
|
|
|
|
|
s.onReload = fn
|
|
|
|
|
}
|
2026-01-30 10:32:05 +00:00
|
|
|
}
|
|
|
|
|
|
2026-01-30 19:28:50 +00:00
|
|
|
func newSignalService(cancel context.CancelFunc, opts ...SignalOption) func(*framework.Core) (any, error) {
|
2026-01-30 10:32:05 +00:00
|
|
|
return func(c *framework.Core) (any, error) {
|
2026-01-30 19:28:50 +00:00
|
|
|
svc := &signalService{
|
2026-01-30 10:41:35 +00:00
|
|
|
cancel: cancel,
|
|
|
|
|
sigChan: make(chan os.Signal, 1),
|
2026-01-30 19:28:50 +00:00
|
|
|
}
|
|
|
|
|
for _, opt := range opts {
|
|
|
|
|
opt(svc)
|
|
|
|
|
}
|
|
|
|
|
return svc, nil
|
2026-01-30 10:32:05 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-30 10:41:35 +00:00
|
|
|
func (s *signalService) OnStartup(ctx context.Context) error {
|
2026-01-30 19:28:50 +00:00
|
|
|
signals := []os.Signal{syscall.SIGINT, syscall.SIGTERM}
|
|
|
|
|
if s.onReload != nil {
|
|
|
|
|
signals = append(signals, syscall.SIGHUP)
|
|
|
|
|
}
|
|
|
|
|
signal.Notify(s.sigChan, signals...)
|
2026-01-30 10:32:05 +00:00
|
|
|
|
|
|
|
|
go func() {
|
2026-01-30 19:28:50 +00:00
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case sig := <-s.sigChan:
|
|
|
|
|
switch sig {
|
|
|
|
|
case syscall.SIGHUP:
|
|
|
|
|
if s.onReload != nil {
|
|
|
|
|
if err := s.onReload(); err != nil {
|
Log all errors at handling point with contextual information (#321)
* feat(log): log all errors at handling point with context
This change ensures all errors are logged at the point where they are
handled, including contextual information such as operations and
logical stack traces.
Key changes:
- Added `StackTrace` and `FormatStackTrace` to `pkg/log/errors.go`.
- Enhanced `Logger.log` in `pkg/log/log.go` to automatically extract
and log `op` and `stack` keys when an error is passed in keyvals.
- Updated CLI logging and output helpers to support structured logging.
- Updated CLI fatal error handlers to log errors before exiting.
- Audited and updated error logging in MCP service (tool handlers and
TCP transport), CLI background services (signal and health), and
Agentic task handlers.
* feat(log): log all errors at handling point with context
This change ensures all errors are logged at the point where they are
handled, including contextual information such as operations and
logical stack traces.
Key changes:
- Added `StackTrace` and `FormatStackTrace` to `pkg/log/errors.go`.
- Enhanced `Logger.log` in `pkg/log/log.go` to automatically extract
and log `op` and `stack` keys when an error is passed in keyvals.
- Updated CLI logging and output helpers to support structured logging.
- Updated CLI fatal error handlers to log errors before exiting.
- Audited and updated error logging in MCP service (tool handlers and
TCP transport), CLI background services (signal and health), and
Agentic task handlers.
- Fixed formatting in `pkg/mcp/mcp.go` and `pkg/io/local/client.go`.
- Removed unused `fmt` import in `pkg/cli/runtime.go`.
* feat(log): log all errors at handling point with context
This change ensures all errors are logged at the point where they are
handled, including contextual information such as operations and
logical stack traces.
Key changes:
- Added `StackTrace` and `FormatStackTrace` to `pkg/log/errors.go`.
- Enhanced `Logger.log` in `pkg/log/log.go` to automatically extract
and log `op` and `stack` keys when an error is passed in keyvals.
- Updated CLI logging and output helpers to support structured logging.
- Updated CLI fatal error handlers to log errors before exiting.
- Audited and updated error logging in MCP service (tool handlers and
TCP transport), CLI background services (signal and health), and
Agentic task handlers.
- Fixed formatting in `pkg/mcp/mcp.go` and `pkg/io/local/client.go`.
- Removed unused `fmt` import in `pkg/cli/runtime.go`.
- Fixed CI failure in `auto-merge` workflow by providing explicit
repository context to the GitHub CLI.
* feat(log): address PR feedback and improve error context extraction
Addressed feedback from PR review:
- Improved `Fatalf` and other fatal functions in `pkg/cli/errors.go` to
use structured logging for the formatted message.
- Added direct unit tests for `StackTrace` and `FormatStackTrace` in
`pkg/log/errors_test.go`, covering edge cases like plain errors,
nil errors, and mixed error chains.
- Optimized the automatic context extraction loop in `pkg/log/log.go`
by capturing the original length of keyvals.
- Fixed a bug in `StackTrace` where operations were duplicated when
the error chain included non-`*log.Err` errors.
- Fixed formatting and unused imports from previous commits.
* fix: address code review comments
- Simplify Fatalf logging by removing redundant format parameter
(the formatted message is already logged as "msg")
- Tests for StackTrace/FormatStackTrace edge cases already exist
- Loop optimization in pkg/log/log.go already implemented
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude <developers@lethean.io>
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-05 07:52:25 +00:00
|
|
|
LogError("reload failed", "err", err)
|
2026-01-30 19:28:50 +00:00
|
|
|
} else {
|
|
|
|
|
LogInfo("configuration reloaded")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
case syscall.SIGINT, syscall.SIGTERM:
|
|
|
|
|
s.cancel()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-01-30 10:32:05 +00:00
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-30 10:41:35 +00:00
|
|
|
func (s *signalService) OnShutdown(ctx context.Context) error {
|
2026-02-05 07:52:23 +00:00
|
|
|
s.shutdownOnce.Do(func() {
|
|
|
|
|
signal.Stop(s.sigChan)
|
|
|
|
|
close(s.sigChan)
|
|
|
|
|
})
|
2026-01-30 10:32:05 +00:00
|
|
|
return nil
|
|
|
|
|
}
|