grim/convey

b70f2a116800
Parents c6c0e3ef2acd
Children 9850991e6f0a
Implement a gomain that actually works correctly...
  • +22 -21
    main.go
  • --- a/main.go Thu Dec 28 00:20:47 2017 -0600
    +++ b/main.go Sat Dec 30 02:49:57 2017 -0600
    @@ -21,6 +21,7 @@
    "fmt"
    "os"
    "path/filepath"
    + "runtime/debug"
    "strings"
    "github.com/alecthomas/kingpin"
    @@ -67,12 +68,7 @@
    planNames = app.Arg("plan", "The plan or list of plans to run in specified order").Strings()
    )
    -func main() {
    - exitCode := 0
    - defer func() {
    - os.Exit(exitCode)
    - }()
    -
    +func gomain() int {
    app.Parse(os.Args[1:])
    // now load the config
    @@ -99,30 +95,26 @@
    // now make sure we found a config file
    if *configFile == "" {
    fmt.Printf("config file not found, looking for %s\n", strings.Join(loader.Filenames(), ","))
    - exitCode = 1
    - return
    + return 1
    }
    // figure out the path to the config file
    cfgPath, err := filepath.Abs(filepath.Dir(*configFile))
    if err != nil {
    fmt.Printf("%#v\n", err)
    - exitCode = 1
    - return
    + return 1
    }
    cfg, err := config.LoadFile(*configFile, loader)
    if err != nil {
    fmt.Printf("%s\n", err)
    - exitCode = 1
    - return
    + return 1
    }
    // setup logging
    if err := logging.Setup(*color, *verbose); err != nil {
    fmt.Printf("failed to setup logging: %s\n", err)
    - exitCode = 1
    - return
    + return 1
    }
    defer gomol.ShutdownLoggers()
    @@ -131,8 +123,7 @@
    defEnv, err := environment.Initialize(cfgPath)
    if err != nil {
    fmt.Printf("%s\n", err)
    - exitCode = 1
    - return
    + return 1
    }
    // if the user specified the shortcut, add * to the list of acceptable keys
    @@ -147,7 +138,7 @@
    enableSSHAgent, err := ssh.ShouldEnable(*sshIdentities)
    if err != nil {
    fmt.Printf("%s\n", err)
    - os.Exit(1)
    + return 1
    }
    cleanupList := cleanup.NewList()
    @@ -167,9 +158,7 @@
    if err := st.Valid(); err != nil {
    fmt.Printf("%s\n", err)
    - exitCode = 1
    -
    - return
    + return 1
    }
    var runner runners.Runner
    @@ -203,5 +192,17 @@
    }
    }
    - exitCode = runner.Run(cfg, realPlans, *env, st)
    + return runner.Run(cfg, realPlans, *env, st)
    }
    +
    +func main() {
    + exitCode := 0
    + defer func() {
    + if r := recover(); r != nil {
    + fmt.Printf("panic: %s\n%s", r, debug.Stack())
    + }
    + os.Exit(exitCode)
    + }()
    +
    + exitCode = gomain()
    +}