Beispiel #1
0
func main() {
	log.InitWithConfig(log.Config{Name: "console"})

	app := cli.NewApp()
	app.Name = "vulcanbundle"
	app.Usage = "Command line interface to compile plugins into vulcan binary"
	app.Commands = []cli.Command{
		{
			Name:   "init",
			Usage:  "Init bundle",
			Action: initBundle,
			Flags: []cli.Flag{
				cli.StringSliceFlag{
					Name:  "middleware, m",
					Value: &cli.StringSlice{},
					Usage: "Path to repo and revision, e.g. github.com/mailgun/vulcand-plugins/auth",
				},
			},
		},
	}
	err := app.Run(os.Args)
	if err != nil {
		log.Errorf("Error: %s\n", err)
	}
}
Beispiel #2
0
func TestApp_DefaultStdout(t *testing.T) {
	app := cli.NewApp()

	if app.Writer != os.Stdout {
		t.Error("Default output writer not set.")
	}
}
Beispiel #3
0
func ExampleAppHelp() {
	// set args for examples sake
	os.Args = []string{"greet", "h", "describeit"}

	app := cli.NewApp()
	app.Name = "greet"
	app.Flags = []cli.Flag{
		cli.StringFlag{Name: "name", Value: "bob", Usage: "a name to say"},
	}
	app.Commands = []cli.Command{
		{
			Name:        "describeit",
			ShortName:   "d",
			Usage:       "use it to see a description",
			Description: "This is how we describe describeit the function",
			Action: func(c *cli.Context) {
				fmt.Printf("i like to describe things")
			},
		},
	}
	app.Run(os.Args)
	// Output:
	// NAME:
	//    describeit - use it to see a description
	//
	// USAGE:
	//    command describeit [arguments...]
	//
	// DESCRIPTION:
	//    This is how we describe describeit the function
}
Beispiel #4
0
func TestGlobalFlagsInSubcommands(t *testing.T) {
	subcommandRun := false
	app := cli.NewApp()

	app.Flags = []cli.Flag{
		cli.BoolFlag{Name: "debug, d", Usage: "Enable debugging"},
	}

	app.Commands = []cli.Command{
		cli.Command{
			Name: "foo",
			Subcommands: []cli.Command{
				{
					Name: "bar",
					Action: func(c *cli.Context) {
						if c.GlobalBool("debug") {
							subcommandRun = true
						}
					},
				},
			},
		},
	}

	app.Run([]string{"command", "-d", "foo", "bar"})

	expect(t, subcommandRun, true)
}
Beispiel #5
0
func ExampleAppBashComplete() {
	// set args for examples sake
	os.Args = []string{"greet", "--generate-bash-completion"}

	app := cli.NewApp()
	app.Name = "greet"
	app.EnableBashCompletion = true
	app.Commands = []cli.Command{
		{
			Name:        "describeit",
			ShortName:   "d",
			Usage:       "use it to see a description",
			Description: "This is how we describe describeit the function",
			Action: func(c *cli.Context) {
				fmt.Printf("i like to describe things")
			},
		}, {
			Name:        "next",
			Usage:       "next example",
			Description: "more stuff to see when generating bash completion",
			Action: func(c *cli.Context) {
				fmt.Printf("the next example")
			},
		},
	}

	app.Run(os.Args)
	// Output:
	// describeit
	// d
	// next
	// help
	// h
}
Beispiel #6
0
func Example() {
	app := cli.NewApp()
	app.Name = "todo"
	app.Usage = "task list on the command line"
	app.Commands = []cli.Command{
		{
			Name:      "add",
			ShortName: "a",
			Usage:     "add a task to the list",
			Action: func(c *cli.Context) {
				println("added task: ", c.Args().First())
			},
		},
		{
			Name:      "complete",
			ShortName: "c",
			Usage:     "complete a task on the list",
			Action: func(c *cli.Context) {
				println("completed task: ", c.Args().First())
			},
		},
	}

	app.Run(os.Args)
}
Beispiel #7
0
func (s *SpecSuite) TestNewCircuitBreakerFromCli(c *C) {
	app := cli.NewApp()
	app.Name = "test"
	executed := false
	app.Action = func(ctx *cli.Context) {
		executed = true
		out, err := FromCli(ctx)
		c.Assert(out, NotNil)
		c.Assert(err, IsNil)

		cl := out.(*Spec)
		c.Assert(cl.Condition, Equals, "LatencyAtQuantileMS(50.0) < 20")
		c.Assert(cl.Fallback, Equals, `{"Type": "response", "Action": {"StatusCode": 400, "Body": "Come back later"}}`)
		c.Assert(cl.OnTripped, Equals, `{"Type": "webhook", "Action": {"URL": "http://localhost", "Method": "GET"}}`)
		c.Assert(cl.OnStandby, Equals, `{"Type": "webhook", "Action": {"URL": "http://localhost", "Method": "POST"}}`)
		c.Assert(cl.FallbackDuration, Equals, 11*time.Second)
		c.Assert(cl.RecoveryDuration, Equals, 12*time.Second)
		c.Assert(cl.CheckPeriod, Equals, 14*time.Millisecond)
	}
	app.Flags = CliFlags()
	app.Run([]string{"test",
		"--condition=LatencyAtQuantileMS(50.0) < 20",
		`--fallback={"Type": "response", "Action": {"StatusCode": 400, "Body": "Come back later"}}`,
		`--onTripped={"Type": "webhook", "Action": {"URL": "http://localhost", "Method": "GET"}}`,
		`--onStandby={"Type": "webhook", "Action": {"URL": "http://localhost", "Method": "POST"}}`,
		`--fallbackDuration=11s`,
		`--recoveryDuration=12s`,
		`--checkPeriod=14ms`,
	})
	c.Assert(executed, Equals, true)
}
Beispiel #8
0
func (cmd *Command) Run(args []string) error {
	url, args, err := findVulcanUrl(args)
	if err != nil {
		return err
	}
	cmd.vulcanUrl = url
	cmd.client = api.NewClient(cmd.vulcanUrl, cmd.registry)

	app := cli.NewApp()
	app.Name = "vctl"
	app.Usage = "Command line interface to a running vulcan instance"
	app.Flags = flags()

	app.Commands = []cli.Command{
		NewLogCommand(cmd),
		NewKeyCommand(cmd),
		NewTopCommand(cmd),
		NewHostCommand(cmd),
		NewBackendCommand(cmd),
		NewFrontendCommand(cmd),
		NewServerCommand(cmd),
		NewListenerCommand(cmd),
	}
	app.Commands = append(app.Commands, NewMiddlewareCommands(cmd)...)
	return app.Run(args)
}
Beispiel #9
0
func TestApp_ParseSliceFlags(t *testing.T) {
	var parsedOption, firstArg string
	var parsedIntSlice []int
	var parsedStringSlice []string

	app := cli.NewApp()
	command := cli.Command{
		Name: "cmd",
		Flags: []cli.Flag{
			cli.IntSliceFlag{Name: "p", Value: &cli.IntSlice{}, Usage: "set one or more ip addr"},
			cli.StringSliceFlag{Name: "ip", Value: &cli.StringSlice{}, Usage: "set one or more ports to open"},
		},
		Action: func(c *cli.Context) {
			parsedIntSlice = c.IntSlice("p")
			parsedStringSlice = c.StringSlice("ip")
			parsedOption = c.String("option")
			firstArg = c.Args().First()
		},
	}
	app.Commands = []cli.Command{command}

	app.Run([]string{"", "cmd", "my-arg", "-p", "22", "-p", "80", "-ip", "8.8.8.8", "-ip", "8.8.4.4"})

	IntsEquals := func(a, b []int) bool {
		if len(a) != len(b) {
			return false
		}
		for i, v := range a {
			if v != b[i] {
				return false
			}
		}
		return true
	}

	StrsEquals := func(a, b []string) bool {
		if len(a) != len(b) {
			return false
		}
		for i, v := range a {
			if v != b[i] {
				return false
			}
		}
		return true
	}
	var expectedIntSlice = []int{22, 80}
	var expectedStringSlice = []string{"8.8.8.8", "8.8.4.4"}

	if !IntsEquals(parsedIntSlice, expectedIntSlice) {
		t.Errorf("%v does not match %v", parsedIntSlice, expectedIntSlice)
	}

	if !StrsEquals(parsedStringSlice, expectedStringSlice) {
		t.Errorf("%v does not match %v", parsedStringSlice, expectedStringSlice)
	}
}
Beispiel #10
0
func TestApp_Command(t *testing.T) {
	app := cli.NewApp()
	fooCommand := cli.Command{Name: "foobar", ShortName: "f"}
	batCommand := cli.Command{Name: "batbaz", ShortName: "b"}
	app.Commands = []cli.Command{
		fooCommand,
		batCommand,
	}

	for _, test := range commandAppTests {
		expect(t, app.Command(test.name) != nil, test.expected)
	}
}
Beispiel #11
0
func TestApp_Run(t *testing.T) {
	s := ""

	app := cli.NewApp()
	app.Action = func(c *cli.Context) {
		s = s + c.Args().First()
	}

	err := app.Run([]string{"command", "foo"})
	expect(t, err, nil)
	err = app.Run([]string{"command", "bar"})
	expect(t, err, nil)
	expect(t, s, "foobar")
}
Beispiel #12
0
func TestApp_Float64Flag(t *testing.T) {
	var meters float64

	app := cli.NewApp()
	app.Flags = []cli.Flag{
		cli.Float64Flag{Name: "height", Value: 1.5, Usage: "Set the height, in meters"},
	}
	app.Action = func(c *cli.Context) {
		meters = c.Float64("height")
	}

	app.Run([]string{"", "--height", "1.93"})
	expect(t, meters, 1.93)
}
Beispiel #13
0
func TestAppNoHelpFlag(t *testing.T) {
	oldFlag := cli.HelpFlag
	defer func() {
		cli.HelpFlag = oldFlag
	}()

	cli.HelpFlag = cli.BoolFlag{}

	app := cli.NewApp()
	err := app.Run([]string{"test", "-h"})

	if err != flag.ErrHelp {
		t.Errorf("expected error about missing help flag, but got: %s (%T)", err, err)
	}
}
Beispiel #14
0
func ExampleApp() {
	// set args for examples sake
	os.Args = []string{"greet", "--name", "Jeremy"}

	app := cli.NewApp()
	app.Name = "greet"
	app.Flags = []cli.Flag{
		cli.StringFlag{Name: "name", Value: "bob", Usage: "a name to say"},
	}
	app.Action = func(c *cli.Context) {
		fmt.Printf("Hello %v\n", c.String("name"))
	}
	app.Run(os.Args)
	// Output:
	// Hello Jeremy
}
Beispiel #15
0
func (s *SecureSuite) TestSecureFromCli(c *C) {
	app := cli.NewApp()
	app.Name = "secure_test"
	executed := false
	app.Action = func(ctx *cli.Context) {
		executed = true
		out, err := FromCli(ctx)
		c.Assert(out, NotNil)
		c.Assert(err, IsNil)

		m := out.(*SecureMiddleware)
		c.Assert(m.Opts.BrowserXssFilter, Equals, true)
	}
	app.Flags = CliFlags()
	app.Run([]string{"secure_test", "--xss-filter"})
	c.Assert(executed, Equals, true)
}
Beispiel #16
0
func TestApp_SetStdout(t *testing.T) {
	w := &mockWriter{}

	app := cli.NewApp()
	app.Name = "test"
	app.Writer = w

	err := app.Run([]string{"help"})

	if err != nil {
		t.Fatalf("Run error: %s", err)
	}

	if len(w.written) == 0 {
		t.Error("App did not write output to desired writer.")
	}
}
Beispiel #17
0
func TestApp_CommandWithNoFlagBeforeTerminator(t *testing.T) {
	var args []string

	app := cli.NewApp()
	command := cli.Command{
		Name: "cmd",
		Action: func(c *cli.Context) {
			args = c.Args()
		},
	}
	app.Commands = []cli.Command{command}

	app.Run([]string{"", "cmd", "my-arg", "--", "notAFlagAtAll"})

	expect(t, args[0], "my-arg")
	expect(t, args[1], "--")
	expect(t, args[2], "notAFlagAtAll")
}
Beispiel #18
0
func (s *ConnLimitSuite) TestNewConnLimitFromCli(c *C) {
	app := cli.NewApp()
	app.Name = "test"
	executed := false
	app.Action = func(ctx *cli.Context) {
		executed = true
		out, err := FromCli(ctx)
		c.Assert(out, NotNil)
		c.Assert(err, IsNil)

		cl := out.(*ConnLimit)
		c.Assert(cl.Variable, Equals, "client.ip")
		c.Assert(cl.Connections, Equals, int64(10))
	}
	app.Flags = CliFlags()
	app.Run([]string{"test", "--var=client.ip", "--connections=10"})
	c.Assert(executed, Equals, true)
}
func (s *AuthSuite) TestAuthFromCli(c *C) {
	app := cli.NewApp()
	app.Name = "test"
	executed := false
	app.Action = func(ctx *cli.Context) {
		executed = true
		out, err := FromCli(ctx)
		c.Assert(out, NotNil)
		c.Assert(err, IsNil)

		a := out.(*AuthMiddleware)
		c.Assert(a.authKeys[0].password, Equals, "pass1")
		c.Assert(a.authKeys[0].username, Equals, "user1")
	}
	app.Flags = CliFlags()
	app.Run([]string{"test", "--credentials=user1:pass1"})
	c.Assert(executed, Equals, true)
}
Beispiel #20
0
func TestAppHelpPrinter(t *testing.T) {
	oldPrinter := cli.HelpPrinter
	defer func() {
		cli.HelpPrinter = oldPrinter
	}()

	var wasCalled = false
	cli.HelpPrinter = func(template string, data interface{}) {
		wasCalled = true
	}

	app := cli.NewApp()
	app.Run([]string{"-h"})

	if wasCalled == false {
		t.Errorf("Help printer expected to be called, but was not")
	}
}
Beispiel #21
0
func TestAppVersionPrinter(t *testing.T) {
	oldPrinter := cli.VersionPrinter
	defer func() {
		cli.VersionPrinter = oldPrinter
	}()

	var wasCalled = false
	cli.VersionPrinter = func(c *cli.Context) {
		wasCalled = true
	}

	app := cli.NewApp()
	ctx := cli.NewContext(app, nil, nil)
	cli.ShowVersion(ctx)

	if wasCalled == false {
		t.Errorf("Version printer expected to be called, but was not")
	}
}
Beispiel #22
0
func (s *RateLimitSuite) TestFromCli(c *C) {
	app := cli.NewApp()
	app.Name = "test"
	app.Flags = GetSpec().CliFlags
	executed := false
	app.Action = func(ctx *cli.Context) {
		executed = true
		out, err := FromCli(ctx)
		c.Assert(out, NotNil)
		c.Assert(err, IsNil)

		rl := out.(*RateLimit)
		m, err := rl.NewHandler(nil)
		c.Assert(m, NotNil)
		c.Assert(err, IsNil)
	}
	app.Run([]string{"test", "--var=client.ip", "--requests=10", "--burst=3", "--period=4"})
	c.Assert(executed, Equals, true)
}
Beispiel #23
0
func (s *TraceSuite) TestNewFromCLI(c *C) {
	app := cli.NewApp()
	app.Name = "test"
	executed := false
	app.Action = func(ctx *cli.Context) {
		executed = true
		out, err := FromCli(ctx)
		c.Assert(out, NotNil)
		c.Assert(err, IsNil)

		t := out.(*Trace)
		c.Assert(t.Addr, Equals, "syslog:///dev/log?sev=INFO&f=MAIL")
		c.Assert(t.ReqHeaders, DeepEquals, []string{"X-A", "X-B"})
		c.Assert(t.RespHeaders, DeepEquals, []string{"X-C", "X-D"})
	}
	app.Flags = CliFlags()
	app.Run([]string{"test", "--addr=syslog:///dev/log?sev=INFO&f=MAIL", "--reqHeader=X-A", "--reqHeader=X-B", "--respHeader=X-C", "--respHeader=X-D"})
	c.Assert(executed, Equals, true)
}
Beispiel #24
0
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 (s *RateLimitSuite) TestNewRateLimitFromCliOk(c *C) {
	app := cli.NewApp()
	app.Name = "test"
	executed := false
	app.Action = func(ctx *cli.Context) {
		executed = true
		out, err := FromCli(ctx)
		c.Assert(out, NotNil)
		c.Assert(err, IsNil)

		rl := out.(*RateLimit)
		c.Assert(rl.Variable, Equals, "client.ip")
		c.Assert(rl.Requests, Equals, 10)
		c.Assert(rl.Burst, Equals, int64(3))
		c.Assert(rl.PeriodSeconds, Equals, 4)
	}
	app.Flags = CliFlags()
	app.Run([]string{"test", "--var=client.ip", "--requests=10", "--burst=3", "--period=4"})
	c.Assert(executed, Equals, true)
}
Beispiel #26
0
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)
}
Beispiel #27
0
func (s *RewriteSuite) TestNewRewriteFromCLIOK(c *C) {
	app := cli.NewApp()
	app.Name = "test"
	executed := false
	app.Action = func(ctx *cli.Context) {
		executed = true
		out, err := FromCli(ctx)
		c.Assert(out, NotNil)
		c.Assert(err, IsNil)

		rw := out.(*Rewrite)
		re, _ := regexp.Compile("^/foo(.*)")
		c.Assert(rw.Regexp, Equals, re.String())
		c.Assert(rw.Replacement, Equals, "$1")
		c.Assert(rw.RewriteBody, Equals, true)
		c.Assert(rw.Redirect, Equals, true)
	}
	app.Flags = CliFlags()
	app.Run([]string{"test", "--regexp=^/foo(.*)", "--replacement=$1", "--rewriteBody", "--redirect"})
	c.Assert(executed, Equals, true)
}
Beispiel #28
0
func TestApp_CommandWithArgBeforeFlags(t *testing.T) {
	var parsedOption, firstArg string

	app := cli.NewApp()
	command := cli.Command{
		Name: "cmd",
		Flags: []cli.Flag{
			cli.StringFlag{Name: "option", Value: "", Usage: "some option"},
		},
		Action: func(c *cli.Context) {
			parsedOption = c.String("option")
			firstArg = c.Args().First()
		},
	}
	app.Commands = []cli.Command{command}

	app.Run([]string{"", "cmd", "my-arg", "--option", "my-option"})

	expect(t, parsedOption, "my-option")
	expect(t, firstArg, "my-arg")
}
Beispiel #29
0
func TestAppCommandNotFound(t *testing.T) {
	beforeRun, subcommandRun := false, false
	app := cli.NewApp()

	app.CommandNotFound = func(c *cli.Context, command string) {
		beforeRun = true
	}

	app.Commands = []cli.Command{
		cli.Command{
			Name: "bar",
			Action: func(c *cli.Context) {
				subcommandRun = true
			},
		},
	}

	app.Run([]string{"command", "foo"})

	expect(t, beforeRun, true)
	expect(t, subcommandRun, false)
}
Beispiel #30
0
func (cmd *Command) Run(args []string) error {
	url, args, err := findVulcanUrl(args)
	if err != nil {
		return err
	}
	cmd.vulcanUrl = url
	cmd.client = api.NewClient(cmd.vulcanUrl, cmd.registry)

	app := cli.NewApp()
	app.Name = "vulcanctl"
	app.Usage = "Command line interface to a running vulcan instance"
	app.Flags = flags()

	app.Commands = []cli.Command{
		NewStatusCommand(cmd),
		NewHostCommand(cmd),
		NewUpstreamCommand(cmd),
		NewLocationCommand(cmd),
		NewEndpointCommand(cmd),
	}
	app.Commands = append(app.Commands, NewMiddlewareCommands(cmd)...)
	return app.Run(args)
}