templatefile/pkg/configs/files.go

47 lines
1.2 KiB
Go
Raw Normal View History

package configs
import (
"fmt"
"os"
"path/filepath"
"strconv"
"forge.cadoles.com/pcaseiro/templatefile/pkg/templater"
)
type ConfigFile struct {
Destination string `form:"destination" json:"destination"`
Source string `form:"source" json:"source"`
Mode string `form:"mod" json:"mode"`
Owner string `json:"owner"`
Group string `json:"group"`
}
func (cf *ConfigFile) Generate(root string, templateDir string, values []byte) error {
dest := filepath.Join(root, cf.Destination)
source := filepath.Join(templateDir, cf.Source)
intMod, err := strconv.ParseInt(cf.Mode, 8, 64)
if err != nil {
return (err)
}
fmt.Printf("Processing %s\n", source)
fileExtension := filepath.Ext(source)
if fileExtension == ".hcl" {
res, err := templater.ProcessHCLTemplate(source, values)
if err != nil {
return fmt.Errorf("Process templates failed with error: %v", err)
}
dirname := filepath.Dir(dest)
err = os.MkdirAll(dirname, os.FileMode(int(0700)))
if err != nil {
return fmt.Errorf("Process templates failed with error: %v", err)
}
err = os.WriteFile(dest, []byte(res), os.FileMode(intMod))
if err != nil {
return fmt.Errorf("Process templates failed with error: %v", err)
}
}
return nil
}