-
Notifications
You must be signed in to change notification settings - Fork 52
/
util.go
59 lines (51 loc) · 1.43 KB
/
util.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
53
54
55
56
57
58
59
package util
import (
"fmt"
"log"
"os"
"path"
"strings"
"text/template"
)
func CreateDirIfDoesNotExist(path string) error {
if _, err := os.Stat(path); os.IsNotExist(err) {
err = os.MkdirAll(path, os.ModePerm)
return err
}
return nil
}
var FuncMap = template.FuncMap{
"Title": strings.Title,
}
func createTemplatedFile(fullFilePath string, template *template.Template, data interface{}) {
f, err := os.Create(fullFilePath)
if err != nil {
log.Printf("Error creating file: %v", err)
}
err = template.Execute(f, data)
if err != nil {
log.Printf("Error templating: %v", err)
}
}
func TemplateFileAndOverwrite(fileDir string, fileName string, template *template.Template, data interface{}) {
fullFilePath := fmt.Sprintf("%v/%v", fileDir, fileName)
err := os.MkdirAll(fileDir, os.ModePerm)
if err != nil {
log.Printf("Error creating directory %v: %v", fullFilePath, err)
}
createTemplatedFile(fullFilePath, template, data)
}
func TemplateFileIfDoesNotExist(fileDir string, fileName string, template *template.Template, data interface{}) {
fullFilePath := path.Join(fileDir, fileName)
if _, err := os.Stat(fullFilePath); os.IsNotExist(err) {
if fileDir != "" {
err := CreateDirIfDoesNotExist(fileDir)
if err != nil {
log.Printf("Error creating directory %v: %v", fullFilePath, err)
}
}
createTemplatedFile(fullFilePath, template, data)
} else {
log.Printf("%v already exists. skipping.", fullFilePath)
}
}