-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhtml_templates.go
More file actions
53 lines (44 loc) · 1.06 KB
/
html_templates.go
File metadata and controls
53 lines (44 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main
import (
"fmt"
"os"
"path/filepath"
"text/template"
"github.com/aljo242/chef"
"github.com/rs/zerolog/log"
)
type htmlTemplateInfo struct {
Host string
// TODO add more
}
// ExecuteTemplateHTML is a util func for executing an html template
// at path and saving the new file to newPath
func ExecuteTemplateHTML(cfg chef.ServerConfig, path, newPath string) error {
filePath := filepath.Clean(newPath)
newFile, err := os.Create(filePath)
if err != nil {
return fmt.Errorf("error creating file %v : %w", newPath, err)
}
defer func() {
err := newFile.Close()
if err != nil {
log.Error().Err(err).Str("filename", filePath).Msg("error closing the file")
}
}()
tpl, err := template.ParseFiles(path)
if err != nil {
return fmt.Errorf("error creating template : %w", err)
}
var httpPrefix string
if cfg.HTTPS {
httpPrefix = "https://"
} else {
httpPrefix = "http://"
}
p := htmlTemplateInfo{httpPrefix + cfg.Host}
err = tpl.Execute(newFile, p)
if err != nil {
return fmt.Errorf("error executing template : %w", err)
}
return nil
}