]> ruderich.org/simon Gitweb - safcm/safcm.git/commitdiff
config: add .GroupHosts to templates
authorSimon Ruderich <simon.ruderich@fau.de>
Tue, 20 Jan 2026 19:11:51 +0000 (20:11 +0100)
committerSimon Ruderich <simon.ruderich@fau.de>
Tue, 20 Jan 2026 19:13:12 +0000 (20:13 +0100)
Permits iterating over all hosts of a given group.

cmd/safcm/config/templates.go

index 059d89bb3dd69df4916f31cb9ba142eec8e64f02..dbc8cdfd49c977f441dfb71b2e17f21bde1f7087 100644 (file)
@@ -10,6 +10,7 @@ import (
        "fmt"
        "os"
        "path/filepath"
+       "slices"
        "strings"
        "text/template"
 
@@ -73,6 +74,7 @@ func LoadTemplates(group string, files map[string]*safcm.File,
                        groups:    groupsMap,
                        allHosts:  allHostsMap,
                        allGroups: allGroupsMap,
+                       allGroupHosts: allGroups,
                })
                if err != nil {
                        return fmt.Errorf("%s: %v", path, err)
@@ -90,6 +92,7 @@ type templateArgs struct {
        groups    map[string]bool
        allHosts  map[string]bool
        allGroups map[string]bool
+       allGroupHosts map[string][]string
 }
 
 // TODO: extend data passed to template
@@ -102,6 +105,18 @@ func (t *templateArgs) IsHost(host string) bool {
        return t.Host == host
 }
 func (t *templateArgs) InGroup(group string) bool {
+       t.groupMustExist(group)
+       return t.groups[group]
+}
+
+func (t *templateArgs) GroupHosts(group string) []string {
+       t.groupMustExist(group)
+       x := t.allGroupHosts[group]
+       slices.Sort(x)
+       return x
+}
+
+func (t *templateArgs) groupMustExist(group string) {
        // Don't permit invalid groups to detect typos; detected groups cannot be
        // checked
        if group != GroupAll &&
@@ -110,7 +125,6 @@ func (t *templateArgs) InGroup(group string) bool {
                !strings.HasPrefix(group, GroupDetectedPrefix) {
                panic(fmt.Sprintf("group %q does not exist", group))
        }
-       return t.groups[group]
 }
 
 // vi: set noet ts=4 sw=4 sts=4: