-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
181 lines (156 loc) · 3.42 KB
/
main.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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package main
import (
_ "embed"
"errors"
"flag"
"fmt"
"io/fs"
"os"
"path"
"path/filepath"
"strings"
"github.com/emcfarlane/starlarkproto"
"go.starlark.net/starlark"
"google.golang.org/protobuf/encoding/protojson"
"sigs.k8s.io/yaml"
)
var (
flagOutDir = flag.String("out", "", "Out directory.")
flagGlobal = flag.String("global", "", "Global starlark file.")
flagVerbose = flag.Bool("v", false, "Verbose mode.")
)
func run() error {
flag.Parse()
args := flag.Args() // kubestar examples/nginx_deployment.star -> examples/nginx_deploment.yaml
if len(args) < 1 {
return fmt.Errorf("missing files glob: e.g. \"*.star\"")
}
pattern := args[0]
if _, err := path.Match(pattern, ""); err != nil {
return err // invalid pattern
}
fsys := os.DirFS(".")
loader := NewLoader(fsys, globals)
thread := &starlark.Thread{
Load: loader.Load,
}
if *flagGlobal != "" {
thread.Name = *flagGlobal
values, err := loader.Load(thread, *flagGlobal)
if err != nil {
return err
}
for k, v := range values {
globals[k] = v
}
}
files, err := fs.Glob(fsys, pattern)
if err != nil {
return err
}
protos := make(map[string][]*starlarkproto.Message, len(files))
for _, name := range files {
thread.Name = name
values, err := loader.Load(thread, name)
if err != nil {
return err
}
mainFn, ok := values["main"]
if !ok {
continue
}
v, err := starlark.Call(thread, mainFn, nil, nil)
if err != nil {
return err
}
switch v := v.(type) {
case *starlarkproto.Message:
protos[name] = append(protos[name], v)
case *starlark.List:
for i := 0; i < v.Len(); i++ {
vpb, ok := v.Index(i).(*starlarkproto.Message)
if !ok {
return fmt.Errorf("%s: expected *starlarkproto.Message got %T", name, vpb)
}
protos[name] = append(protos[name], vpb)
}
default:
return fmt.Errorf("%s: expected *starlarkproto.Message got %T", name, v)
}
}
// encode to yaml
for name, vpbs := range protos {
if err := encode(name, vpbs); err != nil {
return err
}
}
return nil
}
const yamlTmpl = `# Code generated by kubestar. DO NOT EDIT.
# source: %s
`
const objTmpl = `apiVersion: %s
kind: %s
`
func encode(name string, protos []*starlarkproto.Message) error {
filename := path.Join(
*flagOutDir,
strings.TrimSuffix(
name, filepath.Ext(name),
),
) + ".yaml"
f, err := os.Create(filename)
if err != nil {
return err
}
defer f.Close()
for i, vpb := range protos {
if i == 0 {
if _, err := fmt.Fprintf(
f, yamlTmpl, name,
); err != nil {
return err
}
} else {
if _, err := f.WriteString("---\n"); err != nil {
return err
}
}
jb, err := protojson.Marshal(vpb)
if err != nil {
return err
}
yb, err := yaml.JSONToYAML(jb)
if err != nil {
return err
}
// encode resource information:
// https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#resources
md := vpb.ProtoReflect().Descriptor()
kind := string(md.Name())
apiVersion := genAPIVersion(md.ParentFile())
if _, err := fmt.Fprintf(
f, objTmpl, apiVersion, kind,
); err != nil {
return err
}
if _, err := f.Write(yb); err != nil {
return err
}
if *flagVerbose {
fmt.Println(filename)
}
}
return nil
}
func main() {
if err := run(); err != nil {
var evalErr *starlark.EvalError
if errors.As(err, &evalErr) {
fmt.Println(evalErr.Backtrace())
} else {
fmt.Println(err)
}
os.Exit(1)
}
}