diff options
Diffstat (limited to 'exit.go')
| -rw-r--r-- | exit.go | 17 |
1 files changed, 16 insertions, 1 deletions
@@ -6,6 +6,7 @@ import ( "time" "go.wit.com/lib/cobol" + "golang.org/x/term" ) // since we know when the command starts (duh, this parses os.Args) @@ -22,6 +23,18 @@ func BadExit(msg string, err error) { PB.badExit(msg, err) } +func isInteractive() { + // For os.Stdin, this is typically 0. + stdinFd := int(os.Stdin.Fd()) + + // IsTerminal() returns true if the given file descriptor is connected to a terminal. + if term.IsTerminal(stdinFd) { + fmt.Println("Running in an INTERACTIVE terminal.") + } else { + fmt.Println("Running in a NON-INTERACTIVE environment (pipe, script, etc.).") + } +} + func (pb *Argv) goodExit(msg string) { go ExitWatchdog() if me.appExit != nil { @@ -34,6 +47,7 @@ func (pb *Argv) goodExit(msg string) { } else { appname = pb.AppInfo.APPNAME } + isInteractive() fmt.Printf("%s: %s (%s)\n", appname, msg, cobol.FormatDuration(dur)) os.Exit(0) } @@ -43,6 +57,7 @@ func (pb *Argv) badExit(msg string, err error) { if me.appExit != nil { me.appExit() } + isInteractive() // print out errors. this handles wrapped errors which is a useful if err != nil { if u, ok := err.(interface{ Unwrap() []error }); ok { @@ -65,7 +80,7 @@ func (pb *Argv) badExit(msg string, err error) { appname = pb.AppInfo.APPNAME } fmt.Printf("%s error: %s (%s)\n", appname, msg, cobol.FormatDuration(dur)) - os.Exit(1) + os.Exit(int(ExitPB)) } // this code doesn't need to be this complicated. I put it here as reference code for myself so I could remember where it is. |
