-
Notifications
You must be signed in to change notification settings - Fork 64
/
deployment.go
67 lines (55 loc) · 1.88 KB
/
deployment.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"
)
// WriteDeploymentFilesExample shows how to set up a fileWriter and generate a fileMap using WriteDeploymentFiles
func WriteDeploymentFilesExample() 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 deployment type to generate the files for (must correspond to a directory in the template/deployments directory)
deploymentTemplateType := "deployment-manifests"
// Create a map of of inputs to the template (must correspond to the inputs in the template/deployments/<deploymentType>/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 deployment files
outputPath := "./"
// Get the deployment template
d, err := handlers.GetTemplate(deploymentTemplateType, "", 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 deployment files
err = d.Generate()
if err != nil {
return fmt.Errorf("failed to generate manifest: %e", err)
}
// Read written files from the file map
fmt.Printf("Files written in WriteDeploymentFilesExample:\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
}