Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #300 from tsenart/json-targets
JSON targets format
- Loading branch information
Showing
8 changed files
with
447 additions
and
145 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.