github.com/polydawn/docket@v0.5.4-0.20140630233848-90b70fb433da/main/docket.go (about)

     1  package main
     2  
     3  import (
     4  	. "fmt"
     5  	"os"
     6  	"github.com/jessevdk/go-flags"
     7  	. "polydawn.net/hroot/commands"
     8  	. "polydawn.net/hroot/util"
     9  )
    10  
    11  var parser = flags.NewNamedParser("hroot", flags.Default | flags.HelpFlag)
    12  
    13  const EXIT_BADARGS = 1
    14  const EXIT_PANIC = 2
    15  const EXIT_BAD_USER = 10
    16  
    17  // print only the error message (don't dump stacks).
    18  // unless any debug mode is on; then don't recover, because we want to dump stacks.
    19  func panicHandler() {
    20  	if err := recover(); err != nil {
    21  
    22  		//HrootError is used for user-friendly exits. Just print & exit.
    23  		if dockErr, ok := err.(HrootError) ; ok {
    24  			Print(dockErr.Error())
    25  			os.Exit(EXIT_BAD_USER)
    26  		}
    27  
    28  		//Check for existence of debug environment variable
    29  		if len(os.Getenv("DEBUG")) == 0 && len(os.Getenv("DEBUG_STACK")) == 0  {
    30  			//Debug not set, be friendlier about the problem
    31  			Println(err)
    32  			Println("\n" + "Hroot crashed! This could be a problem with docker or git, or hroot itself." + "\n" + "To see more about what went wrong, turn on stack traces by running:" + "\n\n" + "export DEBUG=1" + "\n\n" + "Feel free to contact the developers for help:" + "\n" + "https://github.com/polydawn/hroot" + "\n")
    33  			os.Exit(EXIT_PANIC)
    34  		} else {
    35  			//Adds main to the top of the stack, but keeps original information.
    36  			//Nothing we can do about it. Golaaaaannngggg....
    37  			panic(err)
    38  		}
    39  	}
    40  }
    41  
    42  func main() {
    43  	defer panicHandler()
    44  
    45  	// parser.AddCommand(
    46  	// 	"command",
    47  	// 	"description",
    48  	// 	"long description",
    49  	// 	&whateverCmd{}
    50  	// )
    51  	parser.AddCommand(
    52  		"run",
    53  		"Run a container",
    54  		"Run a container based on configuration in the current directory.",
    55  
    56  		//Default settings
    57  		&RunCmdOpts{ }, //cannot set a default source; default is determined intelligently at runtime
    58  	)
    59  	parser.AddCommand(
    60  		"build",
    61  		"Transform a container",
    62  		"Transform a container based on configuration in the current directory.",
    63  
    64  		//Default settings
    65  		&BuildCmdOpts{
    66  			Destination: "graph",
    67  		},
    68  	)
    69  	parser.AddCommand(
    70  		"version",
    71  		"Print hroot version",
    72  		"Print hroot version",
    73  		&VersionCmdOpts{},
    74  	)
    75  
    76  	//Go-flags is a little too clever with sub-commands.
    77  	//To keep the help-command parity with git & docker / etc, check for 'help' manually before args parse
    78  	if len(os.Args) < 2 || os.Args[1] == "help" {
    79  		parser.WriteHelp(os.Stdout)
    80  		os.Exit(0)
    81  	}
    82  
    83  	//Parse for command & flags, and exit with a relevant return code.
    84  	_, err := parser.Parse()
    85  	if err != nil {
    86  		os.Exit(EXIT_BADARGS)
    87  	} else {
    88  		os.Exit(0)
    89  	}
    90  }