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)
if err != nil {
if err != nil && !errors.Is(err, format.ErrNoImports) {
return fmt.Errorf("could not format file %q: %w", walkPath, err)
}

19
main.go
View File

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