refacto(labels): use external files

Files are more readable as external. Use "go:embed" to inject them.
This commit is contained in:
2024-11-21 11:08:55 +01:00
parent 8c97937b44
commit 3f63375b60
3 changed files with 38 additions and 26 deletions

View File

@@ -0,0 +1,14 @@
## {{ .KatenaryPrefix }}/{{ .Name }}
{{ .Help.Short }}
**Type**: `{{ .Help.Type }}`
{{ .Help.Long }}
**Example:**
```yaml
{{ .Help.Example }}
```

View File

@@ -0,0 +1,9 @@
{{ .KatenaryPrefix }}/{{ .Name }}: {{ .Help.Short }}
Type: {{ .Help.Type }}
{{ .Help.Long }}
Example:
{{ .Help.Example }}

View File

@@ -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
}