From 3f63375b6068f887b7d2717e25ee9a6d70d27195 Mon Sep 17 00:00:00 2001 From: Patrice Ferlet Date: Thu, 21 Nov 2024 11:08:55 +0100 Subject: [PATCH] refacto(labels): use external files Files are more readable as external. Use "go:embed" to inject them. --- generator/labels/help-template.md.tpl | 14 +++++++++ generator/labels/help-template.tpl | 9 ++++++ generator/labels/katenaryLabels.go | 41 ++++++++++----------------- 3 files changed, 38 insertions(+), 26 deletions(-) create mode 100644 generator/labels/help-template.md.tpl create mode 100644 generator/labels/help-template.tpl diff --git a/generator/labels/help-template.md.tpl b/generator/labels/help-template.md.tpl new file mode 100644 index 0000000..73189e7 --- /dev/null +++ b/generator/labels/help-template.md.tpl @@ -0,0 +1,14 @@ +## {{ .KatenaryPrefix }}/{{ .Name }} + +{{ .Help.Short }} + +**Type**: `{{ .Help.Type }}` + +{{ .Help.Long }} + +**Example:** + +```yaml +{{ .Help.Example }} +``` + diff --git a/generator/labels/help-template.tpl b/generator/labels/help-template.tpl new file mode 100644 index 0000000..8c820f8 --- /dev/null +++ b/generator/labels/help-template.tpl @@ -0,0 +1,9 @@ +{{ .KatenaryPrefix }}/{{ .Name }}: {{ .Help.Short }} +Type: {{ .Help.Type }} + +{{ .Help.Long }} + +Example: + +{{ .Help.Example }} + diff --git a/generator/labels/katenaryLabels.go b/generator/labels/katenaryLabels.go index 958c6a6..8d22293 100644 --- a/generator/labels/katenaryLabels.go +++ b/generator/labels/katenaryLabels.go @@ -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 }