fix: set SilenceUsage/SilenceErrors and print errors to stderr

Open Beginner friendly
#57 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
1/5
Estimated time
Under an hour
Newbie friendliness
88/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Quiet
Tech stack
go
Domain
cli

Research direction

Open main.go and inspect the root Cobra command around lines 13-16 and error handling around line 22. Confirm the command suppresses Cobra's usage and duplicate errors, and that execution errors are sent to stderr; done means the CLI reports errors without polluting stdout.

Written by the indexing model from the issue text.

Description

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:

fmt.Println(err)

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

Dominant language
Go
Stars
109
Forks
15
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from go-gorm/cli

All issues in go-gorm/cli

Similar issues

More Go issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.