fix: print errors to stderr and exit code 1
ci/woodpecker/push/goreleaser Pipeline was successful Details

Signed-off-by: jolheiser <john.olheiser@gmail.com>
main v0.0.6
jolheiser 2023-07-16 21:12:52 -05:00
parent f9f2f02f90
commit 5c91d2e95a
Signed by: jolheiser
GPG Key ID: B853ADA5DA7BBF7A
2 changed files with 12 additions and 9 deletions

2
imp.go
View File

@ -49,7 +49,7 @@ func runImp(root, ignore string, write, gofumpt, gofumptExtra bool) error {
} }
formatted, err := impFormat(data, mod, gofumpt, gofumptExtra) formatted, err := impFormat(data, mod, gofumpt, gofumptExtra)
if err != nil { if err != nil && !errors.Is(err, format.ErrNoImports) {
return fmt.Errorf("could not format file %q: %w", walkPath, err) return fmt.Errorf("could not format file %q: %w", walkPath, err)
} }

19
main.go
View File

@ -12,6 +12,13 @@ import (
var Version = "develop" var Version = "develop"
func main() { func main() {
if err := mainErr(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func mainErr() error {
fs := flag.NewFlagSet("imp", flag.ExitOnError) fs := flag.NewFlagSet("imp", flag.ExitOnError)
writeFlag := fs.Bool("write", false, "Write the re-ordered imports instead of just printing them") writeFlag := fs.Bool("write", false, "Write the re-ordered imports instead of just printing them")
fs.BoolVar(writeFlag, "w", *writeFlag, "--write") fs.BoolVar(writeFlag, "w", *writeFlag, "--write")
@ -28,17 +35,15 @@ func main() {
if *stdinFlag { if *stdinFlag {
mod, err := modInfo() mod, err := modInfo()
if err != nil { if err != nil {
fmt.Println(err) return err
return
} }
src, err := io.ReadAll(os.Stdin) src, err := io.ReadAll(os.Stdin)
if err != nil { if err != nil {
fmt.Println(err) return err
return
} }
formatted, err := impFormat(src, mod, *gofumptFlag, *gofumptExtraFlag) formatted, err := impFormat(src, mod, *gofumptFlag, *gofumptExtraFlag)
if err != nil { if err != nil {
fmt.Println(err) return err
} }
fmt.Print(string(formatted)) fmt.Print(string(formatted))
} }
@ -48,9 +53,7 @@ func main() {
root = fs.Arg(0) root = fs.Arg(0)
} }
if err := runImp(root, *ignoreFlag, *writeFlag, *gofumptFlag, *gofumptExtraFlag); err != nil { return runImp(root, *ignoreFlag, *writeFlag, *gofumptFlag, *gofumptExtraFlag)
fmt.Println(err)
}
} }
type module struct { type module struct {