forked from jwilder/dockerize
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtemplate.go
135 lines (118 loc) · 3.01 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
135
package main
import (
"io/ioutil"
"log"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"syscall"
"text/template"
"github.com/Masterminds/sprig/v3"
"github.com/alessio/shellescape"
"github.com/jwilder/gojq"
)
func exists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
func parseUrl(rawurl string) *url.URL {
u, err := url.Parse(rawurl)
if err != nil {
log.Fatalf("unable to parse url %s: %s", rawurl, err)
}
return u
}
func isTrue(s string) bool {
b, err := strconv.ParseBool(strings.ToLower(s))
if err == nil {
return b
}
return false
}
func jsonQuery(jsonObj string, query string) (interface{}, error) {
parser, err := gojq.NewStringQuery(jsonObj)
if err != nil {
return "", err
}
res, err := parser.Query(query)
if err != nil {
return "", err
}
return res, nil
}
func generateFile(templatePath, destPath string) bool {
tmpl := template.New(filepath.Base(templatePath)).Funcs(sprig.TxtFuncMap()).Funcs(template.FuncMap{
"exists": exists,
"parseUrl": parseUrl,
"isTrue": isTrue,
"jsonQuery": jsonQuery,
"shellquote": shellescape.Quote,
})
if len(delims) > 0 {
tmpl = tmpl.Delims(delims[0], delims[1])
}
tmpl, err := tmpl.ParseFiles(templatePath)
if err != nil {
log.Fatalf("unable to parse template: %s", err)
}
// Don't overwrite destination file if it exists and no-overwrite flag passed
if _, err := os.Stat(destPath); err == nil && noOverwriteFlag {
return false
}
dest := os.Stdout
if destPath != "" {
dest, err = os.Create(destPath)
if err != nil {
log.Fatalf("unable to create %s", err)
}
defer dest.Close()
}
err = tmpl.ExecuteTemplate(dest, filepath.Base(templatePath), &Context{})
if err != nil {
log.Fatalf("template error: %s\n", err)
}
if fi, err := os.Stat(destPath); err == nil {
if err := dest.Chmod(fi.Mode()); err != nil {
log.Fatalf("unable to chmod temp file: %s\n", err)
}
if err := dest.Chown(int(fi.Sys().(*syscall.Stat_t).Uid), int(fi.Sys().(*syscall.Stat_t).Gid)); err != nil {
log.Fatalf("unable to chown temp file: %s\n", err)
}
}
return true
}
func generateDir(templateDir, destDir string) bool {
if destDir != "" {
fiDest, err := os.Stat(destDir)
if err != nil {
log.Fatalf("unable to stat %s, error: %s", destDir, err)
}
if !fiDest.IsDir() {
log.Fatalf("if template is a directory, dest must also be a directory (or stdout)")
}
}
files, err := ioutil.ReadDir(templateDir)
if err != nil {
log.Fatalf("bad directory: %s, error: %s", templateDir, err)
}
for _, file := range files {
if file.IsDir() {
nextDestination := filepath.Join(destDir, file.Name())
os.Mkdir(nextDestination, file.Mode())
generateDir(filepath.Join(templateDir, file.Name()), nextDestination)
} else if destDir == "" {
generateFile(filepath.Join(templateDir, file.Name()), "")
} else {
generateFile(filepath.Join(templateDir, file.Name()), filepath.Join(destDir, file.Name()))
}
}
return true
}