Summary
The root command is missing two critical Cobra settings, and errors are printed to stdout instead of stderr.
1. Missing SilenceUsage and SilenceErrors
main.go:13-16:
rootCmd := &cobra.Command{
Use: "gorm",
Short: "GORM CLI Tool",
}
Without SilenceUsage: true, Cobra prints the full usage/help text on every RunE error. Without SilenceErrors: true, Cobra also prints the error itself, so the manual fmt.Println(err) in main causes the error to appear twice.
2. Error printed to stdout
main.go:22:
This writes to stdout. Errors must go to stderr so they don't corrupt piped output when the CLI is used in pipelines.
Fix
rootCmd := &cobra.Command{
Use: "gorm",
Short: "GORM CLI Tool",
SilenceUsage: true,
SilenceErrors: true,
}
// In main():
if err := rootCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
References
Summary
The root command is missing two critical Cobra settings, and errors are printed to stdout instead of stderr.
1. Missing
SilenceUsageandSilenceErrorsmain.go:13-16:Without
SilenceUsage: true, Cobra prints the full usage/help text on everyRunEerror. WithoutSilenceErrors: true, Cobra also prints the error itself, so the manualfmt.Println(err)in main causes the error to appear twice.2. Error printed to stdout
main.go:22:This writes to stdout. Errors must go to stderr so they don't corrupt piped output when the CLI is used in pipelines.
Fix
References