Skip to content

Commit

Permalink
Add jsonschema enum for the property types
Browse files Browse the repository at this point in the history
Signed-off-by: Anders F Björklund <[email protected]>
  • Loading branch information
afbjorklund committed Oct 9, 2024
1 parent 98d6d48 commit 7b0cf97
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
22 changes: 22 additions & 0 deletions cmd/limactl/genschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,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.30.0
golang.org/x/sync v0.8.0
golang.org/x/sys v0.26.0
Expand Down Expand Up @@ -112,7 +113,6 @@ require (
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06 // 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
Expand Down
9 changes: 9 additions & 0 deletions pkg/limayaml/limayaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ type LimaYAML struct {

type (
OS = string
OSType = OS
Arch = string
ArchType = Arch
MountType = string
VMType = string
)
Expand All @@ -75,6 +77,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 VMOpts struct {
QEMU QEMUOpts `yaml:"qemu,omitempty" json:"qemu,omitempty"`
}
Expand Down

0 comments on commit 7b0cf97

Please sign in to comment.