| 1 | |
| 2 | |
| 3 | |
| 4 | package main |
| 5 | |
| 6 | import ( |
| 7 | "encoding/json" |
| 8 | "errors" |
| 9 | "fmt" |
| 10 | "os" |
| 11 | |
| 12 | "github.com/invopop/jsonschema" |
| 13 | orderedmap "github.com/pb33f/ordered-map/v2" |
| 14 | "github.com/sirupsen/logrus" |
| 15 | "github.com/spf13/cobra" |
| 16 | |
| 17 | "github.com/lima-vm/lima/v2/pkg/jsonschemautil" |
| 18 | "github.com/lima-vm/lima/v2/pkg/limatype" |
| 19 | ) |
| 20 | |
| 21 | func newGenSchemaCommand() *cobra.Command { |
| 22 | genschemaCommand := &cobra.Command{ |
| 23 | Use: "generate-jsonschema", |
| 24 | Short: "Generate json-schema document", |
| 25 | Args: WrapArgsError(cobra.ArbitraryArgs), |
| 26 | RunE: genschemaAction, |
| 27 | Hidden: true, |
| 28 | } |
| 29 | genschemaCommand.Flags().String("schemafile", "", "Output file") |
| 30 | return genschemaCommand |
| 31 | } |
| 32 | |
| 33 | func toAny(args []string) []any { |
| 34 | result := []any{nil} |
| 35 | for _, arg := range args { |
| 36 | result = append(result, arg) |
| 37 | } |
| 38 | return result |
| 39 | } |
| 40 | |
| 41 | func getProp(props *orderedmap.OrderedMap[string, *jsonschema.Schema], key string) *jsonschema.Schema { |
| 42 | value, ok := props.Get(key) |
| 43 | if !ok { |
| 44 | return nil |
| 45 | } |
| 46 | return value |
| 47 | } |
| 48 | |
| 49 | func genschemaAction(cmd *cobra.Command, args []string) error { |
| 50 | file, err := cmd.Flags().GetString("schemafile") |
| 51 | if err != nil { |
| 52 | return err |
| 53 | } |
| 54 | |
| 55 | reflector := new(jsonschema.Reflector) |
| 56 | err = reflector.AddGoComments("github.com/lima-vm/lima/v2", "./") |
| 57 | if err != nil { |
| 58 | return err |
| 59 | } |
| 60 | schema := reflector.Reflect(&limatype.LimaYAML{}) |
| 61 | |
| 62 | schema.Definitions["Disk"].Type = "" |
| 63 | schema.Definitions["Disk"].OneOf = []*jsonschema.Schema{ |
| 64 | {Type: "string"}, |
| 65 | {Type: "object"}, |
| 66 | } |
| 67 | |
| 68 | schema.Definitions["BaseTemplates"].Type = "" |
| 69 | schema.Definitions["BaseTemplates"].OneOf = []*jsonschema.Schema{ |
| 70 | {Type: "string"}, |
| 71 | {Type: "array"}, |
| 72 | } |
| 73 | |
| 74 | schema.Definitions["LocatorWithDigest"].Type = "" |
| 75 | schema.Definitions["LocatorWithDigest"].OneOf = []*jsonschema.Schema{ |
| 76 | {Type: "string"}, |
| 77 | {Type: "object"}, |
| 78 | } |
| 79 | properties := schema.Definitions["LimaYAML"].Properties |
| 80 | getProp(properties, "os").Enum = toAny(limatype.OSTypes) |
| 81 | getProp(properties, "arch").Enum = toAny(limatype.ArchTypes) |
| 82 | getProp(properties, "mountType").Enum = toAny(limatype.MountTypes) |
| 83 | getProp(properties, "vmType").Enum = toAny(limatype.VMTypes) |
| 84 | j, err := json.MarshalIndent(schema, "", " ") |
| 85 | if err != nil { |
| 86 | return err |
| 87 | } |
| 88 | if len(args) == 0 { |
| 89 | _, err = fmt.Fprintln(cmd.OutOrStdout(), string(j)) |
| 90 | return err |
| 91 | } |
| 92 | |
| 93 | if file == "" { |
| 94 | return errors.New("need --schemafile to validate") |
| 95 | } |
| 96 | err = os.WriteFile(file, j, 0o644) |
| 97 | if err != nil { |
| 98 | return err |
| 99 | } |
| 100 | for _, f := range args { |
| 101 | err = jsonschemautil.Validate(file, f) |
| 102 | if err != nil { |
| 103 | return fmt.Errorf("%#q: %w", f, err) |
| 104 | } |
| 105 | logrus.Infof("%#q: OK", f) |
| 106 | } |
| 107 | |
| 108 | return err |
| 109 | } |