Skip to content

Commit

Permalink
move HandleExit to the jiracli package
Browse files Browse the repository at this point in the history
  • Loading branch information
coryb committed Jul 29, 2018
1 parent 62303ed commit ef9b731
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
15 changes: 1 addition & 14 deletions cmd/jira/main.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package main

import (
"fmt"
"os"
"path/filepath"
"runtime/debug"

"github.com/coryb/figtree"
"github.com/coryb/oreo"
Expand All @@ -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()

Expand Down
20 changes: 20 additions & 0 deletions jiracli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"os"
"os/exec"
"reflect"
"runtime/debug"
"strings"

"github.com/coryb/figtree"
Expand All @@ -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
Expand Down

0 comments on commit ef9b731

Please sign in to comment.