Skip to content

Commit

Permalink
jsonschema: pre-generate
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Oct 29, 2024
1 parent c02aa88 commit b83482d
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 52 deletions.
1 change: 1 addition & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ version: 2
before:
hooks:
- go mod download
- go generate ./...
builds:
- id: default
env:
Expand Down
21 changes: 0 additions & 21 deletions action.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand Down
23 changes: 23 additions & 0 deletions cmd/go.mod
Original file line number Diff line number Diff line change
@@ -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
)
33 changes: 33 additions & 0 deletions cmd/go.sum
Original file line number Diff line number Diff line change
@@ -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=
27 changes: 27 additions & 0 deletions cmd/schema/main.go
Original file line number Diff line number Diff line change
@@ -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))
}
}
5 changes: 0 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
17 changes: 0 additions & 17 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down
16 changes: 7 additions & 9 deletions schema.go
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions schema.json
Original file line number Diff line number Diff line change
@@ -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"]}}}

0 comments on commit b83482d

Please sign in to comment.