1 Runtime
Virgil edited this page 2026-02-23 04:54:00 +00:00

Runtime

Initialisation

// Simple (recommended)
cli.WithAppName("myapp")
cli.Main(
    cli.WithCommands("cmd", addCommands),
)

// Manual (advanced)
cli.Init(cli.Options{
    AppName: "myapp",
    Version: "1.0.0",
    Services: []framework.Option{...},
    OnReload: func() error { return reloadConfig() },
})
defer cli.Shutdown()
cli.Execute()

Global Accessors

Function Returns Description
cli.Core() *framework.Core Framework container
cli.RootCmd() *cobra.Command Root cobra command
cli.Context() context.Context Cancelled on SIGINT/SIGTERM
cli.Execute() error Run matched command
cli.Shutdown() Stop all services

Version Info

Set via ldflags at build time:

cli.AppVersion      // "1.2.0"
cli.BuildCommit     // "df94c24"
cli.BuildDate       // "2026-02-06"
cli.BuildPreRelease // "dev.8"
cli.SemVer()        // "1.2.0-dev.8+df94c24.20260206"

Build command:

go build -ldflags="-X forge.lthn.ai/core/cli/pkg/cli.AppVersion=1.2.0 \
  -X forge.lthn.ai/core/cli/pkg/cli.BuildCommit=$(git rev-parse --short HEAD) \
  -X forge.lthn.ai/core/cli/pkg/cli.BuildDate=$(date +%Y-%m-%d)"

Signal Handling

Automatic via the signalService registered during Init():

  • SIGINT/SIGTERM → cancels cli.Context()
  • SIGHUP → calls OnReload if configured

No manual signal handling needed in commands — use cli.Context() for cancellation.