From dae3d2d0098123a3e1c71f3feb84bbf9f9146060 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tom=C3=A1s=20Senart?= <tsenart@gmail.com>
Date: Sat, 7 Jul 2018 15:29:53 +0200
Subject: [PATCH] attack cmd: Re-use target format constants

---
 attack.go      | 12 +++++++-----
 lib/targets.go |  3 +++
 2 files changed, 10 insertions(+), 5 deletions(-)

diff --git a/attack.go b/attack.go
index fd479502..c0379b93 100644
--- a/attack.go
+++ b/attack.go
@@ -11,6 +11,7 @@ import (
 	"net/http"
 	"os"
 	"os/signal"
+	"strings"
 	"time"
 
 	vegeta "github.com/tsenart/vegeta/lib"
@@ -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")
@@ -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 {
diff --git a/lib/targets.go b/lib/targets.go
index 9bc8dfbe..b3d157fc 100644
--- a/lib/targets.go
+++ b/lib/targets.go
@@ -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 (