From: Simon Ruderich Date: Tue, 20 Jan 2026 19:11:51 +0000 (+0100) Subject: config: add .GroupHosts to templates X-Git-Url: https://ruderich.org/simon/gitweb/?a=commitdiff_plain;h=baafb6e5365d6260a40c56a935ad814e723e4716;p=safcm%2Fsafcm.git config: add .GroupHosts to templates Permits iterating over all hosts of a given group. --- diff --git a/cmd/safcm/config/templates.go b/cmd/safcm/config/templates.go index 059d89b..dbc8cdf 100644 --- a/cmd/safcm/config/templates.go +++ b/cmd/safcm/config/templates.go @@ -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: