-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JSON targets format #300
JSON targets format #300
Changes from all commits
ed8efd7
50ef463
909e0fe
a7df0cf
ec323fa
188262a
dae3d2d
eb227c2
2e261e6
4911bc1
6de2156
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ import ( | |
"net/http" | ||
"os" | ||
"os/signal" | ||
"strings" | ||
"time" | ||
|
||
vegeta "github.com/tsenart/vegeta/lib" | ||
|
@@ -25,6 +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", vegeta.HTTPTargetFormat, | ||
fmt.Sprintf("Targets format [%s]", strings.Join(vegeta.TargetFormats, ", "))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I find this breaking odd, it doesn't give consistent guidance how to break long lines and makes it hard to edit. Might be preferrable to use the following rule: func myFunc(
param1,
param2,
param3,
) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's discuss this today in our pairing session. |
||
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") | ||
|
@@ -59,6 +62,7 @@ var ( | |
type attackOpts struct { | ||
name string | ||
targetsf string | ||
format string | ||
outputf string | ||
bodyf string | ||
certf string | ||
|
@@ -111,10 +115,23 @@ func attack(opts *attackOpts) (err error) { | |
src = files[opts.targetsf] | ||
hdr = opts.headers.Header | ||
) | ||
if opts.lazy { | ||
tr = vegeta.NewLazyTargeter(src, body, hdr) | ||
} else if tr, err = vegeta.NewEagerTargeter(src, body, hdr); err != nil { | ||
return err | ||
|
||
switch opts.format { | ||
case vegeta.JSONTargetFormat: | ||
tr = vegeta.NewJSONTargeter(src, body, hdr) | ||
case vegeta.HTTPTargetFormat: | ||
tr = vegeta.NewHTTPTargeter(src, body, hdr) | ||
default: | ||
return fmt.Errorf("format %q isn't one of [%s]", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above. |
||
opts.format, strings.Join(vegeta.TargetFormats, ", ")) | ||
} | ||
|
||
if !opts.lazy { | ||
targets, err := vegeta.ReadAllTargets(tr) | ||
if err != nil { | ||
return err | ||
} | ||
tr = vegeta.NewStaticTargeter(targets...) | ||
} | ||
|
||
out, err := file(opts.outputf, true) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"flag" | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"strings" | ||
|
||
"github.com/alecthomas/jsonschema" | ||
|
||
vegeta "github.com/tsenart/vegeta/lib" | ||
) | ||
|
||
func main() { | ||
types := map[string]interface{}{ | ||
"Target": &vegeta.Target{}, | ||
} | ||
|
||
valid := strings.Join(keys(types), ", ") | ||
|
||
fs := flag.NewFlagSet("jsonschema", flag.ContinueOnError) | ||
typ := fs.String("type", "", fmt.Sprintf("Vegeta type to generate a JSON schema for [%s]", valid)) | ||
out := fs.String("output", "stdout", "Output file") | ||
|
||
if err := fs.Parse(os.Args[1:]); err != nil { | ||
die("%s", err) | ||
} | ||
|
||
t, ok := types[*typ] | ||
if !ok { | ||
die("invalid type %q not in [%s]", *typ, valid) | ||
} | ||
|
||
schema, err := json.MarshalIndent(jsonschema.Reflect(t), "", " ") | ||
if err != nil { | ||
die("%s", err) | ||
} | ||
|
||
switch *out { | ||
case "stdout": | ||
_, err = os.Stdout.Write(schema) | ||
default: | ||
err = ioutil.WriteFile(*out, schema, 0644) | ||
} | ||
|
||
if err != nil { | ||
die("%s", err) | ||
} | ||
} | ||
|
||
func die(s string, args ...interface{}) { | ||
fmt.Fprintf(os.Stderr, s, args...) | ||
os.Exit(1) | ||
} | ||
|
||
func keys(types map[string]interface{}) (ks []string) { | ||
for k := range types { | ||
ks = append(ks, k) | ||
} | ||
return ks | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any particular reason why you have removed the explanation of
report
anddump
command?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because it was redundant. It's already included in the output of
vegeta -h
.