func (w *crashHandler) Wrap(cmd cli.Command) cli.Command { oldAction := cmd.Action cmd.Action = func(c *cli.Context) { oldAction(c) } return cmd }
func (w *YAMLConfigLoader) Wrap(cmd cli.Command) cli.Command { cmd.Flags = append(cmd.Flags, []cli.Flag{ cli.StringFlag{"config", w.DefaultPath, "path to YAML config file", ""}, }...) oldAction := cmd.Action cmd.Action = func(c *cli.Context) { configFile := c.String("config") if configFile != "" { err := w.LoadFromFile(configFile) if err != nil { fmt.Println(err) os.Exit(1) } } oldAction(c) } return cmd }
func TestCommandDoNotIgnoreFlags(t *testing.T) { app := cli.NewApp() set := flag.NewFlagSet("test", 0) test := []string{"blah", "blah", "-break"} set.Parse(test) c := cli.NewContext(app, set, set) command := cli.Command{ Name: "test-cmd", ShortName: "tc", Usage: "this is for testing", Description: "testing", Action: func(_ *cli.Context) {}, } err := command.Run(c) expect(t, err.Error(), "flag provided but not defined: -break") }
func TestCommandIgnoreFlags(t *testing.T) { app := cli.NewApp() set := flag.NewFlagSet("test", 0) test := []string{"blah", "blah"} set.Parse(test) c := cli.NewContext(app, set, set) command := cli.Command{ Name: "test-cmd", ShortName: "tc", Usage: "this is for testing", Description: "testing", Action: func(_ *cli.Context) {}, SkipFlagParsing: true, } err := command.Run(c) expect(t, err, nil) }
func (w *Logged) Wrap(cmd cli.Command) cli.Command { cmd.Flags = append(cmd.Flags, []cli.Flag{ cli.StringFlag{"log", w.DefaultTarget, "path to log file, 'stdout', 'stderr' or 'false'", ""}, cli.StringFlag{"log-level", w.DefaultLevel, "logging level", ""}, cli.StringFlag{"log-format", w.DefaultFormat, "log record format: 'term', 'logfmt', 'json'", ""}, }...) oldAction := cmd.Action cmd.Action = func(c *cli.Context) { handler, err := w.HandlerFor(c.String("log"), c.String("log-level"), c.String("log-format")) if err != nil { fmt.Println(err) os.Exit(1) } for _, l := range w.Loggers { l.SetHandler(handler) } oldAction(c) } return cmd }