diff --git a/cmd/jira/main.go b/cmd/jira/main.go index a42765d3..8e85e19a 100644 --- a/cmd/jira/main.go +++ b/cmd/jira/main.go @@ -1,10 +1,8 @@ package main import ( - "fmt" "os" "path/filepath" - "runtime/debug" "github.com/coryb/figtree" "github.com/coryb/oreo" @@ -24,19 +22,8 @@ func (ol *oreoLogger) Printf(format string, args ...interface{}) { ol.logger.Debugf(format, args...) } -func handleExit() { - if e := recover(); e != nil { - if exit, ok := e.(jiracli.Exit); ok { - os.Exit(exit.Code) - } else { - fmt.Fprintf(os.Stderr, "%s\n%s", e, debug.Stack()) - os.Exit(1) - } - } -} - func main() { - defer handleExit() + defer jiracli.HandleExit() jiracli.InitLogging() diff --git a/jiracli/cli.go b/jiracli/cli.go index cff443ce..56838796 100644 --- a/jiracli/cli.go +++ b/jiracli/cli.go @@ -11,6 +11,7 @@ import ( "os" "os/exec" "reflect" + "runtime/debug" "strings" "github.com/coryb/figtree" @@ -28,6 +29,25 @@ type Exit struct { Code int } +// HandleExit will unwind any panics and check to see if they are jiracli.Exit +// and exit accordingly. +// +// Example: +// func main() { +// defer jiracli.HandleExit() +// ... +// } +func HandleExit() { + if e := recover(); e != nil { + if exit, ok := e.(Exit); ok { + os.Exit(exit.Code) + } else { + fmt.Fprintf(os.Stderr, "%s\n%s", e, debug.Stack()) + os.Exit(1) + } + } +} + type GlobalOptions struct { // AuthenticationMethod is the method we use to authenticate with the jira serivce. Possible values are "api-token" or "session". // The default is "api-token" when the service endpoint ends with "atlassian.net", otherwise it "session". Session authentication