-
Notifications
You must be signed in to change notification settings - Fork 63
/
dockerfile.go
67 lines (55 loc) · 1.85 KB
/
dockerfile.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
package example
import (
"fmt"
"github.com/Azure/draft/pkg/handlers"
"github.com/Azure/draft/pkg/templatewriter/writers"
)
// WriteDockerfileExample shows how to set up a fileWriter and generate a fileMap using WriteDockerfile
func WriteDockerfileExample() error {
// Create a file map
fileMap := make(map[string][]byte)
// Create a template writer that writes to the file map
w := writers.FileMapWriter{
FileMap: fileMap,
}
// Select the language to generate the Dockerfile for (must correspond to a directory in the template/dockerfiles directory)
generationLanguage := "go"
// Create a map of inputs to the template (must correspond to the inputs in the template/dockerfiles/<language>/draft.yaml files)
templateVars := map[string]string{
"PORT": "8080",
"APPNAME": "example-app",
"SERVICEPORT": "8080",
"NAMESPACE": "example-namespace",
"IMAGENAME": "example-image",
"IMAGETAG": "latest",
"GENERATORLABEL": "draft",
}
// Set the output path for the Dockerfile
outputPath := "./"
// Get the dockerfile template
d, err := handlers.GetTemplate(fmt.Sprintf("dockerfile-%s", generationLanguage), "", outputPath, &w)
if err != nil {
return fmt.Errorf("failed to get template: %e", err)
}
if d == nil {
return fmt.Errorf("template is nil")
}
// Set the variable values within the template
for k, v := range templateVars {
d.Config.SetVariable(k, v)
}
// Generate the dockerfile files
err = d.Generate()
if err != nil {
return fmt.Errorf("failed to generate dockerfile: %e", err)
}
// Read written files from the file map
fmt.Printf("Files written in WriteDockerfileExample:\n")
for filePath, fileContents := range fileMap {
if fileContents == nil {
return fmt.Errorf("file contents for %s is nil", filePath)
}
fmt.Printf(" %s\n", filePath) // Print the file path
}
return nil
}