-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd.go
More file actions
103 lines (88 loc) · 2.6 KB
/
Copy pathcmd.go
File metadata and controls
103 lines (88 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
* Copyright 2025 - 2026 Zigflow authors <https://github.com/zigflow/schema/graphs/contributors>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package schema
import (
_ "embed"
"encoding/json"
"fmt"
"strings"
"github.com/google/jsonschema-go/jsonschema"
gh "github.com/mrsimonemms/golang-helpers"
"github.com/rs/zerolog"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"sigs.k8s.io/yaml"
)
func NewSchemaCmd(version string) *cobra.Command {
var opts struct {
Output string
}
cmd := &cobra.Command{
Use: "schema",
Short: "Output the Zigflow JSON schema.",
Long: `Output the JSON Schema for the Zigflow workflow specification.
The schema can be used by editors, validation tools and AI code
generators to produce structurally valid Zigflow workflows. It defines
required fields, supported properties and constraints enforced by the
Zigflow CLI.
By exposing the schema programmatically, Zigflow enables reliable
validation, structured generation and automated tooling integration.`,
RunE: func(cmd *cobra.Command, args []string) error {
v := opts.Output
var fn func(*jsonschema.Schema) ([]byte, error)
switch v {
case "json":
fn = func(s *jsonschema.Schema) ([]byte, error) {
return json.MarshalIndent(s, "", " ")
}
case "yaml":
fn = func(s *jsonschema.Schema) ([]byte, error) {
return yaml.Marshal(s)
}
default:
return gh.FatalError{
Msg: "Invalid output",
WithParams: func(l *zerolog.Event) *zerolog.Event {
return l.Str("output", v)
},
}
}
// Build the schema
schema, err := BuildSchema(version, v)
if err != nil {
return gh.FatalError{
Cause: err,
Msg: "Error building Zigflow schema",
}
}
res, err := fn(schema)
if err != nil {
return gh.FatalError{
Cause: err,
Msg: "Error building the schema in the desired output",
}
}
fmt.Println(strings.TrimSpace(string(res)))
return nil
},
}
viper.Set("output", "json")
cmd.Flags().StringVarP(
&opts.Output, "output", "o",
viper.GetString("output"), "Output format. One of: (json, yaml)",
)
return cmd
}