本文整理汇总了Golang中github.com/codegangsta/cli.App.Before方法的典型用法代码示例。如果您正苦于以下问题:Golang App.Before方法的具体用法?Golang App.Before怎么用?Golang App.Before使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/codegangsta/cli.App
的用法示例。
在下文中一共展示了App.Before方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: SetupCPUProfile
func SetupCPUProfile(app *cli.App) {
app.Flags = append(app.Flags, cli.StringFlag{
Name: "cpuprofile",
Usage: "write cpu profile to file",
EnvVar: "CPU_PROFILE",
})
appBefore := app.Before
appAfter := app.After
app.Before = func(c *cli.Context) error {
if cpuProfile := c.String("cpuprofile"); cpuProfile != "" {
f, err := os.Create(cpuProfile)
if err != nil {
return err
}
pprof.StartCPUProfile(f)
}
if appBefore != nil {
return appBefore(c)
}
return nil
}
app.After = func(c *cli.Context) error {
pprof.StopCPUProfile()
if appAfter != nil {
return appAfter(c)
}
return nil
}
}
示例2: Mousetrap
func Mousetrap(app *cli.App) {
oldBefore := app.Before
app.Before = func(c *cli.Context) error {
if mousetrap.StartedByExplorer() {
cmd := exec.Command(os.Args[0], os.Args[1:]...)
cmd.Env = append(os.Environ(), "MOUSETRAP=1")
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Run()
cmd = exec.Command("cmd.exe", "/K")
cmd.Env = os.Environ()
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
fmt.Println("Failed to execute sub-process. Error:", err)
os.Exit(1)
}
os.Exit(0)
}
if oldBefore == nil {
return nil
}
return oldBefore(c)
}
}
示例3: Run
func Run(app *cli.App) {
app.Before = func(c *cli.Context) error {
if c.GlobalString("key") == "" {
log.Fatal("No aio key provided. Use --key KEY_HERE or export AIO_KEY=KEY_HERE")
}
if c.GlobalBool("debug") {
log.SetLevel(log.DebugLevel)
log.Debug("Debug Mode ON")
log.Debug("AIO_KEY: ", c.GlobalString("key"))
}
return nil
}
app.Run(os.Args)
}
示例4: LogRuntimePlatform
func LogRuntimePlatform(app *cli.App) {
appBefore := app.Before
app.Before = func(c *cli.Context) error {
logrus.WithFields(logrus.Fields{
"os": runtime.GOOS,
"arch": runtime.GOARCH,
"version": common.VERSION,
"revision": common.REVISION,
}).Debugln("Runtime platform")
if appBefore != nil {
return appBefore(c)
}
return nil
}
}
示例5: FixHOME
func FixHOME(app *cli.App) {
appBefore := app.Before
app.Before = func(c *cli.Context) error {
// Fix home
if key := homedir.Key(); os.Getenv(key) == "" {
value := homedir.Get()
if value == "" {
return fmt.Errorf("the %q is not set", key)
}
os.Setenv(key, value)
}
if appBefore != nil {
return appBefore(c)
}
return nil
}
}
示例6: setAppBefore
func setAppBefore(app *cli.App) {
app.Before = func(context *cli.Context) error {
if context.GlobalBool("debug") {
logrus.SetLevel(logrus.DebugLevel)
if context.GlobalDuration("metrics-interval") > 0 {
if err := debugMetrics(context.GlobalDuration("metrics-interval"), context.GlobalString("graphite-address")); err != nil {
return err
}
}
}
if p := context.GlobalString("pprof-address"); len(p) > 0 {
pprof.Enable(p)
}
if err := checkLimits(); err != nil {
return err
}
return nil
}
}
示例7: SetupLogLevelOptions
func SetupLogLevelOptions(app *cli.App) {
newFlags := []cli.Flag{
cli.BoolFlag{
Name: "debug",
Usage: "debug mode",
EnvVar: "DEBUG",
},
cli.StringFlag{
Name: "log-level, l",
Value: "info",
Usage: "Log level (options: debug, info, warn, error, fatal, panic)",
},
}
app.Flags = append(app.Flags, newFlags...)
appBefore := app.Before
// logs
app.Before = func(c *cli.Context) error {
log.SetOutput(os.Stderr)
level, err := log.ParseLevel(c.String("log-level"))
if err != nil {
log.Fatalf(err.Error())
}
log.SetLevel(level)
// If a log level wasn't specified and we are running in debug mode,
// enforce log-level=debug.
if !c.IsSet("log-level") && !c.IsSet("l") && c.Bool("debug") {
log.SetLevel(log.DebugLevel)
}
if appBefore != nil {
return appBefore(c)
} else {
return nil
}
}
}
示例8: setupFlags
func setupFlags(app *cli.App) {
app.Flags = []cli.Flag{
cli.StringFlag{"app-id", "", "AppID"},
cli.StringFlag{"app-key", "", "AppKey"},
cli.StringFlag{"client-id", "", "ClientID"},
cli.StringFlag{"client-secret", "", "ClientSecret"},
cli.StringFlag{"site", "", "us,jp,cn,sg"},
cli.StringFlag{"endpoint-url", "", "Site URL"},
cli.BoolFlag{"verbose", "Verbosely"},
cli.StringFlag{"profile", "default", "Profile name for ~/.kii/config"},
}
app.Before = func(c *cli.Context) error {
profile := c.GlobalString("profile")
inifile := loadIniFile()
if profile != "default" && len((*inifile)[profile]) == 0 {
print(fmt.Sprintf("profile %s is not found in ~/.kii/config\n", profile))
os.Exit(ExitMissingParams)
}
get := func(name string) string {
v, _ := inifile.Get(profile, name)
return v
}
globalConfig = &GlobalConfig{
pickup(c.GlobalString("app-id"), os.ExpandEnv("${KII_APP_ID}"), get("app_id")),
pickup(c.GlobalString("app-key"), os.ExpandEnv("${KII_APP_KEY}"), get("app_key")),
pickup(c.GlobalString("client-id"), os.ExpandEnv("${KII_CLIENT_ID}"), get("client_id")),
pickup(c.GlobalString("client-secret"), os.ExpandEnv("${KII_CLIENT_SECRET}"), get("client_secret")),
pickup(c.GlobalString("site"), os.ExpandEnv("${KII_SITE}"), get("site")),
pickup(c.GlobalString("endpoint-url"), os.ExpandEnv("${KII_ENDPOINT_URL}"), get("endpoint_url")),
}
if c.Bool("verbose") {
logger = &_Logger{}
}
return nil
}
}