Skip to content

Commit

Permalink
print errors to stderr
Browse files Browse the repository at this point in the history
  • Loading branch information
xrstf committed Jan 5, 2024
1 parent db8ac68 commit 5248741
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
7 changes: 4 additions & 3 deletions cmd/rudi/cmd/console/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"errors"
"fmt"
"os"
"strings"

"go.xrstf.de/rudi"
Expand Down Expand Up @@ -98,10 +99,10 @@ func Run(handler *util.SignalHandler, opts *options.Options, library rudi.Progra
if err != nil {
parseErr := &rudi.ParseError{}
if errors.As(err, parseErr) {
fmt.Println(parseErr.Snippet())
fmt.Println(parseErr)
fmt.Fprintln(os.Stderr, parseErr.Snippet())
fmt.Fprintln(os.Stderr, parseErr)
} else {
fmt.Printf("Error: %v\n", err)
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
}
}
if stop {
Expand Down
16 changes: 8 additions & 8 deletions cmd/rudi/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,15 @@ func main() {
}

if err := opts.Validate(); err != nil {
fmt.Printf("Invalid command line: %v\n", err)
fmt.Fprintf(os.Stderr, "Invalid command line: %v\n", err)
os.Exit(2)
}

args := pflag.Args()

if opts.ShowHelp || (len(args) > 0 && args[0] == "help") {
if err := help.Run(&opts, args); err != nil {
fmt.Printf("Error: %v\n", err)
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}

Expand All @@ -120,7 +120,7 @@ func main() {
for _, filename := range opts.LibraryFiles {
_, script, err := util.ParseFile(filename)
if err != nil {
fmt.Printf("Error: library %q: %v\n", filename, err)
fmt.Fprintf(os.Stderr, "Error: library %q: %v\n", filename, err)
os.Exit(1)
}

Expand All @@ -136,14 +136,14 @@ func main() {
baseProgram, err = rudi.Parse("(library)", strings.Join(baseScripts, "\n"))
if err != nil {
// This should never happen, each script was already syntax-checked.
fmt.Printf("Error: library: %v\n", err)
fmt.Fprintf(os.Stderr, "Error: library: %v\n", err)
os.Exit(1)
}
}

if opts.Interactive {
if err := console.Run(handler, &opts, baseProgram, args, BuildTag); err != nil {
fmt.Printf("Error: %v\n", err)
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}

Expand All @@ -153,10 +153,10 @@ func main() {
if err := script.Run(handler, &opts, baseProgram, args); err != nil {
parseErr := &rudi.ParseError{}
if errors.As(err, parseErr) {
fmt.Println(parseErr.Snippet())
fmt.Println(parseErr)
fmt.Fprintln(os.Stderr, parseErr.Snippet())
fmt.Fprintln(os.Stderr, parseErr)
} else {
fmt.Printf("Error: %v\n", err)
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
}

os.Exit(1)
Expand Down

0 comments on commit 5248741

Please sign in to comment.