From 6b76b4d37f8b1a0d6441d0616396750f6b7d7717 Mon Sep 17 00:00:00 2001 From: Snider Date: Fri, 30 Jan 2026 22:16:34 +0000 Subject: [PATCH] refactor(cli): add Fatal() and simplify main entry point - Add cli.Fatal(err) that prints styled error and exits - Change cli.Main() to handle errors internally via Fatal() - Simplify main.go to just: cli.Main() Co-Authored-By: Claude Opus 4.5 --- main.go | 8 +------- pkg/cli/app.go | 7 ++++--- pkg/cli/runtime.go | 8 ++++++++ 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/main.go b/main.go index 38a51c35..1a852756 100644 --- a/main.go +++ b/main.go @@ -1,9 +1,6 @@ package main import ( - "fmt" - "os" - "github.com/host-uk/core/pkg/cli" // Build variants import commands via self-registration. @@ -12,8 +9,5 @@ import ( ) func main() { - if err := cli.Main(); err != nil { - fmt.Fprintln(os.Stderr, err) - os.Exit(1) - } + cli.Main() } diff --git a/pkg/cli/app.go b/pkg/cli/app.go index 7a2e0147..9a3c0a09 100644 --- a/pkg/cli/app.go +++ b/pkg/cli/app.go @@ -17,7 +17,8 @@ const ( // Main initialises and runs the CLI application. // This is the main entry point for the CLI. -func Main() error { +// Exits with code 1 on error. +func Main() { // Initialise CLI runtime with services if err := Init(Options{ AppName: AppName, @@ -29,14 +30,14 @@ func Main() error { })), }, }); err != nil { - return err + Fatal(err) } defer Shutdown() // Add completion command to the CLI's root RootCmd().AddCommand(completionCmd) - return Execute() + Fatal(Execute()) } // completionCmd generates shell completion scripts. diff --git a/pkg/cli/runtime.go b/pkg/cli/runtime.go index cd160e6c..6054ef34 100644 --- a/pkg/cli/runtime.go +++ b/pkg/cli/runtime.go @@ -156,6 +156,14 @@ func Error(msg string) { fmt.Println(ErrorStyle.Render(SymbolCross + " " + msg)) } +// Fatal prints an error message and exits with code 1. +func Fatal(err error) { + if err != nil { + Error(err.Error()) + os.Exit(1) + } +} + // Warning prints a warning message. func Warning(msg string) { fmt.Println(WarningStyle.Render(SymbolWarning + " " + msg))