Skip to content

Commit e4a3a84

Browse files
committed
print cli errors to stderr
This is a small change that prints any errors that surface when executing the cli (i.e., `cmd.Execute()`) to stderr rather than stdout. When redirecting streams, its useful to keep errors separated from program output so that in the case of an error the message won't be redirected and is still surfaced. This issue came up for me when using this terraform-docs in a script, where I encountered an error: $ terraform-docs markdown path/not/to/mod > outfile zsh: exit 1 terraform-docs markdown path/not/to/mod > outfile When I ran the above command in my script an error was generated because the path was incorrect, however the error message wasn't printed to my console and the outfile was filled with the error message. The change here modifies the cli's output to instead show: $ terraform-docs markdown path/not/to/mod > outfile Error: Failed to read module directory: Module directory path/not/to/mod does not exist or cannot be read. zsh: exit 1 terraform-docs markdown path/not/to/mod > outfile and the outfile is empty. Signed-off-by: Anthony O'Brien <anthony@bearonis.com>
1 parent 9c738b9 commit e4a3a84

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

cmd/root.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ package cmd
1212

1313
import (
1414
"fmt"
15+
"os"
1516

1617
"github.com/spf13/cobra"
1718

@@ -32,7 +33,7 @@ import (
3233
// This is called by main.main(). It only needs to happen once to the rootCmd.
3334
func Execute() error {
3435
if err := NewCommand().Execute(); err != nil {
35-
fmt.Printf("Error: %s\n", err.Error())
36+
fmt.Fprintf(os.Stderr, "Error: %s\n", err.Error())
3637
return err
3738
}
3839
return nil

0 commit comments

Comments
 (0)