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/node"
"github.com/warewulf/warewulf/internal/pkg/overlay" "github.com/warewulf/warewulf/internal/pkg/overlay"
"github.com/warewulf/warewulf/internal/pkg/wwlog" "github.com/warewulf/warewulf/internal/pkg/wwlog"
"golang.org/x/exp/maps"
) )
func CobraRunE(cmd *cobra.Command, args []string) error { func CobraRunE(cmd *cobra.Command, args []string) error {
@@ -30,6 +31,13 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
if vars == nil { if vars == nil {
return fmt.Errorf("could not parse variables for %s in overlay %s", filePath, overlayName) 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 := table.New(cmd.OutOrStdout())
t.AddHeader("OVERLAY VARIABLE", "HELP", "TYPE", "CMD OPTION") t.AddHeader("OVERLAY VARIABLE", "HELP", "TYPE", "CMD OPTION")

View File

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

View File

@@ -16,7 +16,6 @@ import (
"github.com/Masterminds/sprig/v3" "github.com/Masterminds/sprig/v3"
"github.com/coreos/go-systemd/v22/unit" "github.com/coreos/go-systemd/v22/unit"
"gopkg.in/yaml.v2"
"github.com/warewulf/warewulf/internal/pkg/config" "github.com/warewulf/warewulf/internal/pkg/config"
"github.com/warewulf/warewulf/internal/pkg/node" "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. // 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. // 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") { if !strings.HasSuffix(file, ".ww") {
return nil return nil
} }
@@ -328,24 +328,15 @@ func (overlay Overlay) ParseCommentVars(file string) map[string]string {
return nil return nil
} }
vars := make(map[string]string) re := regexp.MustCompile(`{{\s*/\*\s*(.*?):\s*(.*?)\s*\*/\s*}}`)
re := regexp.MustCompile(`{{\s*/\*(.*?)\*/\s*}}`)
matches := re.FindAllStringSubmatch(string(content), -1) matches := re.FindAllStringSubmatch(string(content), -1)
wwlog.Debug("matches: %v len(%d:%d)", matches, len(matches), len(matches[0]))
for _, match := range matches { for i := range matches {
commentContent := strings.TrimSpace(match[1]) if len(matches[i]) > 2 {
var data map[string]string retMap[matches[i][1]] = matches[i][2]
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)
} }
} }
return
return vars
} }
// walkParseTree recursively traverses the template's parse tree to find variables. // walkParseTree recursively traverses the template's parse tree to find variables.

View File

@@ -89,10 +89,13 @@ Creating and Modifying Overlays
You can add a new overlay to Warewulf with ``wwctl overlay create``. You can add a new overlay to Warewulf with ``wwctl overlay create``.
.. code-block:: shell .. code-block:: shell
wwctl overlay create issue wwctl overlay create issue
A new overlay is just an empty directory. For it to be useful it needs to A new overlay is just an empty directory. For it to be useful it needs to
contain some files. contain some files.
@@ -103,11 +106,14 @@ into the overlay.
wwctl overlay import --parents issue /etc/issue wwctl overlay import --parents issue /etc/issue
This imports ``/etc/issue`` from the Warewulf server into the new ``issue`` This imports ``/etc/issue`` from the Warewulf server into the new ``issue``
overlay. overlay.
.. note:: .. note::
The ``issue`` overlay already existed as a distribution overlay. Creating one The ``issue`` overlay already existed as a distribution overlay. Creating one
shadows the distribution overlay with a new site overlay, allowing for local shadows the distribution overlay with a new site overlay, allowing for local
modification. modification.
@@ -118,10 +124,12 @@ overlay.
You can also edit a new or existing overlay file in an interactive editor. You can also edit a new or existing overlay file in an interactive editor.
.. code-block:: shell .. code-block:: shell
wwctl overlay edit issue /etc/issue wwctl overlay edit issue /etc/issue
Use ``wwctl overlay show`` to inspect the content of an overlay file. Use ``wwctl overlay show`` to inspect the content of an overlay file.
.. code-block:: shell .. code-block:: shell
@@ -138,6 +146,8 @@ distribution to a given cluster node.
wwctl overlay import issue /etc/issue /etc/issue.ww wwctl overlay import issue /etc/issue /etc/issue.ww
wwctl overlay show issue /etc/issue.ww --render=n1 wwctl overlay show issue /etc/issue.ww --render=n1
More information about templates is available in :ref:`its own section More information about templates is available in :ref:`its own section
<templates>`. <templates>`.
@@ -151,6 +161,20 @@ option.
It is not possible to delete files with an overlay. It is not possible to delete files with an overlay.
Overlay Variables
-----------------
The command ``wwctl overlay variables`` can be used to show the used variables
in a template. The command also shows the help text for each variable. The help
text can be defined in two ways. The first is a general documentation which is
not tied to a variable. This can be done with the following syntax:
``{{/* wwdoc: Your documentation text */}}``. The second way is to define a help
text for a specific variable. This can be done with the following syntax:
``{{/* .My.Var: Your help text */}}``. If a help text is defined for a variable,
it will be used instead of the default help text.
Permissions Permissions
----------- -----------
@@ -163,6 +187,7 @@ mode that they have on the Warewulf server. Use ``wwctl overlay chown`` and
wwctl overlay chown issue /etc/issue.ww root root wwctl overlay chown issue /etc/issue.ww root root
wwctl overlay chmod issue /etc/issue.ww 0644 wwctl overlay chmod issue /etc/issue.ww 0644
Distribution Overlays Distribution Overlays
===================== =====================