forked from tsenart/vegeta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
63 lines (50 loc) · 1.11 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
}