From bb008a385667ccf8eaacbf7cd51635f8fd65b8a8 Mon Sep 17 00:00:00 2001 From: TJ Hoplock Date: Mon, 8 Jul 2024 12:49:56 -0400 Subject: [PATCH] feat(inv): add GetTemplatesForHost and self to inventory.Store interface --- internal/inventory/inventory.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/internal/inventory/inventory.go b/internal/inventory/inventory.go index 9506d44..7e7a289 100644 --- a/internal/inventory/inventory.go +++ b/internal/inventory/inventory.go @@ -130,6 +130,7 @@ type Store interface { GetRolesForHost(host string) []Role GetGroupsForHost(host string) []Group GetVariablesForHost(host string) []string + GetTemplatesForHost(host string) []string // Self checks GetDirectivesForSelf() []Directive @@ -137,6 +138,7 @@ type Store interface { GetRolesForSelf() []Role GetGroupsForSelf() []Group GetVariablesForSelf() []string + GetTemplatesForSelf() []string } // NewInventory parses the files/directories in the provided path @@ -463,6 +465,36 @@ func (i *Inventory) GetVariablesForSelf() []string { return i.GetVariablesForHost(i.hostname) } +// GetTemplatesForHost returns slice of strings, containing the paths of any +// templates files found for this host. All role templates are provided first, +// then group templates second, with host-specific templates provided last (to +// allow for overriding default group variable data). +func (i *Inventory) GetTemplatesForHost(host string) []string { + var tmpls []string + + for _, role := range i.GetRolesForHost(host) { + tmpls = append(tmpls, role.templateFiles...) + } + + for _, group := range i.GetGroupsForHost(host) { + tmpls = append(tmpls, group.templateFiles...) + } + + if h, found := i.GetHost(host); found { + tmpls = append(tmpls, h.templateFiles...) + } + + return tmpls +} + +// GetTemplatesForSelf returns slice of strings, containing the paths of any +// templates files found for the running host. All role templates are provided +// first, then group templates second, with host-specific templates provided +// last (to allow for overriding default group variable data). +func (i *Inventory) GetTemplatesForSelf() []string { + return i.GetTemplatesForHost(i.hostname) +} + func filterDuplicateModules(input []Module) []Module { modMap := make(map[string]Module)