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"
|
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"`
|
|
|
|
Services map[string]Service `json:"Services"`
|
|
|
|
GlobalService Service `json:"Global"`
|
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-06-15 17:29:45 +02:00
|
|
|
func (tc *TemplaterConfig) New(confpath string, templateDir string) error {
|
|
|
|
// 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
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2022-03-10 17:31:29 +01:00
|
|
|
|
2022-06-30 10:10:26 +02:00
|
|
|
func (tc *TemplaterConfig) ManageServices(rootDir string) error {
|
2022-06-15 17:29:45 +02:00
|
|
|
for _, svr := range tc.Services {
|
2022-06-30 10:10:26 +02:00
|
|
|
if err := svr.Manage(tc.TemplateDirectory, rootDir); err != nil {
|
2022-06-15 17:29:45 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2022-03-11 10:39:07 +01:00
|
|
|
}
|