Documentation
¶
Overview ¶
Package cli runs caller-owned urfave/cli trees inside selected lifecycle modules. It owns temporary signal subscriptions and exit classification, not process exit, configuration, resource handles or unsupervised goroutines.
Example ¶
package main
import (
"context"
"fmt"
"io"
"log/slog"
urfavecli "github.com/urfave/cli/v3"
"go.auvem.com/corestack/lifecycle"
"go.auvem.com/corestack/lifecycle/cli"
)
func main() {
cfg := lifecycle.Config{Logger: slog.New(slog.NewTextHandler(io.Discard, nil))}
for _, args := range [][]string{{"app", "serve"}, {"app", "inspect", "--verbose"}} {
var database string
db := lifecycle.Module{Name: "database",
Setup: func(context.Context) error { database = "connected"; fmt.Println("open database"); return nil },
Teardown: func(context.Context) error { fmt.Println("close database"); database = ""; return nil },
}
server := lifecycle.Module{Name: "server",
Setup: func(context.Context) error { fmt.Println("open server"); return nil },
// A real server blocks until cancellation; this finite service keeps the example executable.
Run: func(context.Context) error { fmt.Println("serve:", database); return nil },
Teardown: func(context.Context) error { fmt.Println("close server"); return nil },
}
cliRoot := &urfavecli.Command{Name: "app", Commands: []*urfavecli.Command{
{Name: "serve", Action: cli.Serve(cfg, []lifecycle.Module{db, server})},
{Name: "inspect", Flags: []urfavecli.Flag{&urfavecli.BoolFlag{Name: "verbose"}},
Action: cli.Action(cfg, []lifecycle.Module{db}, func(_ context.Context, cmd *urfavecli.Command) error {
fmt.Println("inspect:", database, "verbose:", cmd.Bool("verbose"))
return nil
})},
}}
code, err := cli.Run(context.Background(), cliRoot, args)
fmt.Println("exit:", code, err)
// main may call os.Exit(code) here, after adapter cleanup.
}
}
Output: open database open server serve: connected close server close database exit: 0 <nil> open database inspect: connected verbose: true close database exit: 0 <nil>
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Action ¶
func Action(config lifecycle.Config, modules []lifecycle.Module, fn urfavecli.ActionFunc) urfavecli.ActionFunc
Action returns a native one-off action using exactly modules with their Run callbacks suppressed. All setups precede fn; completion triggers reverse cleanup. fn receives lifecycle's signal-aware context and the selected native command. Nil fn is a runtime configuration error. Signal and ownership rules match Serve; hooks outside this action are outside the lifecycle shutdown guarantee.
func Run ¶
Run executes cliRoot with args including argv[0], returning 0 for success/help, 2 for usage, and 1 for runtime failures. Lifecycle failures always take priority. It prints a final diagnostic to cliRoot.ErrWriter and never exits the process. Run reserves ExitErrHandler, OnUsageError and CommandNotFound throughout the declared tree. Construct fresh trees for independent runs; do not mutate them concurrently. Native generated-help parse and environment validation failures may return 1 and print native diagnostics in addition to the final diagnostic.
func Serve ¶
Serve returns a native action running exactly modules, including their services, in registration order with reverse cleanup. Bindings capture declarations without starting work; resources and declarations must not be mutated concurrently. Nil config.Signals subscribes to SIGINT/SIGTERM for the invocation, buffered for two signals; an injected channel and its subscription remain caller-owned. The private invocation App is unavailable for managed-worker admission.
Types ¶
This section is empty.