Exemple #1
0
func run(cmd *commander.Command) (returnCode int) {
	defer func() {
		if r := recover(); r != nil {
			fatal, ok := r.(*ctx.FatalError)
			if !ok {
				panic(r)
			}
			fmt.Fprintln(os.Stderr, "ERROR:", fatal.Message)
			returnCode = fatal.ReturnCode
		}
	}()
	returnCode = 0

	flags, _, err := cmd.ParseFlags([]string{"-config", "conf/slapt.conf"})
	if err != nil {
		ctx.Fatal(err)
	}

	err = initCtx(flags)
	if err != nil {
		ctx.Fatal(err)
	}
	defer shutdownContext()
	context.UpdateFlags(flags)

	err = http.ListenAndServe("localhost:8080", web.Router(context))
	if err != nil {
		ctx.Fatal(fmt.Errorf("unable to serve: %s", err))
	}

	return
}
Exemple #2
0
// Run runs single command starting from root cmd with args, optionally initializing context
func Run(cmd *commander.Command, cmdArgs []string, initContext bool) (returnCode int) {
	defer func() {
		if r := recover(); r != nil {
			fatal, ok := r.(*FatalError)
			if !ok {
				panic(r)
			}
			fmt.Println("ERROR:", fatal.Message)
			returnCode = fatal.ReturnCode
		}
	}()

	returnCode = 0

	flags, args, err := cmd.ParseFlags(cmdArgs)
	if err != nil {
		Fatal(err)
	}

	if initContext {
		err = InitContext(flags)
		if err != nil {
			Fatal(err)
		}
		defer ShutdownContext()
	}

	context.UpdateFlags(flags)

	err = cmd.Dispatch(args)
	if err != nil {
		Fatal(err)
	}

	return
}