This repository has been archived by the owner on Mar 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
141 lines (122 loc) · 3.6 KB
/
main.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
136
137
138
139
140
141
package main
import (
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"runtime"
"strings"
"text/template"
)
const (
ConfigurationFilepath = "configuration.json"
DockerTemplateFilepath = "Dockerfile.gotmpl"
)
var (
_, b, _, _ = runtime.Caller(0)
basepath = filepath.Dir(b)
)
// Platforms struct which contains
// an array of platforms.
type Platforms struct {
Poetry12 Platform `json:"1.2"`
Poetry11 Platform `json:"1.1"`
}
func (p *Platforms) GetPlatforms() []Platform {
return []Platform{
p.Poetry12,
p.Poetry11,
}
}
// Platform struct which contains a name
// a type and a list of social links
type Platform struct {
PythonVersions []string `json:"pythons"`
ImageVariants []string `json:"variants"`
Version string `json:"version"`
}
type pythonImageVariant struct {
PythonVersion string
ImageVariant string
}
func (piv *pythonImageVariant) GetDockerfileNotation() string {
return fmt.Sprintf("%s-%s", piv.PythonVersion, piv.ImageVariant)
}
func openConfiguration(filepath string) (*os.File, error) {
jsonFile, err := os.Open(filepath)
if err != nil {
return nil, err
}
return jsonFile, nil
}
func unmarshalConfiguration(reader io.Reader) (*Platforms, error) {
// read our opened jsonFile as a byte array.
byteValue, _ := io.ReadAll(reader)
// we unmarshal our byteArray which contains our
// jsonFile's content into 'platforms' which we defined above
var platforms Platforms
err := json.Unmarshal(byteValue, &platforms)
if err != nil {
return nil, err
}
return &platforms, nil
}
func getImageNamesFrom(pythonVersions []string, imageVariants []string) []pythonImageVariant {
pairs := make([]pythonImageVariant, 0, len(pythonVersions)*len(imageVariants))
for _, pv := range pythonVersions {
for _, iv := range imageVariants {
pair := pythonImageVariant{
PythonVersion: pv,
ImageVariant: iv,
}
pairs = append(pairs, pair)
}
}
return pairs
}
func getWritingPathFrom(platform Platform, pair pythonImageVariant) string {
patchIndex := strings.LastIndex(platform.Version, ".")
return fmt.Sprintf("%s/%s/%s/%s/Dockerfile", basepath, platform.Version[:patchIndex], pair.PythonVersion, pair.ImageVariant)
}
func generateDockerfilesFrom(tmpl *template.Template, platform Platform, pairs []pythonImageVariant) {
for _, image := range pairs {
path := getWritingPathFrom(platform, image)
f, err := os.Create(path)
if err != nil {
fmt.Printf("an error occured while trying to create Dockerfile for platform [%s] and image [%s]",
platform.Version, image)
}
err = tmpl.Execute(f, map[string]string{"FromVersion": image.GetDockerfileNotation(), "PoetryVersion": platform.Version, "ImageVariant": image.ImageVariant})
if err != nil {
fmt.Printf("an error occured while trying to generate Dockerfile content for platform [%s] and image [%s].\ngot: %s\n",
platform.Version, image, err)
}
if err = f.Close(); err != nil {
fmt.Printf("couldn't close file for platform [%s] and image [%s]",
platform.Version, image)
}
}
}
func generateDockerfilesFor(tmpl *template.Template, platforms *Platforms) {
for _, platform := range platforms.GetPlatforms() {
images := getImageNamesFrom(platform.PythonVersions, platform.ImageVariants)
generateDockerfilesFrom(tmpl, platform, images)
}
}
func main() {
confFile, err := openConfiguration(ConfigurationFilepath)
defer confFile.Close()
if err != nil {
panic(err)
}
platforms, err := unmarshalConfiguration(confFile)
if err != nil {
panic(err)
}
tmpl, err := template.New(DockerTemplateFilepath).ParseFiles(DockerTemplateFilepath)
if err != nil {
panic(err)
}
generateDockerfilesFor(tmpl, platforms)
}