kuma
131 строка · 3.0 Кб
1package cmd
2
3import (
4"os"
5"path/filepath"
6"text/template"
7
8"github.com/spf13/cobra"
9
10"github.com/kumahq/kuma/tools/policy-gen/generator/pkg/parse"
11"github.com/kumahq/kuma/tools/policy-gen/generator/pkg/save"
12)
13
14func newHelpers(rootArgs *args) *cobra.Command {
15cmd := &cobra.Command{
16Use: "helpers",
17Short: "Generate helper funcs for the policy",
18Long: "Generate helper funcs for the policy.",
19RunE: func(cmd *cobra.Command, _ []string) error {
20policyName := filepath.Base(rootArgs.pluginDir)
21policyPath := filepath.Join(rootArgs.pluginDir, "api", rootArgs.version, policyName+".go")
22if _, err := os.Stat(policyPath); err != nil {
23return err
24}
25
26pconfig, err := parse.Policy(policyPath)
27if err != nil {
28return err
29}
30
31outPath := filepath.Join(filepath.Dir(policyPath), "zz_generated.helpers.go")
32return save.GoTemplate(helpersTemplate, map[string]interface{}{
33"name": pconfig.Name,
34"version": pconfig.Package,
35"generateTo": pconfig.HasTo,
36"generateFrom": pconfig.HasFrom,
37"skipGetDefault": pconfig.SkipGetDefault,
38"generateGetPolicyItem": !pconfig.HasFrom && !pconfig.HasTo,
39}, outPath)
40},
41}
42
43return cmd
44}
45
46var helpersTemplate = template.Must(template.New("missingkey=error").Parse(
47`
48// Generated by tools/policy-gen.
49// Run "make generate" to update this file.
50
51// nolint:whitespace
52package {{.version}}
53
54import (
55common_api "github.com/kumahq/kuma/api/common/v1alpha1"
56core_model "github.com/kumahq/kuma/pkg/core/resources/model"
57)
58
59func (x *{{.name}}) GetTargetRef() common_api.TargetRef {
60return x.TargetRef
61}
62
63{{ if .generateFrom }}
64
65func (x *From) GetTargetRef() common_api.TargetRef {
66return x.TargetRef
67}
68{{ if not .skipGetDefault }}
69
70func (x *From) GetDefault() interface{} {
71return x.Default
72}
73{{- end }}
74
75func (x *{{.name}}) GetFromList() []core_model.PolicyItem {
76var result []core_model.PolicyItem
77for i := range x.From {
78item := x.From[i]
79result = append(result, &item)
80}
81return result
82}
83{{- end }}
84{{ if .generateTo }}
85
86func (x *To) GetTargetRef() common_api.TargetRef {
87return x.TargetRef
88}
89{{ if not .skipGetDefault }}
90
91func (x *To) GetDefault() interface{} {
92return x.Default
93}
94{{- end }}
95
96func (x *{{.name}}) GetToList() []core_model.PolicyItem {
97var result []core_model.PolicyItem
98for i := range(x.To) {
99item := x.To[i]
100result = append(result, &item)
101}
102return result
103}
104{{- end }}
105
106{{ if .generateGetPolicyItem}}
107{{ if not .skipGetDefault }}
108
109func (x *{{.name}}) GetDefault() interface{} {
110return x.Default
111}
112{{- end }}
113
114func (x *{{.name}}) GetPolicyItem() core_model.PolicyItem {
115return &policyItem{
116{{.name}}: x,
117}
118}
119
120// policyItem is an auxiliary struct with the implementation of the GetTargetRef() to always return empty result
121type policyItem struct {
122*{{.name}}
123}
124
125var _ core_model.PolicyItem = &policyItem{}
126
127func (p *policyItem) GetTargetRef() common_api.TargetRef {
128return common_api.TargetRef{Kind: common_api.Mesh}
129}
130{{- end }}
131`))
132