Skip to content

Commit

Permalink
jv: use single source structure for drafts and outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
scop committed Jun 28, 2024
1 parent 89d3b9e commit df36dc8
Showing 1 changed file with 33 additions and 15 deletions.
48 changes: 33 additions & 15 deletions cmd/jv/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,41 @@ import (
"os"
"runtime/debug"
"slices"
"sort"
"strings"

"github.com/santhosh-tekuri/jsonschema/v6"
flag "github.com/spf13/pflag"
)

var (
validDrafts = map[int]*jsonschema.Draft{
4: jsonschema.Draft4,
6: jsonschema.Draft6,
7: jsonschema.Draft7,
2019: jsonschema.Draft2019,
2020: jsonschema.Draft2020,
}
validOutputs = []string{"simple", "alt", "flag", "basic", "detailed"}
)

func main() {
drafts := func() string {
ds := make([]int, 0, len(validDrafts))
for d := range validDrafts {
ds = append(ds, d)
}
sort.Ints(ds)
var b strings.Builder
for i, d := range ds {
if i != 0 {
b.WriteString(", ")
}
fmt.Fprintf(&b, "%d", d)
}
return b.String()
}()

flag.Usage = func() {
eprintln("Usage: jv [OPTIONS] SCHEMA [INSTANCE...]")
eprintln("")
Expand All @@ -23,8 +51,8 @@ func main() {
help := flag.BoolP("help", "h", false, "Print help information")
version := flag.BoolP("version", "v", false, "Print build information")
quiet := flag.BoolP("quiet", "q", false, "Do not print errors")
draftVersion := flag.IntP("draft", "d", 2020, "Draft `version` used when '$schema' is missing. Valid values 4, 6, 7, 2019, 2020")
output := flag.StringP("output", "o", "simple", "Output `format`. Valid values simple, alt, flag, basic, detailed")
draftVersion := flag.IntP("draft", "d", 2020, "Draft `version` used when '$schema' is missing. Valid values "+drafts)
output := flag.StringP("output", "o", "simple", "Output `format`. Valid values "+strings.Join(validOutputs, ", "))
assertFormat := flag.BoolP("assert-format", "f", false, "Enable format assertions with draft >= 2019")
assertContent := flag.BoolP("assert-content", "c", false, "Enable content assertions with draft >= 7")
insecure := flag.BoolP("insecure", "k", false, "Use insecure TLS connection")
Expand Down Expand Up @@ -54,26 +82,16 @@ func main() {

// draft --
var draft *jsonschema.Draft
switch *draftVersion {
case 4:
draft = jsonschema.Draft4
case 6:
draft = jsonschema.Draft6
case 7:
draft = jsonschema.Draft7
case 2019:
draft = jsonschema.Draft2019
case 2020:
draft = jsonschema.Draft2020
default:
var ok bool
if draft, ok = validDrafts[*draftVersion]; !ok {
eprintln("invalid draft: %v", *draftVersion)
eprintln("")
flag.Usage()
os.Exit(2)
}

// output --
if !slices.Contains([]string{"simple", "alt", "flag", "basic", "detailed"}, *output) {
if !slices.Contains(validOutputs, *output) {
eprintln("invalid output: %v", *output)
eprintln("")
flag.Usage()
Expand Down

0 comments on commit df36dc8

Please sign in to comment.