kuma

Форк
0
131 строка · 3.0 Кб
1
package cmd
2

3
import (
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

14
func newHelpers(rootArgs *args) *cobra.Command {
15
	cmd := &cobra.Command{
16
		Use:   "helpers",
17
		Short: "Generate helper funcs for the policy",
18
		Long:  "Generate helper funcs for the policy.",
19
		RunE: func(cmd *cobra.Command, _ []string) error {
20
			policyName := filepath.Base(rootArgs.pluginDir)
21
			policyPath := filepath.Join(rootArgs.pluginDir, "api", rootArgs.version, policyName+".go")
22
			if _, err := os.Stat(policyPath); err != nil {
23
				return err
24
			}
25

26
			pconfig, err := parse.Policy(policyPath)
27
			if err != nil {
28
				return err
29
			}
30

31
			outPath := filepath.Join(filepath.Dir(policyPath), "zz_generated.helpers.go")
32
			return 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

43
	return cmd
44
}
45

46
var 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
52
package {{.version}}
53

54
import (
55
	common_api "github.com/kumahq/kuma/api/common/v1alpha1"
56
	core_model "github.com/kumahq/kuma/pkg/core/resources/model"
57
)
58

59
func (x *{{.name}}) GetTargetRef() common_api.TargetRef {
60
	return x.TargetRef
61
}
62

63
{{ if .generateFrom }}
64

65
func (x *From) GetTargetRef() common_api.TargetRef {
66
	return x.TargetRef
67
}
68
{{ if not .skipGetDefault }}
69

70
func (x *From) GetDefault() interface{} {
71
	return x.Default
72
}
73
{{- end }}
74

75
func (x *{{.name}}) GetFromList() []core_model.PolicyItem {
76
	var result []core_model.PolicyItem
77
	for i  := range x.From {
78
		item := x.From[i]
79
		result = append(result, &item)
80
	}
81
	return result
82
}
83
{{- end }}
84
{{ if .generateTo }}
85

86
func (x *To) GetTargetRef() common_api.TargetRef {
87
	return x.TargetRef
88
}
89
{{ if not .skipGetDefault }}
90

91
func (x *To) GetDefault() interface{} {
92
	return x.Default
93
}
94
{{- end }}
95

96
func (x *{{.name}}) GetToList() []core_model.PolicyItem {
97
	var result []core_model.PolicyItem
98
	for i := range(x.To) {
99
		item := x.To[i]
100
		result = append(result, &item)
101
	}
102
	return result
103
}
104
{{- end }}
105

106
{{ if .generateGetPolicyItem}}
107
{{ if not .skipGetDefault }}
108

109
func (x *{{.name}}) GetDefault() interface{} {
110
	return x.Default
111
}
112
{{- end }}
113

114
func (x *{{.name}}) GetPolicyItem() core_model.PolicyItem {
115
	return &policyItem{
116
		{{.name}}: x,
117
	}
118
}
119

120
// policyItem is an auxiliary struct with the implementation of the GetTargetRef() to always return empty result
121
type policyItem struct {
122
	*{{.name}}
123
}
124

125
var _ core_model.PolicyItem = &policyItem{}
126

127
func (p *policyItem) GetTargetRef() common_api.TargetRef {
128
	return common_api.TargetRef{Kind: common_api.Mesh}
129
}
130
{{- end }}
131
`))
132

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.