fix(bootstraper): adding global variables support

Now we can declare "global" variables, in the "Globals" section the  the
configuration, this variables will be merged into the "Service"
variables and used in the templates. This how we share variables between
services.
This commit is contained in:
2022-07-01 14:56:41 +02:00
parent 9bb4e93d65
commit e9f8ff2506
8 changed files with 1447 additions and 30 deletions

View File

@ -14,8 +14,9 @@ var CacheFilePath = "/var/cache/templater.db"
type TemplaterConfig struct {
Name string `json:"Name"`
TemplateDirectory string `json:"TemplateDirectory"`
RootDirectory string `json:"RootDirectory"`
Services map[string]Service `json:"Services"`
GlobalService Service `json:"Global"`
GlobalService Service `json:"Globals"`
}
func (tc *TemplaterConfig) loadCache() error {
@ -33,7 +34,8 @@ func (tc *TemplaterConfig) loadCache() error {
return nil
}
func (tc *TemplaterConfig) New(confpath string, templateDir string) error {
// Create new configuration "object"
func (tc *TemplaterConfig) New(confpath string, templateDir string, rootDir string) error {
// Load stored globals if needed
lerr := tc.loadCache()
if lerr != nil {
@ -72,17 +74,28 @@ func (tc *TemplaterConfig) New(confpath string, templateDir string) error {
}
tc.TemplateDirectory = templateDir
tc.RootDirectory = rootDir
return nil
}
func (tc *TemplaterConfig) ManageServices(rootDir string) error {
// Process the services contained in the configuration "object"
func (tc *TemplaterConfig) ManageServices() error {
// Get global vars to add on each service
gbls := tc.GlobalService.Vars
for name, svr := range tc.Services {
log.Printf("*** Working on service %s", name)
if err := svr.Manage(tc.TemplateDirectory, rootDir); err != nil {
err := mergo.Merge(&svr.Vars, gbls)
if err != nil {
return err
}
log.Printf("*** Service %s processed", name)
log.Printf("=== Working on service %s", name)
if err := svr.Manage(tc.TemplateDirectory, tc.RootDirectory); err != nil {
return err
}
log.Printf("=== Service %s processed", name)
log.Printf("")
}
return nil
}