refacto(labels): use external files
Files are more readable as external. Use "go:embed" to inject them.
This commit is contained in:
14
generator/labels/help-template.md.tpl
Normal file
14
generator/labels/help-template.md.tpl
Normal file
@@ -0,0 +1,14 @@
|
||||
## {{ .KatenaryPrefix }}/{{ .Name }}
|
||||
|
||||
{{ .Help.Short }}
|
||||
|
||||
**Type**: `{{ .Help.Type }}`
|
||||
|
||||
{{ .Help.Long }}
|
||||
|
||||
**Example:**
|
||||
|
||||
```yaml
|
||||
{{ .Help.Example }}
|
||||
```
|
||||
|
9
generator/labels/help-template.tpl
Normal file
9
generator/labels/help-template.tpl
Normal file
@@ -0,0 +1,9 @@
|
||||
{{ .KatenaryPrefix }}/{{ .Name }}: {{ .Help.Short }}
|
||||
Type: {{ .Help.Type }}
|
||||
|
||||
{{ .Help.Long }}
|
||||
|
||||
Example:
|
||||
|
||||
{{ .Help.Example }}
|
||||
|
@@ -42,6 +42,12 @@ var (
|
||||
|
||||
// parsed yaml
|
||||
labelFullHelp map[string]Help
|
||||
|
||||
//go:embed help-template.tpl
|
||||
helpTemplatePlain string
|
||||
|
||||
//go:embed help-template.md.tpl
|
||||
helpTemplateMarkdown string
|
||||
)
|
||||
|
||||
// Label is a katenary label to find in compose files.
|
||||
@@ -95,9 +101,6 @@ func GetLabelHelpFor(labelname string, asMarkdown bool) string {
|
||||
help.Example = strings.TrimPrefix(help.Example, "\n")
|
||||
help.Short = strings.TrimPrefix(help.Short, "\n")
|
||||
|
||||
// get help template
|
||||
helpTemplate := getHelpTemplate(asMarkdown)
|
||||
|
||||
if asMarkdown {
|
||||
// enclose templates in backticks
|
||||
help.Long = regexp.MustCompile(`\{\{(.*?)\}\}`).ReplaceAllString(help.Long, "`{{$1}}`")
|
||||
@@ -110,6 +113,15 @@ func GetLabelHelpFor(labelname string, asMarkdown bool) string {
|
||||
help.Long = utils.WordWrap(help.Long, 80)
|
||||
}
|
||||
|
||||
// get help template
|
||||
var helpTemplate string
|
||||
switch asMarkdown {
|
||||
case true:
|
||||
helpTemplate = helpTemplateMarkdown
|
||||
case false:
|
||||
helpTemplate = helpTemplatePlain
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
template.Must(template.New("shorthelp").Parse(help.Long)).Execute(&buf, struct {
|
||||
KatenaryPrefix string
|
||||
@@ -207,29 +219,6 @@ func generateTableHeaderSeparator(maxNameLength, maxDescriptionLength, maxTypeLe
|
||||
)
|
||||
}
|
||||
|
||||
func getHelpTemplate(asMarkdown bool) string {
|
||||
if asMarkdown {
|
||||
return `## {{ .KatenaryPrefix }}/{{ .Name }}
|
||||
|
||||
{{ .Help.Short }}
|
||||
|
||||
**Type**: ` + "`" + `{{ .Help.Type }}` + "`" + `
|
||||
|
||||
{{ .Help.Long }}
|
||||
|
||||
**Example:**` + "\n\n```yaml\n" + `{{ .Help.Example }}` + "\n```\n"
|
||||
}
|
||||
|
||||
return `{{ .KatenaryPrefix }}/{{ .Name }}: {{ .Help.Short }}
|
||||
Type: {{ .Help.Type }}
|
||||
|
||||
{{ .Help.Long }}
|
||||
|
||||
Example:
|
||||
{{ .Help.Example }}
|
||||
`
|
||||
}
|
||||
|
||||
func Prefix() string {
|
||||
return KatenaryLabelPrefix
|
||||
}
|
||||
|
Reference in New Issue
Block a user