Skip to content

Commit

Permalink
Extracted chagnes in cli.go to #321
Browse files Browse the repository at this point in the history
  • Loading branch information
miry committed Sep 17, 2021
1 parent c39d29f commit 40899c6
Showing 1 changed file with 125 additions and 175 deletions.
300 changes: 125 additions & 175 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ var toxicDescription = `
toxic add:
usage: toxiproxy-cli toxic add --type <toxicType> --toxicName <toxicName> \
--attribute <key=value> [--downstream|--upstream] <proxyName>
--attribute <key=value> --upstream --downstream <proxyName>
example: toxiproxy-cli toxic add -t latency -n myToxic -a latency=100 -a jitter=50 myProxy
Expand All @@ -77,28 +77,7 @@ func main() {
app.Name = "toxiproxy-cli"
app.Version = toxiproxyServer.Version
app.Usage = "Simulate network and system conditions"
app.Commands = cliCommands()
cli.HelpFlag = &cli.BoolFlag{
Name: "help",
Usage: "show help",
}
app.Flags = []cli.Flag{
&cli.StringFlag{
Name: "host",
Aliases: []string{"h"},
Value: "http://localhost:8474",
Usage: "toxiproxy host to connect to",
Destination: &hostname,
},
}

isTTY = terminal.IsTerminal(int(os.Stdout.Fd()))

app.Run(os.Args)
}

func cliCommands() []*cli.Command {
return []*cli.Command{
app.Commands = []*cli.Command{
{
Name: "list",
Usage: "list all proxies\n\tusage: 'toxiproxy-cli list'\n",
Expand Down Expand Up @@ -147,105 +126,104 @@ func cliCommands() []*cli.Command {
Aliases: []string{"t"},
Usage: "\tadd, remove or update a toxic\n\t\tusage: see 'toxiproxy-cli toxic'\n",
Description: toxicDescription,
Subcommands: cliToxiSubCommands(),
Subcommands: []*cli.Command{
{
Name: "add",
Aliases: []string{"a"},
Usage: "add a new toxic",
ArgsUsage: "<proxyName>",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "toxicName",
Aliases: []string{"n"},
Usage: "name of the toxic",
},
&cli.StringFlag{
Name: "type",
Aliases: []string{"t"},
Usage: "type of toxic",
},
&cli.StringFlag{
Name: "toxicity",
Aliases: []string{"tox"},
Usage: "toxicity of toxic",
},
&cli.StringSliceFlag{
Name: "attribute",
Aliases: []string{"a"},
Usage: "toxic attribute in key=value format",
},
&cli.BoolFlag{
Name: "upstream",
Aliases: []string{"u"},
Usage: "add toxic to upstream",
},
&cli.BoolFlag{
Name: "downstream",
Aliases: []string{"d"},
Usage: "add toxic to downstream",
},
},
Action: withToxi(addToxic),
},
{
Name: "update",
Aliases: []string{"u"},
Usage: "update an enabled toxic",
ArgsUsage: "<proxyName>",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "toxicName",
Aliases: []string{"n"},
Usage: "name of the toxic",
},
&cli.StringFlag{
Name: "toxicity",
Aliases: []string{"tox"},
Usage: "toxicity of toxic",
},
&cli.StringSliceFlag{
Name: "attribute",
Aliases: []string{"a"},
Usage: "toxic attribute in key=value format",
},
},
Action: withToxi(updateToxic),
},
{
Name: "remove",
Aliases: []string{"r", "delete", "d"},
Usage: "remove an enabled toxic",
ArgsUsage: "<proxyName>",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "toxicName",
Aliases: []string{"n"},
Usage: "name of the toxic",
},
},
Action: withToxi(removeToxic),
},
},
},
}
}

func cliToxiSubCommands() []*cli.Command {
return []*cli.Command{
cliToxiAddSubCommand(),
cliToxiUpdateSubCommand(),
cliToxiRemoveSubCommand(),
cli.HelpFlag = &cli.BoolFlag{
Name: "help",
Usage: "show help",
}
}

func cliToxiAddSubCommand() *cli.Command {
return &cli.Command{
Name: "add",
Aliases: []string{"a"},
Usage: "add a new toxic",
ArgsUsage: "<proxyName>",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "toxicName",
Aliases: []string{"n"},
Usage: "name of the toxic",
},
&cli.StringFlag{
Name: "type",
Aliases: []string{"t"},
Usage: "type of toxic",
},
&cli.StringFlag{
Name: "toxicity",
Aliases: []string{"tox"},
Usage: "toxicity of toxic",
},
&cli.StringSliceFlag{
Name: "attribute",
Aliases: []string{"a"},
Usage: "toxic attribute in key=value format",
},
&cli.BoolFlag{
Name: "upstream",
Aliases: []string{"u"},
Usage: "add toxic to upstream",
DefaultText: "false",
},
&cli.BoolFlag{
Name: "downstream",
Aliases: []string{"d"},
Usage: "add toxic to downstream",
DefaultText: "true",
},
app.Flags = []cli.Flag{
&cli.StringFlag{
Name: "host",
Aliases: []string{"h"},
Value: "http://localhost:8474",
Usage: "toxiproxy host to connect to",
Destination: &hostname,
},
Action: withToxi(addToxic),
}
}

func cliToxiUpdateSubCommand() *cli.Command {
return &cli.Command{
Name: "update",
Aliases: []string{"u"},
Usage: "update an enabled toxic",
ArgsUsage: "<proxyName>",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "toxicName",
Aliases: []string{"n"},
Usage: "name of the toxic",
},
&cli.StringFlag{
Name: "toxicity",
Aliases: []string{"tox"},
Usage: "toxicity of toxic",
},
&cli.StringSliceFlag{
Name: "attribute",
Aliases: []string{"a"},
Usage: "toxic attribute in key=value format",
},
},
Action: withToxi(updateToxic),
}
}
isTTY = terminal.IsTerminal(int(os.Stdout.Fd()))

func cliToxiRemoveSubCommand() *cli.Command {
return &cli.Command{
Name: "remove",
Aliases: []string{"r", "delete", "d"},
Usage: "remove an enabled toxic",
ArgsUsage: "<proxyName>",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "toxicName",
Aliases: []string{"n"},
Usage: "name of the toxic",
},
},
Action: withToxi(removeToxic),
}
app.Run(os.Args)
}

type toxiAction func(*cli.Context, *toxiproxy.Client) error
Expand Down Expand Up @@ -442,87 +420,59 @@ func parseToxicity(c *cli.Context, defaultToxicity float32) (float32, error) {
return toxicity, nil
}

type ToxicOptions struct {
proxyName, toxicName, toxicType string
toxicity float32
attributes toxiproxy.Attributes
upstream, downstream bool
}

func (o *ToxicOptions) stream() string {
if o.upstream {
return "upstream"
}
return "downstream"
}

func parseAddToxicParams(c *cli.Context) (*ToxicOptions, error) {
upstream := c.Bool("upstream")
downstream := c.Bool("downstream")
if upstream && downstream {
return nil, errorf("Only one should be specified: upstream or downstream.\n")
}

func addToxic(c *cli.Context, t *toxiproxy.Client) error {
proxyName := c.Args().First()
if proxyName == "" {
cli.ShowSubcommandHelp(c)
return nil, errorf("Proxy name is missing.\n")
return errorf("Proxy name is required as the first argument.\n")
}

toxicName := c.String("toxicName")
toxicType, err := getArgOrFail(c, "type")
if err != nil {
return nil, err
return err
}

upstream := c.Bool("upstream")
downstream := c.Bool("downstream")

toxicity, err := parseToxicity(c, 1.0)
if err != nil {
return nil, err
return err
}

attributes := parseAttributes(c, "attribute")

return &ToxicOptions{
proxyName: proxyName,
toxicName: toxicName,
toxicType: toxicType,
toxicity: toxicity,
attributes: attributes,
upstream: upstream,
downstream: downstream,
}, nil
}

func addToxic(c *cli.Context, t *toxiproxy.Client) error {
toxicParams, err := parseAddToxicParams(c)
p, err := t.Proxy(proxyName)
if err != nil {
return err
return errorf("Failed to retrieve proxy %s: %s\n", proxyName, err.Error())
}

proxy, err := t.Proxy(toxicParams.proxyName)
if err != nil {
return errorf("Failed to retrieve proxy %s: %s\n", toxicParams.proxyName, err.Error())
addToxic := func(stream string) error {
t, err := p.AddToxic(toxicName, toxicType, stream, toxicity, attributes)
if err != nil {
return errorf("Failed to add toxic: %s\n", err.Error())
}
toxicName = t.Name
fmt.Printf(
"Added %s %s toxic '%s' on proxy '%s'\n",
stream,
toxicType,
toxicName,
proxyName,
)
return nil
}

toxic, err := proxy.AddToxic(
toxicParams.toxicName,
toxicParams.toxicType,
toxicParams.stream(),
toxicParams.toxicity,
toxicParams.attributes,
)
if err != nil {
return errorf("Failed to add toxic: %s\n", err.Error())
if upstream {
err := addToxic("upstream")
if err != nil {
return err
}
}
// Default to downstream.
if downstream || (!downstream && !upstream) {
return addToxic("downstream")
}

fmt.Printf(
"Added %s %s toxic '%s' on proxy '%s'\n",
toxic.Stream,
toxic.Type,
toxic.Name,
toxicParams.proxyName,
)

return nil
}

Expand Down

0 comments on commit 40899c6

Please sign in to comment.