From 6564ac61633ab3bdb303abca8b85d123e881ddb8 Mon Sep 17 00:00:00 2001 From: rsteube Date: Sun, 16 Oct 2022 20:38:47 +0200 Subject: [PATCH] schema: use annotations --- command.go | 38 +++++++++++++------------------------- schema.go | 6 +----- 2 files changed, 14 insertions(+), 30 deletions(-) diff --git a/command.go b/command.go index a335a6c..764bc73 100644 --- a/command.go +++ b/command.go @@ -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 { diff --git a/schema.go b/schema.go index a5430fa..a27966a 100644 --- a/schema.go +++ b/schema.go @@ -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