-
Notifications
You must be signed in to change notification settings - Fork 625
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2306 from afbjorklund/schema
Add command to generate jsonschema for limayaml
- Loading branch information
Showing
14 changed files
with
173 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
_output/ | ||
_artifacts/ | ||
lima.REJECTED.yaml | ||
schema-limayaml.json | ||
.config |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
"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 { | ||
genschemaCommand := &cobra.Command{ | ||
Use: "generate-jsonschema", | ||
Short: "Generate json-schema document", | ||
Args: WrapArgsError(cobra.NoArgs), | ||
RunE: genschemaAction, | ||
Hidden: true, | ||
} | ||
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) | ||
schema.Definitions["Disk"].Type = "" // was: "object" | ||
schema.Definitions["Disk"].OneOf = []*jsonschema.Schema{ | ||
{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 | ||
} | ||
_, err = fmt.Fprintln(cmd.OutOrStdout(), string(j)) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.