package templater import ( "fmt" "io/ioutil" "os" "github.com/imdario/mergo" ) var CacheFilePath = "/var/cache/templater.db" type TemplaterConfig struct { Name string `json:"Name"` TemplateDirectory string `json:"TemplateDirectory"` Services map[string]Service `json:"Services"` GlobalService Service `json:"Global"` } func (tc *TemplaterConfig) loadCache() error { // Load globals from cache var cache Service err := Load(CacheFilePath, &cache) if err != nil { fmt.Printf("Warning: No globals to load\n") } err = mergo.Merge(&tc.GlobalService, cache) if err != nil { return err } return nil } 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 } 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) } } tc.TemplateDirectory = templateDir return nil } func (tc *TemplaterConfig) ManageServices() error { for _, svr := range tc.Services { if err := svr.Manage(tc.TemplateDirectory); err != nil { return err } } return nil }