add more generic documentation via wwdoc(n)

doc
This commit is contained in:
Christian Goll
2025-10-20 11:47:56 +02:00
committed by Jonathon Anderson
parent 68758e3e45
commit d1d398d2ff
4 changed files with 45 additions and 17 deletions

View File

@@ -10,6 +10,7 @@ import (
"github.com/warewulf/warewulf/internal/pkg/node"
"github.com/warewulf/warewulf/internal/pkg/overlay"
"github.com/warewulf/warewulf/internal/pkg/wwlog"
"golang.org/x/exp/maps"
)
func CobraRunE(cmd *cobra.Command, args []string) error {
@@ -30,6 +31,13 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
if vars == nil {
return fmt.Errorf("could not parse variables for %s in overlay %s", filePath, overlayName)
}
commentKeys := maps.Keys(commentMap)
sort.Strings(commentKeys)
for _, docLn := range commentKeys {
if strings.Contains(docLn, "wwdoc") {
wwlog.Info(commentMap[docLn])
}
}
t := table.New(cmd.OutOrStdout())
t.AddHeader("OVERLAY VARIABLE", "HELP", "TYPE", "CMD OPTION")

View File

@@ -13,10 +13,13 @@ import (
func Test_Overlay_Variables(t *testing.T) {
env := testenv.New(t)
defer env.RemoveAll()
wwlog.SetLogLevel(wwlog.DEBUG)
warewulfd.SetNoDaemon()
templateContent := `
{{/* .Kernel.Tags.foo: "some help text" */}}
{{/* wwdoc1: First Line */}}
{{/* wwdoc2: Second Line */}}
{{ .Kernel.Tags.foo }}
{{ .Node.Tags.bar }}
{{ .Cluster.Tags.baz }}
@@ -39,6 +42,7 @@ func Test_Overlay_Variables(t *testing.T) {
assert.Contains(t, output, "OVERLAY VARIABLE")
assert.Contains(t, output, ".Kernel.Tags.foo")
assert.Contains(t, output, "some help text")
assert.Regexp(t, `(?s)First Line.*Second Line`, output, "First Line should come before Second Line")
})
t.Run("overlay variables no file", func(t *testing.T) {

View File

@@ -16,7 +16,6 @@ import (
"github.com/Masterminds/sprig/v3"
"github.com/coreos/go-systemd/v22/unit"
"gopkg.in/yaml.v2"
"github.com/warewulf/warewulf/internal/pkg/config"
"github.com/warewulf/warewulf/internal/pkg/node"
@@ -312,7 +311,8 @@ func (overlay Overlay) ParseVars(file string) []string {
// ParseCommentVars parses a template file for comments that contain variable documentations.
// The comments must be in the format `{{/* key: value */}}`. The content is parsed as YAML.
func (overlay Overlay) ParseCommentVars(file string) map[string]string {
func (overlay Overlay) ParseCommentVars(file string) (retMap map[string]string) {
retMap = make(map[string]string)
if !strings.HasSuffix(file, ".ww") {
return nil
}
@@ -328,24 +328,15 @@ func (overlay Overlay) ParseCommentVars(file string) map[string]string {
return nil
}
vars := make(map[string]string)
re := regexp.MustCompile(`{{\s*/\*(.*?)\*/\s*}}`)
re := regexp.MustCompile(`{{\s*/\*\s*(.*?):\s*(.*?)\s*\*/\s*}}`)
matches := re.FindAllStringSubmatch(string(content), -1)
for _, match := range matches {
commentContent := strings.TrimSpace(match[1])
var data map[string]string
err := yaml.Unmarshal([]byte(commentContent), &data)
if err == nil {
for k, v := range data {
vars[k] = v
}
} else {
wwlog.Debug("Could not parse template comment as yaml in file %s: %s", file, err)
wwlog.Debug("matches: %v len(%d:%d)", matches, len(matches), len(matches[0]))
for i := range matches {
if len(matches[i]) > 2 {
retMap[matches[i][1]] = matches[i][2]
}
}
return vars
return
}
// walkParseTree recursively traverses the template's parse tree to find variables.