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.
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
printVersionHeader(os.Args[1:])
|
||||||
if err := rootCmd.Execute(); err != nil {
|
if err := rootCmd.Execute(); err != nil {
|
||||||
fmt.Fprintln(os.Stderr, err)
|
fmt.Fprintln(os.Stderr, err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
|||||||
+19
-3
@@ -13,6 +13,7 @@ var (
|
|||||||
version = "dev"
|
version = "dev"
|
||||||
buildDate = "unknown"
|
buildDate = "unknown"
|
||||||
prisma7 bool
|
prisma7 bool
|
||||||
|
noVersion bool
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@@ -54,9 +55,6 @@ bidirectional conversion between various database schema formats.
|
|||||||
It reads database schemas from multiple sources (live databases, DBML,
|
It reads database schemas from multiple sources (live databases, DBML,
|
||||||
DCTX, DrawDB, etc.) and writes them to various formats (GORM, Bun,
|
DCTX, DrawDB, etc.) and writes them to various formats (GORM, Bun,
|
||||||
JSON, YAML, SQL, etc.).`,
|
JSON, YAML, SQL, etc.).`,
|
||||||
PersistentPreRun: func(cmd *cobra.Command, args []string) {
|
|
||||||
fmt.Printf("RelSpec %s (built: %s)\n\n", version, buildDate)
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@@ -72,4 +70,22 @@ func init() {
|
|||||||
rootCmd.AddCommand(versionCmd)
|
rootCmd.AddCommand(versionCmd)
|
||||||
rootCmd.AddCommand(reportCmd)
|
rootCmd.AddCommand(reportCmd)
|
||||||
rootCmd.PersistentFlags().BoolVar(&prisma7, "prisma7", false, "Use Prisma 7 generator conventions when reading/writing Prisma schemas")
|
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 <version> (built: <date>)" 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)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user