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 <noreply@anthropic.com>
This commit is contained in:
Snider 2026-01-30 22:16:34 +00:00
parent 5b2c4eef75
commit 6b76b4d37f
3 changed files with 13 additions and 10 deletions

View file

@ -1,9 +1,6 @@
package main package main
import ( import (
"fmt"
"os"
"github.com/host-uk/core/pkg/cli" "github.com/host-uk/core/pkg/cli"
// Build variants import commands via self-registration. // Build variants import commands via self-registration.
@ -12,8 +9,5 @@ import (
) )
func main() { func main() {
if err := cli.Main(); err != nil { cli.Main()
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
} }

View file

@ -17,7 +17,8 @@ const (
// Main initialises and runs the CLI application. // Main initialises and runs the CLI application.
// This is the main entry point for the CLI. // 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 // Initialise CLI runtime with services
if err := Init(Options{ if err := Init(Options{
AppName: AppName, AppName: AppName,
@ -29,14 +30,14 @@ func Main() error {
})), })),
}, },
}); err != nil { }); err != nil {
return err Fatal(err)
} }
defer Shutdown() defer Shutdown()
// Add completion command to the CLI's root // Add completion command to the CLI's root
RootCmd().AddCommand(completionCmd) RootCmd().AddCommand(completionCmd)
return Execute() Fatal(Execute())
} }
// completionCmd generates shell completion scripts. // completionCmd generates shell completion scripts.

View file

@ -156,6 +156,14 @@ func Error(msg string) {
fmt.Println(ErrorStyle.Render(SymbolCross + " " + msg)) 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. // Warning prints a warning message.
func Warning(msg string) { func Warning(msg string) {
fmt.Println(WarningStyle.Render(SymbolWarning + " " + msg)) fmt.Println(WarningStyle.Render(SymbolWarning + " " + msg))