/
show_template_functions.go
223 lines (197 loc) · 6.35 KB
/
show_template_functions.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package command
import (
"encoding/json"
"fmt"
"github.com/alecthomas/kingpin"
"github.com/echocat/kubor/common"
"github.com/echocat/kubor/template/functions"
"gopkg.in/yaml.v2"
"os"
"regexp"
"strings"
)
const (
showTemplateFunctionsTemplate = `{{$width := .Width -}}
List of available function within kubor rending context by category:
{{range $categoryName, $category := .Categories -}}
{{ $categoryName | upper }}:{{range $functionName, $function := $category.Functions}}
{{- "\n {{ " -}}{{- $functionName -}}
{{- range $i, $parameter := $function.Parameters }}
{{- if $parameter.VarArg }} [<{{ $parameter.GetName }}> ...]
{{- else }} <{{ $parameter.GetName }}>{{ end -}}
{{- end -}}{{- " }}" }}
{{- if $function.Description }}
{{ $function.Description | replace "\n" " " | warpCustom $width "\n " false }}{{ end -}}
{{- range $i, $parameter := $function.Parameters }}
- {{ $parameter.GetName }}: {{ $parameter.GetType -}}
{{- if $parameter.Description }} - {{ $parameter.Description | replace "\n" " " | warpCustom $width "\n " false }}{{ end -}}
{{- end }}
Returns: {{ $function.Returns.GetType -}}
{{- if $function.Returns.Description }} - {{ $function.Returns.Description | replace "\n" " " | warpCustom $width "\n " false }}{{ end }}
{{ end }}
{{ end -}}
NOTES:
Please refer https://golang.org/pkg/text/template/ for more information about the template language of kubor.
`
)
type ShowTemplateFunctionsOutput string
func (instance *ShowTemplateFunctionsOutput) Set(plain string) error {
if plain != "json" && plain != "yaml" && plain != "text" {
return fmt.Errorf("unsupported output format: %s", plain)
}
*instance = ShowTemplateFunctionsOutput(plain)
return nil
}
func (instance ShowTemplateFunctionsOutput) String() string {
return string(instance)
}
type ShowTemplateFulltextTerm struct {
Regexp *regexp.Regexp
}
func (instance *ShowTemplateFulltextTerm) Set(plain string) error {
if plain == "" {
(*instance).Regexp = nil
return nil
}
p := plain
if !strings.HasPrefix(p, "(?") {
p = "(?i)" + p
}
if r, err := regexp.Compile(p); err != nil {
return err
} else {
(*instance).Regexp = r
return nil
}
}
func (instance ShowTemplateFulltextTerm) String() string {
r := instance.Regexp
if r == nil {
return ""
}
return r.String()
}
func init() {
cmd := &ShowTemplateFunctions{
Output: ShowTemplateFunctionsOutput("text"),
}
common.RegisterCliFactory(cmd)
}
type ShowTemplateFunctions struct {
Output ShowTemplateFunctionsOutput
FulltextSearchTerm ShowTemplateFulltextTerm
FunctionNameFilters []string
}
func (instance *ShowTemplateFunctions) ConfigureCliCommands(context string, hc common.HasCommands, version string) error {
if context != "show/template" {
return nil
}
cmd := hc.Command("functions", "Shows a list of all available template functions.").
Action(instance.ExecuteFromCli)
cmd.Arg("function-name-regexp", "Filter for functions that matches at least one of the given patterns.").
StringsVar(&instance.FunctionNameFilters)
cmd.Flag("output", "Specifies how to render the output.").
Short('o').
SetValue(&instance.Output)
cmd.Flag("fulltextSearch", "Specifies a term that needs to be matched in any property of a function (name, description, ...)").
Short('s').
SetValue(&instance.FulltextSearchTerm)
return nil
}
func (instance *ShowTemplateFunctions) createPredicate() (func(name string) bool, error) {
regexps := make([]*regexp.Regexp, len(instance.FunctionNameFilters))
for i, pattern := range instance.FunctionNameFilters {
if r, err := regexp.Compile(pattern); err != nil {
return nil, fmt.Errorf("problems while compile function regexp pattern: %w", err)
} else {
regexps[i] = r
}
}
if len(regexps) <= 0 {
return func(name string) bool {
return true
}, nil
}
return func(name string) bool {
for _, r := range regexps {
if r.MatchString(name) {
return true
}
}
return false
}, nil
}
func (instance *ShowTemplateFunctions) createFulltextPredicate() func(functionName string, function functions.Function) bool {
term := instance.FulltextSearchTerm.Regexp
if term == nil {
return func(functionName string, function functions.Function) bool {
return true
}
}
return func(functionName string, function functions.Function) bool {
if term.FindStringIndex(functionName) != nil {
return true
}
if function.MatchesFulltextSearch(term) {
return true
}
return false
}
}
func (instance *ShowTemplateFunctions) ExecuteFromCli(_ *kingpin.ParseContext) error {
namePredicate, err := instance.createPredicate()
if err != nil {
return err
}
fulltextPredicate := instance.createFulltextPredicate()
context := instance.newShowTemplateFunctionsContext(namePredicate, fulltextPredicate, functions.CategoriesDefault)
switch instance.Output {
case ShowTemplateFunctionsOutput("yaml"):
return context.renderYaml()
case ShowTemplateFunctionsOutput("json"):
return context.renderJson()
default:
return context.renderText()
}
}
func (instance *ShowTemplateFunctions) newShowTemplateFunctionsContext(
namePredicate func(name string) bool,
fulltextPredicate func(functionName string, function functions.Function) bool,
categories functions.Categories,
) showTemplateFunctionsContext {
targetCategories := functions.Categories{}
for categoryName, category := range categories {
for functionName, function := range category.Functions {
if namePredicate(functionName) && fulltextPredicate(functionName, function) {
targetCategories = targetCategories.WithFunction(categoryName, functionName, function)
}
}
}
return showTemplateFunctionsContext{
Categories: targetCategories,
Width: common.GuessOutputWidth(os.Stdout),
}
}
type showTemplateFunctionsContext struct {
Categories functions.Categories
Width int
}
func (instance showTemplateFunctionsContext) renderYaml() error {
enc := yaml.NewEncoder(os.Stdout)
return enc.Encode(instance.Categories)
}
func (instance showTemplateFunctionsContext) renderJson() error {
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
return enc.Encode(instance.Categories)
}
func (instance showTemplateFunctionsContext) renderText() error {
if tmpl, err := functions.DefaultTemplateFactory().New(
"show_template_functions.go",
showTemplateFunctionsTemplate,
); err != nil {
return err
} else {
return tmpl.Execute(instance, os.Stdout)
}
}