func describeApp(app *cli.App, version string) { app.Name = "init-exporter" app.Usage = "exports services described by Procfile to systemd" app.Version = version app.Flags = []cli.Flag{ cli.StringFlag{ Name: "n, appname", Usage: "Application name (This name only affects the names of generated files)", }, cli.BoolFlag{ Name: "c, uninstall", Usage: "Remove scripts and helpers for a particular application", }, cli.StringFlag{ Name: "config", Value: defaultConfigPath, Usage: "path to configuration file", }, cli.StringFlag{ Name: "p, procfile", Usage: "path to procfile", }, cli.StringFlag{ Name: "f, format", Usage: "Format of init files (upstart | systemd)", }, } }
func loadCommands(app *cli.App) { // Add top level flags and commands app.Action = mainCmd // Filters modify what type of pr to display filters := []cli.Flag{ cli.BoolFlag{"no-merge", "display only prs that cannot be merged"}, cli.BoolFlag{"lgtm", "display the number of LGTM"}, cli.BoolFlag{"closed", "display closed prs"}, cli.BoolFlag{"new", "display prs opened in the last 24 hours"}, } // Options modify how to display prs options := []cli.Flag{ cli.BoolFlag{"no-trunc", "don't truncate pr name"}, cli.StringFlag{"user", "", "display only prs from <user>"}, cli.StringFlag{"comment", "", "add a comment to the pr"}, } app.Flags = append(filters, options...) // Add subcommands app.Commands = []cli.Command{ { Name: "repo", Usage: "List information about the current repository", Action: repositoryInfoCmd, }, { Name: "auth", Usage: "Add a github token for authentication", Action: authCmd, Flags: []cli.Flag{ cli.StringFlag{"add", "", "add new token for authentication"}, }, }, { Name: "alru", Usage: "Show the Age of the Least Recently Updated pull request for this repo. Lower is better.", Action: alruCmd, }, { Name: "merge", Usage: "Merge a pull request", Action: mergeCmd, Flags: []cli.Flag{ cli.StringFlag{"m", "", "commit message for merge"}, cli.BoolFlag{"force", "merge a pull request that has not been approved"}, }, }, { Name: "checkout", Usage: "Checkout a pull request into your local repo", Action: checkoutCmd, }, { Name: "approve", Usage: "Approve a pull request by adding LGTM to the comments", Action: approveCmd, }, } }
func setBasicInfos(app *cli.App, appName string, version string) { app.Name = appName app.Version = version app.Usage = "Focus on your tasks and let your code flow." t := time.Now() app.Copyright = "TechMantra - " + strconv.Itoa(t.Year()) }
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 } }
func loadCommands(app *cli.App) { app.Action = mainCmd app.Flags = []cli.Flag{ cli.StringFlag{"assigned", "", "display issues assigned to <user>. Use '*' for all assigned, or 'none' for all unassigned."}, cli.BoolFlag{"no-trunc", "do not truncate the issue name"}, } app.Commands = []cli.Command{ { Name: "alru", Usage: "Show the Age of the Least Recently Updated issue for this repo. Lower is better.", Action: alruCmd, }, { Name: "repo", Usage: "List information about the current repository", Action: repositoryInfoCmd, }, { Name: "auth", Usage: "Add a github token for authentication", Action: authCmd, Flags: []cli.Flag{ cli.StringFlag{"add", "", "add new token for authentication"}, }, }, } }
func callCoreCommand(args []string, theApp *cli.App) { err := theApp.Run(args) if err != nil { os.Exit(1) } gateways := gatewaySliceFromMap(deps.gateways) warningsCollector := net.NewWarningsCollector(deps.termUI, gateways...) warningsCollector.PrintWarnings() }
func newCmdPresenter(app *cli.App, maxNameLen int, cmdName string) (presenter cmdPresenter) { cmd := app.Command(cmdName) presenter.Name = presentCmdName(*cmd) padding := strings.Repeat(" ", maxNameLen-len(presenter.Name)) presenter.Name = presenter.Name + padding presenter.Description = cmd.Description return }
func loadCommands(app *cli.App) { app.Action = mainCmd app.Flags = []cli.Flag{ cli.StringFlag{"assigned", "", "display issues assigned to <user>. Use '*' for all assigned, or 'none' for all unassigned."}, cli.StringFlag{"remote", "origin", "git remote to treat as origin"}, cli.BoolFlag{"no-trunc", "do not truncate the issue name"}, cli.IntFlag{"votes", -1, "display the number of votes '+1' filtered by the <number> specified."}, cli.BoolFlag{"vote", "add '+1' to an specific issue."}, } app.Commands = []cli.Command{ { Name: "alru", Usage: "Show the Age of the Least Recently Updated issue for this repo. Lower is better.", Action: alruCmd, }, { Name: "repo", Usage: "List information about the current repository", Action: repositoryInfoCmd, }, { Name: "take", Usage: "Assign an issue to your github account", Action: takeCmd, Flags: []cli.Flag{ cli.BoolFlag{"overwrite", "overwrites a taken issue"}, }, }, { Name: "search", Usage: "Find issues by state and keyword.", Action: searchCmd, Flags: []cli.Flag{ cli.StringFlag{"author", "", "Finds issues created by a certain user"}, cli.StringFlag{"assignee", "", "Finds issues that are assigned to a certain user"}, cli.StringFlag{"mentions", "", "Finds issues that mention a certain user"}, cli.StringFlag{"commenter", "", "Finds issues that a certain user commented on"}, cli.StringFlag{"involves", "", "Finds issues that were either created by a certain user, assigned to that user, mention that user, or were commented on by that user"}, cli.StringFlag{"labels", "", "Filters issues based on their labels"}, cli.StringFlag{"state", "", "Filter issues based on whether they’re open or closed"}, }, }, { Name: "auth", Usage: "Add a github token for authentication", Action: authCmd, Flags: []cli.Flag{ cli.StringFlag{"add", "", "add new token for authentication"}, }, }, } }
// so we can catch panics func run(app *cli.App) { defer func() { if r := recover(); r != nil { trace := make([]byte, 2048) count := runtime.Stack(trace, true) fmt.Println("Panic: ", r) fmt.Printf("Stack of %d bytes: %s", count, trace) } }() app.Run(os.Args) }
func GetByCmdName(app *cli.App, cmdName string) (cmd *cli.Command, err error) { cmd = app.Command(cmdName) if cmd == nil { for _, c := range app.Commands { if c.ShortName == cmdName { return &c, nil } } err = errors.New("Command not found") } return }
func loadCommands(app *cli.App) { // default to listing a service app.Action = getAction app.Flags = []cli.Flag{ cli.BoolFlag{"json", "output to json"}, cli.StringFlag{"host", os.Getenv("SKYDNS"), "url to SkyDNS's HTTP endpoints (defaults to env. var. SKYDNS)"}, cli.StringFlag{"dns", func() string { if x := os.Getenv("SKYDNS_DNS"); x != "" { if strings.HasPrefix(x, "http") { return x } return "http://" + x // default to http for now } return "127.0.0.1:53" }(), "DNS port of SkyDNS's DNS endpoint (defaults to env. var. SKYDNS_DNS)"}, cli.StringFlag{"domain", func() string { if x := os.Getenv("SKYDNS_DOMAIN"); x != "" { return x } return "skydns.local" }(), "DNS domain of SkyDNS (defaults to env. var. SKYDNS_DOMAIN))"}, cli.StringFlag{"secret", "", "secret to authenticate with"}, } app.Commands = []cli.Command{ { Name: "list", Usage: "list a service from skydns", Action: getAction, Flags: []cli.Flag{cli.BoolFlag{"d", "use DNS instead of HTTP"}}, }, { Name: "add", Usage: "add a new service to skydns", Action: addAction, }, { Name: "delete", Usage: "delete a service from skydns", Action: deleteAction, }, { Name: "update", Usage: "update a service's ttl in skydns", Action: updateAction, }, } }
func loadCommands(app *cli.App) { // default to listing a service app.Action = getAction app.Flags = []cli.Flag{ cli.BoolFlag{"json", "output to json"}, cli.StringFlag{"host", os.Getenv("SKYDNS"), "url to SkyDNS's HTTP endpoints (defaults to env. var. SKYDNS)"}, cli.StringFlag{"dnsport", func() string { x := os.Getenv("SKYDNS_DNSPORT") if x == "" { x = "53" } return x }(), "DNS port of SkyDNS's DNS endpoint (defaults to env. var. SKYDNS_DNSPORT or 53)"}, cli.StringFlag{"dnsdomain", func() string { x := os.Getenv("SKYDNS_DNSDOMAIN") if x == "" { x = "skydns.local" } return x }(), "DNS domain of SkyDNS (defaults to env. var. SKYDNS_DNSDOMAIN or skydns.local)"}, cli.StringFlag{"secret", "", "secret to authenticate with"}, } app.Commands = []cli.Command{ { Name: "list", Usage: "list a service from skydns", Action: getAction, Flags: []cli.Flag{cli.BoolFlag{"d", "use DNS instead of HTTP"}}, }, { Name: "add", Usage: "add a new service to skydns", Action: addAction, }, { Name: "delete", Usage: "delete a service from skydns", Action: deleteAction, }, { Name: "update", Usage: "update a service's ttl in skydns", Action: updateAction, }, } }
func TestParseGenericFromEnvCascade(t *testing.T) { os.Clearenv() os.Setenv("APP_FOO", "99,2000") a := cli.App{ Flags: []cli.Flag{ cli.GenericFlag{Name: "foos", Value: &Parser{}, EnvVar: "COMPAT_FOO,APP_FOO"}, }, Action: func(ctx *cli.Context) { if !reflect.DeepEqual(ctx.Generic("foos"), &Parser{"99", "2000"}) { t.Errorf("value not set from env") } }, } a.Run([]string{"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) }
func cmdConsole(app *cli.App, c *cli.Context) { for { fmt.Printf("> ") bufReader := bufio.NewReader(os.Stdin) line, more, err := bufReader.ReadLine() if more { Exit("input is too long") } else if err != nil { Exit(err.Error()) } args := []string{"tmsp"} args = append(args, strings.Split(string(line), " ")...) app.Run(args) } }
func AddAdditinalFlags(a *cli.App) { // nop a.Flags = append(a.Flags, cli.StringFlag{memProfileFlagName, "", "If set, the given file will contain the memory profile of the previous run"}, cli.StringFlag{cpuProfileFlagName, "", "If set, the given file will contain the CPU profile of the previous run"}, ) }
func setDefaultAction(app *cli.App) { app.Action = func(c *cli.Context) { // TODO: If in a focused directory show the list of tasks // If not show the app help or some basic direction to init it cli.ShowAppHelp(c) } }
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) } }
func TestParseGeneric(t *testing.T) { a := cli.App{ Flags: []cli.Flag{ cli.GenericFlag{Name: "serve, s", Value: &Parser{}}, }, Action: func(ctx *cli.Context) { if !reflect.DeepEqual(ctx.Generic("serve"), &Parser{"10", "20"}) { t.Errorf("main name not set") } if !reflect.DeepEqual(ctx.Generic("s"), &Parser{"10", "20"}) { t.Errorf("short name not set") } }, } a.Run([]string{"run", "-s", "10,20"}) }
func TestParseMultiBoolT(t *testing.T) { a := cli.App{ Flags: []cli.Flag{ cli.BoolTFlag{Name: "serve, s"}, }, Action: func(ctx *cli.Context) { if ctx.BoolT("serve") != true { t.Errorf("main name not set") } if ctx.BoolT("s") != true { t.Errorf("short name not set") } }, } a.Run([]string{"run", "--serve"}) }
func TestParseMultiFloat64(t *testing.T) { a := cli.App{ Flags: []cli.Flag{ cli.Float64Flag{Name: "serve, s"}, }, Action: func(ctx *cli.Context) { if ctx.Float64("serve") != 10.2 { t.Errorf("main name not set") } if ctx.Float64("s") != 10.2 { t.Errorf("short name not set") } }, } a.Run([]string{"run", "-s", "10.2"}) }
func TestParseGenericFromEnv(t *testing.T) { os.Setenv("APP_SERVE", "20,30") a := cli.App{ Flags: []cli.Flag{ cli.GenericFlag{Name: "serve, s", Value: &Parser{}, EnvVar: "APP_SERVE"}, }, Action: func(ctx *cli.Context) { if !reflect.DeepEqual(ctx.Generic("serve"), &Parser{"20", "30"}) { t.Errorf("main name not set from env") } if !reflect.DeepEqual(ctx.Generic("s"), &Parser{"20", "30"}) { t.Errorf("short name not set from env") } }, } a.Run([]string{"run"}) }
func TestParseMultiBoolTFromEnv(t *testing.T) { os.Setenv("APP_DEBUG", "0") a := cli.App{ Flags: []cli.Flag{ cli.BoolTFlag{Name: "debug, d", EnvVar: "APP_DEBUG"}, }, Action: func(ctx *cli.Context) { if ctx.BoolT("debug") != false { t.Errorf("main name not set from env") } if ctx.BoolT("d") != false { t.Errorf("short name not set from env") } }, } a.Run([]string{"run"}) }
func TestParseMultiFloat64FromEnv(t *testing.T) { os.Setenv("APP_TIMEOUT_SECONDS", "15.5") a := cli.App{ Flags: []cli.Flag{ cli.Float64Flag{Name: "timeout, t", EnvVar: "APP_TIMEOUT_SECONDS"}, }, Action: func(ctx *cli.Context) { if ctx.Float64("timeout") != 15.5 { t.Errorf("main name not set") } if ctx.Float64("t") != 15.5 { t.Errorf("short name not set") } }, } a.Run([]string{"run"}) }
func TestParseMultiInt(t *testing.T) { a := cli.App{ Flags: []cli.Flag{ cli.IntFlag{Name: "serve, s"}, }, Action: func(ctx *cli.Context) int { if ctx.Int("serve") != 10 { t.Errorf("main name not set") } if ctx.Int("s") != 10 { t.Errorf("short name not set") } return 0 }, } a.Run([]string{"run", "-s", "10"}) }
func setupPingCommand(app *cli.App) { app.Commands = append(app.Commands, cli.Command{ Name: "ping", Aliases: []string{"p"}, Usage: "ping the cluster to see if it's available", Action: ping, }, ) }
func TestParseMultiIntFromEnvCascade(t *testing.T) { os.Clearenv() os.Setenv("APP_TIMEOUT_SECONDS", "10") a := cli.App{ Flags: []cli.Flag{ cli.IntFlag{Name: "timeout, t", EnvVar: "COMPAT_TIMEOUT_SECONDS,APP_TIMEOUT_SECONDS"}, }, Action: func(ctx *cli.Context) { if ctx.Int("timeout") != 10 { t.Errorf("main name not set") } if ctx.Int("t") != 10 { t.Errorf("short name not set") } }, } a.Run([]string{"run"}) }
func TestParseMultiBoolFromEnvCascade(t *testing.T) { os.Clearenv() os.Setenv("APP_DEBUG", "1") a := cli.App{ Flags: []cli.Flag{ cli.BoolFlag{Name: "debug, d", EnvVar: "COMPAT_DEBUG,APP_DEBUG"}, }, Action: func(ctx *cli.Context) { if ctx.Bool("debug") != true { t.Errorf("main name not set from env") } if ctx.Bool("d") != true { t.Errorf("short name not set from env") } }, } a.Run([]string{"run"}) }
func cmdBatch(app *cli.App, c *cli.Context) { bufReader := bufio.NewReader(os.Stdin) for { line, more, err := bufReader.ReadLine() if more { Exit("input line is too long") } else if err == io.EOF { break } else if len(line) == 0 { continue } else if err != nil { Exit(err.Error()) } args := []string{"tmsp"} args = append(args, strings.Split(string(line), " ")...) app.Run(args) } }
func setupGlobalCommands(app *cli.App) { app.Commands = []cli.Command{ { Name: "help", Aliases: []string{"man"}, Usage: "prints this help message", Action: cli.ShowAppHelp, }, } }