enbas/cmd/enbas-codegen/schema.go
Dan Anglin 84091f398d
feat: add Enbas CLI schema and code generator
Summary:

- Created a custom CLI schema for Enbas which will act as the Source
  of Truth for code and document generation.
- Created a code generator which uses the schema to generate the
  executor definitions and code in the internal usage package.

Changes:

- Created the Enbas CLI schema as the Source of Truth for Enbas.
- Created the code generator that generates the executor
  definitions and code in the usage package.
- Regenerated the executor definitions using the code generator.
- Moved the custom flag value types to the new internal flag
  package.
- Created a new flag value type for the bool pointer to replace the
  flag.BoolFunc() used for the sensitive and no-color flags.
- Moved the version and build variables to the new internal version
  package to simplify the version executor.
- Created a new usage package and moved the usage functions there.
- Changed the type of the account-name flag from string to the
  internal StringSliceValue type.
2024-08-13 14:53:26 +01:00

72 lines
1.7 KiB
Go

package main
import (
"encoding/json"
"fmt"
"os"
)
type enbasCLISchema struct {
Flags enbasCLISchemaFlagMap `json:"flags"`
Commands map[string]enbasCLISchemaCommand `json:"commands"`
}
func newEnbasCLISchemaFromFile(path string) (enbasCLISchema, error) {
file, err := os.Open(path)
if err != nil {
return enbasCLISchema{}, fmt.Errorf("unable to open the schema file: %w", err)
}
defer file.Close()
var schema enbasCLISchema
if err := json.NewDecoder(file).Decode(&schema); err != nil {
return enbasCLISchema{}, fmt.Errorf("unable to decode the JSON data: %w", err)
}
return schema, nil
}
type enbasCLISchemaFlag struct {
Type string `json:"type"`
Description string `json:"description"`
}
type enbasCLISchemaFlagMap map[string]enbasCLISchemaFlag
func (e enbasCLISchemaFlagMap) getType(name string) string {
flag, ok := e[name]
if !ok {
return "UNKNOWN TYPE"
}
return flag.Type
}
func (e enbasCLISchemaFlagMap) getDescription(name string) string {
flag, ok := e[name]
if !ok {
return "UNKNOWN DESCRIPTION"
}
return flag.Description
}
type enbasCLISchemaCommand struct {
AdditionalFields []enbasCLISchemaAdditionalFields `json:"additionalFields"`
Flags []enbasCLISchemaFlagReference `json:"flags"`
Summary string `json:"summary"`
UseConfig bool `json:"useConfig"`
UsePrinter bool `json:"usePrinter"`
}
type enbasCLISchemaFlagReference struct {
Flag string `json:"flag"`
FieldName string `json:"fieldName"`
Default string `json:"default"`
}
type enbasCLISchemaAdditionalFields struct {
Name string `json:"name"`
Type string `json:"type"`
}