跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

genschema.go

109 行 · 2.8 KB历史下载
1// SPDX-FileCopyrightText: Copyright The Lima Authors
2// SPDX-License-Identifier: Apache-2.0
3
4package main
5
6import (
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
21func 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
33func toAny(args []string) []any {
34 result := []any{nil}
35 for _, arg := range args {
36 result = append(result, arg)
37 }
38 return result
39}
40
41func 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
49func 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 // allow Disk to be either string (name) or object (struct)
62 schema.Definitions["Disk"].Type = "" // was: "object"
63 schema.Definitions["Disk"].OneOf = []*jsonschema.Schema{
64 {Type: "string"},
65 {Type: "object"},
66 }
67 // allow BaseTemplates to be either string (url) or array (array)
68 schema.Definitions["BaseTemplates"].Type = "" // was: "array"
69 schema.Definitions["BaseTemplates"].OneOf = []*jsonschema.Schema{
70 {Type: "string"},
71 {Type: "array"},
72 }
73 // allow LocatorWithDigest to be either string (url) or object (struct)
74 schema.Definitions["LocatorWithDigest"].Type = "" // was: "object"
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}