From b83482d6d24f4e0b56dcd47bd0ae8751cec9e63f Mon Sep 17 00:00:00 2001 From: rsteube Date: Tue, 29 Oct 2024 14:52:56 +0100 Subject: [PATCH] jsonschema: pre-generate --- .goreleaser.yml | 1 + action.go | 21 --------------------- cmd/go.mod | 23 +++++++++++++++++++++++ cmd/go.sum | 33 +++++++++++++++++++++++++++++++++ cmd/schema/main.go | 27 +++++++++++++++++++++++++++ go.mod | 5 ----- go.sum | 17 ----------------- schema.go | 16 +++++++--------- schema.json | 1 + 9 files changed, 92 insertions(+), 52 deletions(-) create mode 100644 cmd/go.mod create mode 100644 cmd/go.sum create mode 100644 cmd/schema/main.go create mode 100644 schema.json diff --git a/.goreleaser.yml b/.goreleaser.yml index 20dec13..96a8cd9 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -2,6 +2,7 @@ version: 2 before: hooks: - go mod download + - go generate ./... builds: - id: default env: diff --git a/action.go b/action.go index 4d9765f..fcaaecf 100644 --- a/action.go +++ b/action.go @@ -5,11 +5,9 @@ import ( "os" "path/filepath" "regexp" - "sort" "strings" "github.com/carapace-sh/carapace" - "github.com/invopop/jsonschema" "github.com/spf13/cobra" "github.com/spf13/pflag" "gopkg.in/yaml.v3" @@ -29,25 +27,6 @@ func NewAction(s []string) action { // TODO rename return a } -func (value) JSONSchema() *jsonschema.Schema { - sortedNames := make([]string, 0, len(macros)) - for name := range macros { - sortedNames = append(sortedNames, name) - } - sort.Strings(sortedNames) - - examples := make([]interface{}, 0, len(macros)) - for _, name := range sortedNames { - examples = append(examples, fmt.Sprintf("$%v(%v)", name, macros[name].macro.Signature())) - } - return &jsonschema.Schema{ - Type: "string", - Title: "Action", - Description: "A static value or a macro", - Examples: examples, - } -} - func executable() string { s, err := os.Executable() if err != nil { diff --git a/cmd/go.mod b/cmd/go.mod new file mode 100644 index 0000000..86028bd --- /dev/null +++ b/cmd/go.mod @@ -0,0 +1,23 @@ +module github.com/carapace-sh/carapace-spec/cmd + +go 1.23.1 + +replace github.com/carapace-sh/carapace-spec => ../ + +require ( + github.com/carapace-sh/carapace v1.3.3 + github.com/carapace-sh/carapace-spec v0.0.0-00010101000000-000000000000 + github.com/invopop/jsonschema v0.12.0 + github.com/spf13/cobra v1.8.1 + gopkg.in/yaml.v3 v3.0.1 +) + +require ( + github.com/bahlo/generic-list-go v0.2.0 // indirect + github.com/buger/jsonparser v1.1.1 // indirect + github.com/carapace-sh/carapace-shlex v1.0.1 // indirect + github.com/inconshreveable/mousetrap v1.1.0 // indirect + github.com/mailru/easyjson v0.7.7 // indirect + github.com/spf13/pflag v1.0.5 // indirect + github.com/wk8/go-ordered-map/v2 v2.1.8 // indirect +) diff --git a/cmd/go.sum b/cmd/go.sum new file mode 100644 index 0000000..d6ea9aa --- /dev/null +++ b/cmd/go.sum @@ -0,0 +1,33 @@ +github.com/bahlo/generic-list-go v0.2.0 h1:5sz/EEAK+ls5wF+NeqDpk5+iNdMDXrh3z3nPnH1Wvgk= +github.com/bahlo/generic-list-go v0.2.0/go.mod h1:2KvAjgMlE5NNynlg/5iLrrCCZ2+5xWbdbCW3pNTGyYg= +github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs= +github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0= +github.com/carapace-sh/carapace v1.3.3 h1:rxA1KHt6bZs0frgPz0JkknKQxtIKCZaY7TeAxuOWLQw= +github.com/carapace-sh/carapace v1.3.3/go.mod h1:djegtVDi/3duSAqZNU+/nCq7XtDRMRZUb5bW0O/HnEs= +github.com/carapace-sh/carapace-shlex v1.0.1 h1:ww0JCgWpOVuqWG7k3724pJ18Lq8gh5pHQs9j3ojUs1c= +github.com/carapace-sh/carapace-shlex v1.0.1/go.mod h1:lJ4ZsdxytE0wHJ8Ta9S7Qq0XpjgjU0mdfCqiI2FHx7M= +github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= +github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/invopop/jsonschema v0.12.0 h1:6ovsNSuvn9wEQVOyc72aycBMVQFKz7cPdMJn10CvzRI= +github.com/invopop/jsonschema v0.12.0/go.mod h1:ffZ5Km5SWWRAIN6wbDXItl95euhFz2uON45H2qjYt+0= +github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= +github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0= +github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM= +github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/wk8/go-ordered-map/v2 v2.1.8 h1:5h/BUHu93oj4gIdvHHHGsScSTMijfx5PeYkE/fJgbpc= +github.com/wk8/go-ordered-map/v2 v2.1.8/go.mod h1:5nJHM5DyteebpVlHnWMV0rPz6Zp7+xBAnxjb1X5vnTw= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/cmd/schema/main.go b/cmd/schema/main.go new file mode 100644 index 0000000..9264ecf --- /dev/null +++ b/cmd/schema/main.go @@ -0,0 +1,27 @@ +package main + +import ( + "fmt" + "os" + "path/filepath" + + spec "github.com/carapace-sh/carapace-spec" + "github.com/invopop/jsonschema" +) + +func main() { + schema := jsonschema.Reflect(&spec.Command{}) + m, err := schema.MarshalJSON() + if err != nil { + panic(err.Error()) + } + + switch len(os.Args) { + case 2: + if path := os.Args[1]; filepath.Base(path) == "schema.json" { + os.WriteFile(path, m, os.ModePerm) + } + default: + fmt.Println(string(m)) + } +} diff --git a/go.mod b/go.mod index a0766e8..dec42eb 100644 --- a/go.mod +++ b/go.mod @@ -4,17 +4,12 @@ go 1.23.0 require ( github.com/carapace-sh/carapace v1.3.3 - github.com/invopop/jsonschema v0.12.0 github.com/spf13/cobra v1.8.1 github.com/spf13/pflag v1.0.5 gopkg.in/yaml.v3 v3.0.1 ) require ( - github.com/bahlo/generic-list-go v0.2.0 // indirect - github.com/buger/jsonparser v1.1.1 // indirect github.com/carapace-sh/carapace-shlex v1.0.1 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect - github.com/mailru/easyjson v0.7.7 // indirect - github.com/wk8/go-ordered-map/v2 v2.1.8 // indirect ) diff --git a/go.sum b/go.sum index d6ea9aa..32d6b4e 100644 --- a/go.sum +++ b/go.sum @@ -1,32 +1,15 @@ -github.com/bahlo/generic-list-go v0.2.0 h1:5sz/EEAK+ls5wF+NeqDpk5+iNdMDXrh3z3nPnH1Wvgk= -github.com/bahlo/generic-list-go v0.2.0/go.mod h1:2KvAjgMlE5NNynlg/5iLrrCCZ2+5xWbdbCW3pNTGyYg= -github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs= -github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0= github.com/carapace-sh/carapace v1.3.3 h1:rxA1KHt6bZs0frgPz0JkknKQxtIKCZaY7TeAxuOWLQw= github.com/carapace-sh/carapace v1.3.3/go.mod h1:djegtVDi/3duSAqZNU+/nCq7XtDRMRZUb5bW0O/HnEs= github.com/carapace-sh/carapace-shlex v1.0.1 h1:ww0JCgWpOVuqWG7k3724pJ18Lq8gh5pHQs9j3ojUs1c= github.com/carapace-sh/carapace-shlex v1.0.1/go.mod h1:lJ4ZsdxytE0wHJ8Ta9S7Qq0XpjgjU0mdfCqiI2FHx7M= github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= -github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= -github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= -github.com/invopop/jsonschema v0.12.0 h1:6ovsNSuvn9wEQVOyc72aycBMVQFKz7cPdMJn10CvzRI= -github.com/invopop/jsonschema v0.12.0/go.mod h1:ffZ5Km5SWWRAIN6wbDXItl95euhFz2uON45H2qjYt+0= -github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= -github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0= -github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= -github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= -github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM= github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= -github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= -github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= -github.com/wk8/go-ordered-map/v2 v2.1.8 h1:5h/BUHu93oj4gIdvHHHGsScSTMijfx5PeYkE/fJgbpc= -github.com/wk8/go-ordered-map/v2 v2.1.8/go.mod h1:5nJHM5DyteebpVlHnWMV0rPz6Zp7+xBAnxjb1X5vnTw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/schema.go b/schema.go index a27966a..0964937 100644 --- a/schema.go +++ b/schema.go @@ -1,15 +1,13 @@ package spec import ( - "github.com/invopop/jsonschema" + _ "embed" ) -// Schema returns a json schema with currently registered macros -func Schema() (string, error) { - schema := jsonschema.Reflect(&Command{}) - out, err := schema.MarshalJSON() - if err != nil { - return "", err - } - return string(out), nil +//go:embed schema.json +var schema string + +//go:generate go run -C cmd/schema . ../../schema.json +func Schema() string { + return schema } diff --git a/schema.json b/schema.json new file mode 100644 index 0000000..6f70dcc --- /dev/null +++ b/schema.json @@ -0,0 +1 @@ +{"$schema":"https://json-schema.org/draft/2020-12/schema","$id":"https://github.com/carapace-sh/carapace-spec/command","$ref":"#/$defs/Command","$defs":{"Command":{"properties":{"name":{"type":"string","description":"Name of the command"},"aliases":{"items":{"type":"string"},"type":"array","description":"Aliases of the command"},"description":{"type":"string","description":"Description of the command"},"group":{"type":"string","description":"Group of the command"},"hidden":{"type":"boolean","description":"Hidden state of the command"},"parsing":{"type":"string","enum":["interspersed","non-interspersed","disabled"],"description":"Flag parsing mode of the command"},"flags":{"additionalProperties":{"type":"string"},"type":"object","description":"Flags of the command with their description"},"persistentflags":{"additionalProperties":{"type":"string"},"type":"object","description":"Persistent flags of the command with their description"},"exclusiveflags":{"items":{"items":{"type":"string"},"type":"array"},"type":"array","description":"Flags that are mutually exclusive"},"run":{"type":"string","description":"Command or script to execute in runnable mode"},"completion":{"properties":{"flag":{"additionalProperties":{"items":{"type":"string"},"type":"array"},"type":"object","description":"Flag completion"},"positional":{"items":{"items":{"type":"string"},"type":"array"},"type":"array","description":"Positional completion"},"positionalany":{"items":{"type":"string"},"type":"array","description":"Positional completion for every other position"},"dash":{"items":{"items":{"type":"string"},"type":"array"},"type":"array","description":"Dash completion"},"dashany":{"items":{"type":"string"},"type":"array","description":"Dash completion of every other position"}},"additionalProperties":false,"type":"object","description":"Completion definition"},"commands":{"items":{"$ref":"#/$defs/Command"},"type":"array","description":"Subcommands of the command"},"documentation":{"properties":{"command":{"type":"string","description":"Documentation of the command"},"flag":{"additionalProperties":{"type":"string"},"type":"object","description":"Documentation of flags"},"positional":{"items":{"type":"string"},"type":"array","description":"Documentation of positional arguments"},"positionalany":{"type":"string","description":"Documentation of other positional arguments"},"dash":{"items":{"type":"string"},"type":"array","description":"Documentation of dash arguments"},"dashany":{"type":"string","description":"Documentation of other dash arguments"}},"additionalProperties":false,"type":"object","description":"Documentation"},"examples":{"additionalProperties":{"type":"string"},"type":"object","description":"Examples"}},"additionalProperties":false,"type":"object","required":["name"]}}} \ No newline at end of file