Ejemplo n.º 1
0
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
	}
}
Ejemplo n.º 2
0
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)
	}
}
Ejemplo n.º 3
0
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)
}
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
	}

}
Ejemplo n.º 5
0
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
	}
}
Ejemplo n.º 6
0
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
	}
}
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
		}
	}
}
Ejemplo n.º 8
0
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
	}
}