2022-03-24 00:49:23 +01:00
|
|
|
package templater
|
2022-03-10 17:31:29 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-06-15 17:29:45 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
2022-03-10 17:31:29 +01:00
|
|
|
"os"
|
2022-03-11 10:39:07 +01:00
|
|
|
|
2022-06-15 17:29:45 +02:00
|
|
|
"github.com/imdario/mergo"
|
2022-03-24 00:49:23 +01:00
|
|
|
)
|
2022-03-11 10:39:07 +01:00
|
|
|
|
2022-06-15 17:29:45 +02:00
|
|
|
var CacheFilePath = "/var/cache/templater.db"
|
2022-03-10 17:31:29 +01:00
|
|
|
|
2022-06-15 17:29:45 +02:00
|
|
|
type TemplaterConfig struct {
|
|
|
|
Name string `json:"Name"`
|
|
|
|
TemplateDirectory string `json:"TemplateDirectory"`
|
2022-07-01 14:56:41 +02:00
|
|
|
RootDirectory string `json:"RootDirectory"`
|
2022-06-15 17:29:45 +02:00
|
|
|
Services map[string]Service `json:"Services"`
|
2022-07-01 14:56:41 +02:00
|
|
|
GlobalService Service `json:"Globals"`
|
2022-03-10 17:31:29 +01:00
|
|
|
}
|
|
|
|
|
2022-06-15 17:29:45 +02:00
|
|
|
func (tc *TemplaterConfig) loadCache() error {
|
|
|
|
// Load globals from cache
|
|
|
|
var cache Service
|
|
|
|
err := Load(CacheFilePath, &cache)
|
2022-03-11 10:39:07 +01:00
|
|
|
if err != nil {
|
2022-06-15 17:29:45 +02:00
|
|
|
fmt.Printf("Warning: No globals to load\n")
|
2022-03-11 10:39:07 +01:00
|
|
|
}
|
2022-03-10 17:31:29 +01:00
|
|
|
|
2022-06-15 17:29:45 +02:00
|
|
|
err = mergo.Merge(&tc.GlobalService, cache)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-03-10 17:31:29 +01:00
|
|
|
}
|
2022-06-15 17:29:45 +02:00
|
|
|
return nil
|
|
|
|
}
|
2022-03-10 17:31:29 +01:00
|
|
|
|
2022-07-01 14:56:41 +02:00
|
|
|
// Create new configuration "object"
|
|
|
|
func (tc *TemplaterConfig) New(confpath string, templateDir string, rootDir string) error {
|
2022-06-15 17:29:45 +02:00
|
|
|
// Load stored globals if needed
|
|
|
|
lerr := tc.loadCache()
|
|
|
|
if lerr != nil {
|
|
|
|
return lerr
|
|
|
|
}
|
|
|
|
// Check if the configuration path is a Directory or a file
|
|
|
|
fileInfo, err := os.Stat(confpath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-03-10 17:31:29 +01:00
|
|
|
}
|
|
|
|
|
2022-06-15 17:29:45 +02:00
|
|
|
if fileInfo.IsDir() {
|
|
|
|
// The conf path is a directory we load all the files and merge data
|
|
|
|
files, err := ioutil.ReadDir(confpath)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Templater configuration load failed with error: %v", err)
|
|
|
|
}
|
|
|
|
for _, file := range files {
|
|
|
|
fname := fmt.Sprintf("%s/%s", confpath, file.Name())
|
|
|
|
var ntc TemplaterConfig
|
|
|
|
err := Load(fname, &ntc)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Templater configuration load failed with error: %v", err)
|
|
|
|
}
|
|
|
|
err = mergo.Merge(tc, ntc)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Templater configuration load failed with error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// The conf path is a file we only load this file (of course)
|
|
|
|
err = Load(confpath, tc)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Confiuration read failed with error: %v", err)
|
2022-03-10 17:31:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-15 17:29:45 +02:00
|
|
|
tc.TemplateDirectory = templateDir
|
2022-07-01 14:56:41 +02:00
|
|
|
tc.RootDirectory = rootDir
|
2022-06-15 17:29:45 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2022-03-10 17:31:29 +01:00
|
|
|
|
2022-07-01 14:56:41 +02:00
|
|
|
// 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
|
|
|
|
|
2022-06-15 17:29:45 +02:00
|
|
|
for name, svr := range tc.Services {
|
2022-07-01 14:56:41 +02:00
|
|
|
err := mergo.Merge(&svr.Vars, gbls)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("=== Working on service %s", name)
|
|
|
|
if err := svr.Manage(tc.TemplateDirectory, tc.RootDirectory); err != nil {
|
2022-06-15 17:29:45 +02:00
|
|
|
return err
|
|
|
|
}
|
2022-07-01 14:56:41 +02:00
|
|
|
log.Printf("=== Service %s processed", name)
|
|
|
|
log.Printf("")
|
2022-06-15 17:29:45 +02:00
|
|
|
}
|
|
|
|
return nil
|
2022-03-11 10:39:07 +01:00
|
|
|
}
|