Skip to content

Commit

Permalink
attack cmd: Re-use target format constants
Browse files Browse the repository at this point in the history
  • Loading branch information
tsenart committed Jul 7, 2018
1 parent 188262a commit dae3d2d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
12 changes: 7 additions & 5 deletions attack.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"net/http"
"os"
"os/signal"
"strings"
"time"

vegeta "github.com/tsenart/vegeta/lib"
Expand All @@ -25,7 +26,8 @@ func attackCmd() command {

fs.StringVar(&opts.name, "name", "", "Attack name")
fs.StringVar(&opts.targetsf, "targets", "stdin", "Targets file")
fs.StringVar(&opts.format, "format", "http", "Targets format [http, json]")
fs.StringVar(&opts.format, "format", vegeta.HTTPTargetFormat,
fmt.Sprintf("Targets format [%s]", strings.Join(vegeta.TargetFormats, ", ")))
fs.StringVar(&opts.outputf, "output", "stdout", "Output file")
fs.StringVar(&opts.bodyf, "body", "", "Requests body file")
fs.StringVar(&opts.certf, "cert", "", "TLS client PEM encoded certificate file")
Expand Down Expand Up @@ -115,13 +117,13 @@ func attack(opts *attackOpts) (err error) {
)

switch opts.format {
case "json":
case vegeta.JSONTargetFormat:
tr = vegeta.NewJSONTargeter(src, body, hdr)
case "http":
case vegeta.HTTPTargetFormat:
tr = vegeta.NewHTTPTargeter(src, body, hdr)
default:
valid := [...]string{vegeta.HTTPTargetFormat, vegeta.JSONTargetFormat}
return fmt.Errorf("format %q isn't one of %v", opts.format, valid)
return fmt.Errorf("format %q isn't one of [%s]",
opts.format, strings.Join(vegeta.TargetFormats, ", "))
}

if !opts.lazy {
Expand Down
3 changes: 3 additions & 0 deletions lib/targets.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ var (
ErrNoTargets = errors.New("no targets to attack")
// ErrNilTarget is returned when the passed Target pointer is nil.
ErrNilTarget = errors.New("nil target")
// TargetFormats contains the canonical list of the valid target
// format identifiers.
TargetFormats = []string{HTTPTargetFormat, JSONTargetFormat}
)

const (
Expand Down

0 comments on commit dae3d2d

Please sign in to comment.