Skip to content
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

jv: use single source structure for drafts and outputs #177

Open
wants to merge 1 commit into
base: boon
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading