-
Notifications
You must be signed in to change notification settings - Fork 1
/
template.go
52 lines (43 loc) · 1.44 KB
/
template.go
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
package templating
import (
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
"text/template"
filesystem "github.com/kilianpaquier/filesystem/pkg"
)
// Execute runs Execute function from input tmpl with input data and write result to given dest file.
func Execute(tmpl *template.Template, data any, dest string) error {
// create destination directory only if one file would be generated
if err := os.MkdirAll(filepath.Dir(dest), filesystem.RwxRxRxRx); err != nil && !os.IsExist(err) {
return fmt.Errorf("create directory: %w", err)
}
var result strings.Builder
if err := tmpl.Execute(&result, data); err != nil {
return fmt.Errorf("template execution: %w", err)
}
return WriteFile(dest, []byte(result.String()))
}
// WriteFile removes dest and rewrites it with input content.
// It's done as is to ensure file rights are recalculated.
func WriteFile(dest string, content []byte) error {
// remove file before rewritting it (in case rights changed)
if err := os.Remove(dest); err != nil && !errors.Is(err, fs.ErrNotExist) {
return fmt.Errorf("delete file: %w", err)
}
// write new file content
if err := os.WriteFile(dest, content, GetRights(dest)); err != nil {
return fmt.Errorf("write file: %w", err)
}
return nil
}
// GetRights returns the appropriate file mode according to input file path.
func GetRights(dest string) os.FileMode {
if strings.HasSuffix(dest, ".sh") {
return filesystem.RwxRxRxRx
}
return filesystem.RwRR
}