Skip to content

Commit

Permalink
schema: use annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Oct 16, 2022
1 parent 5146ad3 commit 6564ac6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 30 deletions.
38 changes: 13 additions & 25 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,19 @@ func (action) JSONSchema() *jsonschema.Schema {
}

type Command struct {
// Name of the command
Name string `json:"name"`
// Aliases of the command
Aliases []string `json:"aliases,omitempty"`
// Description of the command
Description string `json:"description,omitempty"`
// Flags of the command with their description
Flags map[string]string `json:"flags,omitempty"`
// Persistent flags of the command with their description
PersistentFlags map[string]string `json:"persistentflags,omitempty"`
// Completion definition
Completion struct {
// Flag completion
Flag map[string][]action `json:"flag,omitempty"`
// Positional completion
Positional [][]action `json:"positional,omitempty"`
// Positional completion for every other position
PositionalAny []action `json:"positionalany,omitempty"`
// Dash completion
Dash [][]action `json:"dash,omitempty"`
// Dash completion for every other position
DashAny []action `json:"dashany,omitempty"`
} `json:"completion,omitempty"`
// Subcommands of the command
Commands []Command `json:"commands,omitempty"`
Name string `json:"name" jsonschema_description:"Name of the command"`
Aliases []string `json:"aliases,omitempty" jsonschema_description:"Aliases of the command"`
Description string `json:"description,omitempty" jsonschema_description:"Description of the command"`
Flags map[string]string `json:"flags,omitempty" jsonschema_description:"Flags of the command with their description"`
PersistentFlags map[string]string `json:"persistentflags,omitempty" jsonschema_description:"Persistent flags of the command with their description"`
Completion struct {
Flag map[string][]action `json:"flag,omitempty" jsonschema_description:"Flag completion"`
Positional [][]action `json:"positional,omitempty" jsonschema_description:"Positional completion"`
PositionalAny []action `json:"positionalany,omitempty" jsonschema_description:"Positional completion for every other position"`
Dash [][]action `json:"dash,omitempty" jsonschema_description:"Dash completion"`
DashAny []action `json:"dashany,omitempty" jsonschema_description:"Dash completion of every other position"`
} `json:"completion,omitempty" jsonschema_description:"Subcommands of the command"`
Commands []Command `json:"commands,omitempty" jsonschema_description:"Completion definition"`
}

func (c *Command) ToCobra() *cobra.Command {
Expand Down
6 changes: 1 addition & 5 deletions schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ import (

// Schema returns a json schema with currently registered macros
func Schema() (string, error) {
r := new(jsonschema.Reflector)
if err := r.AddGoComments("github.com/rsteube/carapace-spec", "./"); err != nil {
return "", err
}
schema := r.Reflect(&Command{})
schema := jsonschema.Reflect(&Command{})
out, err := schema.MarshalJSON()
if err != nil {
return "", err
Expand Down

0 comments on commit 6564ac6

Please sign in to comment.