52 lines
1.4 KiB
Go
52 lines
1.4 KiB
Go
package templater
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"path/filepath"
|
|
)
|
|
|
|
type Service struct {
|
|
EnabledBy SimpleCondition `json:"EnabledBy"`
|
|
ConfigFiles []ConfigFile `json:"ConfigFiles"`
|
|
Vars map[string]interface{} `json:"Vars"`
|
|
Daemon SystemService `json:"Daemon"`
|
|
Users map[string]SystemUser `json:"Users"`
|
|
}
|
|
|
|
func (s *Service) Manage(templateDir string) error {
|
|
err := processConfigFiles(s.ConfigFiles, s.Vars, templateDir)
|
|
if err != nil {
|
|
return fmt.Errorf("ProcessingTemplatesFailed with error: %v", err)
|
|
}
|
|
|
|
err = s.Daemon.Manage()
|
|
if err != nil {
|
|
return fmt.Errorf("Error managing service daemons: %v", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func processConfigFiles(tpls []ConfigFile, variables map[string]interface{}, templateDir string) error {
|
|
values, err := json.Marshal(variables)
|
|
if err != nil {
|
|
return fmt.Errorf("Error unmarshaling values on template process; %v", err)
|
|
}
|
|
|
|
for _, tpl := range tpls {
|
|
fileExt := filepath.Ext(tpl.Source)
|
|
if fileExt == ".hcl" {
|
|
tpl.TemplateType = "hcl"
|
|
} else if fileExt == ".tpl" {
|
|
tpl.TemplateType = "go"
|
|
} else {
|
|
return fmt.Errorf("Unsupported file extention %s, templates extensions have to be '.hcl' or '.tpl'", fileExt)
|
|
}
|
|
if err := tpl.Generate("/tmp/test", templateDir, values); err != nil {
|
|
return fmt.Errorf("Template %s generation failed with error %v", tpl.Source, err)
|
|
}
|
|
|
|
}
|
|
return nil
|
|
}
|