go-gorm / go-gorm/cli

fix: set SilenceUsage/SilenceErrors and print errors to stderr

Open
#57 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
108
Forks
15
PR merge metrics
No merged PRs in 30d

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`:
```go
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`:
```go
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

```go
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

- [Cobra docs — SilenceUsage](https://github.com/spf13/cobra#silence-usage-and-silence-errors)
- Unix convention: stdout is for program output, stderr is for diagnostics/errors

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.