From f8f49a6257f94f7e30038f3a76797db0da0a983c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20F=20Bj=C3=B6rklund?= Date: Thu, 16 May 2024 20:08:32 +0200 Subject: [PATCH] Add jsonschema enum for the property types MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Anders F Björklund --- cmd/limactl/genschema.go | 22 ++++++++++++++++++++++ go.mod | 2 +- pkg/limayaml/limayaml.go | 9 +++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/cmd/limactl/genschema.go b/cmd/limactl/genschema.go index 09bec5e930f..92fb56f112f 100644 --- a/cmd/limactl/genschema.go +++ b/cmd/limactl/genschema.go @@ -7,6 +7,7 @@ import ( "github.com/invopop/jsonschema" "github.com/lima-vm/lima/pkg/limayaml" "github.com/spf13/cobra" + orderedmap "github.com/wk8/go-ordered-map/v2" ) func newGenSchemaCommand() *cobra.Command { @@ -20,6 +21,22 @@ func newGenSchemaCommand() *cobra.Command { return genschemaCommand } +func toAny(args []string) []any { + result := []any{nil} + for _, arg := range args { + result = append(result, arg) + } + return result +} + +func getProp(props *orderedmap.OrderedMap[string, *jsonschema.Schema], key string) *jsonschema.Schema { + value, ok := props.Get(key) + if !ok { + return nil + } + return value +} + func genschemaAction(cmd *cobra.Command, _ []string) error { schema := jsonschema.Reflect(&limayaml.LimaYAML{}) // allow Disk to be either string (name) or object (struct) @@ -28,6 +45,11 @@ func genschemaAction(cmd *cobra.Command, _ []string) error { {Type: "string"}, {Type: "object"}, } + properties := schema.Definitions["LimaYAML"].Properties + getProp(properties, "os").Enum = toAny(limayaml.OSTypes) + getProp(properties, "arch").Enum = toAny(limayaml.ArchTypes) + getProp(properties, "mountType").Enum = toAny(limayaml.MountTypes) + getProp(properties, "vmType").Enum = toAny(limayaml.VMTypes) j, err := json.MarshalIndent(schema, "", " ") if err != nil { return err diff --git a/go.mod b/go.mod index a79475e0de7..4e40cb7982b 100644 --- a/go.mod +++ b/go.mod @@ -39,6 +39,7 @@ require ( github.com/sirupsen/logrus v1.9.4-0.20230606125235-dd1b4c2e81af github.com/spf13/cobra v1.8.1 github.com/spf13/pflag v1.0.5 + github.com/wk8/go-ordered-map/v2 v2.1.8 golang.org/x/net v0.28.0 golang.org/x/sync v0.8.0 golang.org/x/sys v0.24.0 @@ -108,7 +109,6 @@ require ( github.com/rivo/uniseg v0.2.0 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/u-root/uio v0.0.0-20240224005618-d2acac8f3701 // indirect - github.com/wk8/go-ordered-map/v2 v2.1.8 // indirect github.com/x448/float16 v0.8.4 // indirect github.com/yuin/gopher-lua v1.1.1 // indirect go.uber.org/atomic v1.7.0 // indirect diff --git a/pkg/limayaml/limayaml.go b/pkg/limayaml/limayaml.go index 6d615294cbb..cb595dd75bc 100644 --- a/pkg/limayaml/limayaml.go +++ b/pkg/limayaml/limayaml.go @@ -47,7 +47,9 @@ type LimaYAML struct { type ( OS = string + OSType = OS Arch = string + ArchType = Arch MountType = string VMType = string ) @@ -72,6 +74,13 @@ const ( WSL2 VMType = "wsl2" ) +var ( + OSTypes = []OSType{LINUX} + ArchTypes = []ArchType{X8664, AARCH64, ARMV7L, RISCV64} + MountTypes = []MountType{REVSSHFS, NINEP, VIRTIOFS, WSLMount} + VMTypes = []VMType{QEMU, VZ, WSL2} +) + type Rosetta struct { Enabled *bool `yaml:"enabled,omitempty" json:"enabled,omitempty" jsonschema:"nullable"` BinFmt *bool `yaml:"binfmt,omitempty" json:"binfmt,omitempty" jsonschema:"nullable"`