-
Notifications
You must be signed in to change notification settings - Fork 33
/
command.go
48 lines (45 loc) · 1.05 KB
/
command.go
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
package schemas
import (
"errors"
"io/fs"
"os"
"path"
"path/filepath"
"github.com/kyverno/chainsaw/pkg/data"
"github.com/spf13/cobra"
)
func Command() *cobra.Command {
cmd := &cobra.Command{
Use: "schemas",
Short: "Export JSON schemas",
Args: cobra.ExactArgs(1),
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
outPath := args[0]
if _, err := os.Stat(outPath); err != nil {
if !errors.Is(err, os.ErrNotExist) {
return err
}
if err := os.MkdirAll(outPath, os.ModeDir|os.ModePerm); err != nil {
return err
}
}
schemasFs := data.Schemas()
entries, err := fs.ReadDir(schemasFs, path.Join("schemas", "json"))
if err != nil {
return err
}
for _, entry := range entries {
input, err := fs.ReadFile(schemasFs, path.Join("schemas", "json", entry.Name()))
if err != nil {
return err
}
if err := os.WriteFile(filepath.Join(outPath, entry.Name()), input, os.ModePerm); err != nil {
return err
}
}
return nil
},
}
return cmd
}