From 241bfc23023af2c23b2011e0302fdfe79cc8ca09 Mon Sep 17 00:00:00 2001 From: Hein Date: Mon, 24 Aug 2026 12:59:14 +0200 Subject: [PATCH] feat(cli): always print version header first, add --no-version flag Previously the version banner only printed via PersistentPreRun, which Cobra skips for --help and bare invocations. It now prints from main() before Cobra parses anything, so it's the first line for every command. Suppressible with --no-version; skipped for the version subcommand to avoid duplicating its own output. --- cmd/relspec/main.go | 1 + cmd/relspec/root.go | 22 +++++++++++++++++++--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/cmd/relspec/main.go b/cmd/relspec/main.go index a5a50d9..87b12a7 100644 --- a/cmd/relspec/main.go +++ b/cmd/relspec/main.go @@ -6,6 +6,7 @@ import ( ) func main() { + printVersionHeader(os.Args[1:]) if err := rootCmd.Execute(); err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) diff --git a/cmd/relspec/root.go b/cmd/relspec/root.go index b2847ca..df46d41 100644 --- a/cmd/relspec/root.go +++ b/cmd/relspec/root.go @@ -13,6 +13,7 @@ var ( version = "dev" buildDate = "unknown" prisma7 bool + noVersion bool ) func init() { @@ -54,9 +55,6 @@ bidirectional conversion between various database schema formats. It reads database schemas from multiple sources (live databases, DBML, DCTX, DrawDB, etc.) and writes them to various formats (GORM, Bun, JSON, YAML, SQL, etc.).`, - PersistentPreRun: func(cmd *cobra.Command, args []string) { - fmt.Printf("RelSpec %s (built: %s)\n\n", version, buildDate) - }, } func init() { @@ -72,4 +70,22 @@ func init() { rootCmd.AddCommand(versionCmd) rootCmd.AddCommand(reportCmd) rootCmd.PersistentFlags().BoolVar(&prisma7, "prisma7", false, "Use Prisma 7 generator conventions when reading/writing Prisma schemas") + rootCmd.PersistentFlags().BoolVar(&noVersion, "no-version", false, "Suppress the RelSpec version header") +} + +// printVersionHeader prints the "RelSpec (built: )" banner +// that precedes all command output. It is invoked from main() before cobra +// parses/executes anything, so it runs even for --help and bare invocations. +// It is skipped when --no-version is present, or when the version subcommand +// is being run (which prints its own, more detailed output). +func printVersionHeader(args []string) { + for _, a := range args { + if a == "--no-version" { + return + } + } + if len(args) > 0 && args[0] == "version" { + return + } + fmt.Printf("RelSpec %s (built: %s)\n\n", version, buildDate) }