-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder.go
84 lines (71 loc) · 1.83 KB
/
builder.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
package main
import (
"bytes"
"fmt"
"text/template"
"io/ioutil"
"os"
)
type FileGenerator interface {
ReadPrelude() string
ReadEpilogue(packages []string) string
ReadBase() string
ReadExtras(packageName string) string
Write(template string) (ret int, err error)
}
type Dockerfile struct {
bytes []byte
}
func ParsedSnippet(path string, data interface{}) string {
text := new(bytes.Buffer)
t := template.New("prelude")
contents,err := ioutil.ReadFile(path)
if err != nil {
fmt.Println(err)
}
t = template.Must(t.Parse(string(contents)))
t.Execute(text, data)
return text.String()
}
func FileExists(path string) bool {
if _, err := os.Stat(path); err != nil {
if os.IsNotExist(err) {
return false
}
}
return true
}
func (d Dockerfile) ReadPrelude() string {
return ParsedSnippet("templates/prelude.tmpl", nil)
}
func (d Dockerfile) ReadEpilogue(packages []string) string {
return ParsedSnippet("templates/epilogue.tmpl", packages)
}
func (d Dockerfile) ReadExtras(packageName string) string {
path := "templates/extras/" + packageName + ".tmpl"
if FileExists(path) {
return ParsedSnippet(path, nil)
}
return ""
}
func (d Dockerfile) ReadBase() string {
return ParsedSnippet("templates/base.tmpl", nil)
}
func (d *Dockerfile) Write(template string) (ret int,err error) {
d.bytes = []byte(template)
return len(d.bytes), nil
}
func MakeDockerfile(packages []string, g FileGenerator) {
var final bytes.Buffer
final.WriteString(fmt.Sprintln(g.ReadPrelude()))
final.WriteString(fmt.Sprintln(g.ReadBase()))
for _, p := range packages {
line := fmt.Sprintln(g.ReadExtras(p))
final.WriteString(line)
}
final.WriteString(fmt.Sprintln(g.ReadEpilogue(packages)))
g.Write(final.String())
}
func (d Dockerfile) Contents() string {
return string(d.bytes[:len(d.bytes)])
}