-
Notifications
You must be signed in to change notification settings - Fork 14
/
template.go
134 lines (115 loc) · 2.6 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"errors"
"fmt"
"os"
"path/filepath"
"text/template"
// "io/ioutil"
"bytes"
"io/ioutil"
"log"
)
type OptionalString struct {
ptr *string
}
func (s OptionalString) String() string {
if s.ptr == nil {
return ""
}
return *s.ptr
}
var alwaysThere string = "always_there"
func Env(key string) OptionalString {
// We need to simulate always existing key for testing, but we do not want to pollute env
if key == "ALWAYS_THERE" {
return OptionalString{&alwaysThere}
}
value, ok := os.LookupEnv(key)
if !ok {
return OptionalString{nil}
}
return OptionalString{&value}
}
func Default(args ...interface{}) (string, error) {
for _, arg := range args {
if arg == nil {
continue
}
switch v := arg.(type) {
case string:
return v, nil
case *string:
if v != nil {
return *v, nil
}
case OptionalString:
if v.ptr != nil {
return *v.ptr, nil
}
default:
return "", fmt.Errorf("Default: unsupported type '%T'!", v)
}
}
return "", errors.New("Default: all arguments are nil!")
}
func Require(arg interface{}) (string, error) {
if arg == nil {
return "", errors.New("Required argument is missing!")
}
switch v := arg.(type) {
case string:
return v, nil
case *string:
if v != nil {
return *v, nil
}
case OptionalString:
if v.ptr != nil {
return *v.ptr, nil
}
}
return "", fmt.Errorf("Requires: unsupported type '%T'!", v)
}
var funcMap = template.FuncMap{
"env": Env,
"default": Default,
"require": Require,
}
func generateTemplate(source, name string) (string, error) {
var t *template.Template
var err error
if t, err = template.New(name).Funcs(funcMap).Parse(source); err != nil {
return "", err
}
var buffer bytes.Buffer
if err = t.Execute(&buffer, nil); err != nil {
return "", err
}
return buffer.String(), nil
}
func generateFile(templatePath, destinationPath string, debugTemplates bool) error {
if !filepath.IsAbs(templatePath) {
return fmt.Errorf("Template path '%s' is not absolute!", templatePath)
}
if !filepath.IsAbs(destinationPath) {
return fmt.Errorf("Destination path '%s' is not absolute!", destinationPath)
}
var slice []byte
var err error
if slice, err = ioutil.ReadFile(templatePath); err != nil {
return err
}
s := string(slice)
result, err := generateTemplate(s, filepath.Base(templatePath))
if err != nil {
return err
}
if debugTemplates {
log.Printf("Printing parsed template to stdout. (It's delimited by 2 character sequence of '\\x00\\n'.)\n%s\x00\n", result)
}
if err = ioutil.WriteFile(destinationPath, []byte(result), 0664); err != nil {
return err
}
return nil
}