@@ -17,35 +17,92 @@ import (
17
17
//go:embed templates/*.tmpl
18
18
var templates embed.FS
19
19
20
- func buildTemplate ( input ContainerInput ) ( file , error ) {
21
- tpl , err := template . ParseFS ( templates , "templates/*.tmpl" )
22
- if err != nil {
23
- return file {}, xerrors . Errorf ( "parse template: %w" , err )
24
- }
20
+ type TemplateInput struct {
21
+ TemplateName string
22
+
23
+ Kubernetes KubeOptions
24
+ }
25
25
26
- tpl = tpl .Funcs (template.FuncMap {
26
+ type ReadmeInput struct {
27
+ Platform string
28
+ Name string
29
+ Description string
30
+ Tags []string
31
+ Icon string
32
+ }
33
+
34
+ func buildTemplate (input TemplateInput ) ([]byte , error ) {
35
+ tpl , err := template .New ("" ).Funcs (template.FuncMap {
27
36
"join" : strings .Join ,
28
37
"contains" : slice .Contains [string ],
29
- })
38
+ "quote" : func (in string ) string { return "\" " + in + "\" " },
39
+ }).ParseFS (templates , "templates/*.tmpl" )
40
+ if err != nil {
41
+ return nil , xerrors .Errorf ("parse template: %w" , err )
42
+ }
43
+
44
+ var out bytes.Buffer
45
+ tarWriter := tar .NewWriter (& out )
30
46
31
- var buf bytes.Buffer
32
- // TODO: Change the name based on the input
33
- err = tpl .ExecuteTemplate (& buf , "docker" , input )
47
+ tf , readme , err := buildKube (tpl , input .Kubernetes )
34
48
if err != nil {
35
- return file {}, xerrors . Errorf ( "execute template: %w" , err )
49
+ return nil , err
36
50
}
37
51
38
- return file {
39
- name : "main.tf" ,
40
- content : buf .Bytes (),
41
- }, nil
52
+ md , err := buildReadme (tpl , readme )
53
+ if err != nil {
54
+ return nil , err
55
+ }
56
+
57
+ err = writeFiles (tarWriter , tf , md )
58
+ if err != nil {
59
+ return nil , xerrors .Errorf ("write tar files: %w" , err )
60
+ }
61
+
62
+ err = tarWriter .Close ()
63
+ if err != nil {
64
+ return nil , xerrors .Errorf ("close tar writer: %w" , err )
65
+ }
66
+ return out .Bytes (), nil
42
67
}
43
68
44
69
type file struct {
45
70
name string
46
71
content []byte
47
72
}
48
73
74
+ func buildKube (tpl * template.Template , input KubeOptions ) (file , ReadmeInput , error ) {
75
+ var out bytes.Buffer
76
+ err := tpl .ExecuteTemplate (& out , "kubernetes" , input )
77
+ if err != nil {
78
+ return file {}, ReadmeInput {}, xerrors .Errorf ("execute kube template: %w" , err )
79
+ }
80
+
81
+ return file {
82
+ name : "main.tf" ,
83
+ content : out .Bytes (),
84
+ }, ReadmeInput {
85
+ Platform : "Kubernetes" ,
86
+ Name : "Kubernetes based template" ,
87
+ Description : "Pod based developer workspaces that live in kubernetes." ,
88
+ Tags : []string {"cloud" , "kubernetes" },
89
+ Icon : "/icon/k8s.png" ,
90
+ }, nil
91
+ }
92
+
93
+ func buildReadme (tpl * template.Template , input ReadmeInput ) (file , error ) {
94
+ var out bytes.Buffer
95
+ err := tpl .ExecuteTemplate (& out , "readme" , input )
96
+ if err != nil {
97
+ return file {}, xerrors .Errorf ("execute readme template: %w" , err )
98
+ }
99
+
100
+ return file {
101
+ name : "README.md" ,
102
+ content : out .Bytes (),
103
+ }, nil
104
+ }
105
+
49
106
func writeFiles (w * tar.Writer , files ... file ) error {
50
107
for _ , f := range files {
51
108
err := w .WriteHeader (& tar.Header {
@@ -59,42 +116,12 @@ func writeFiles(w *tar.Writer, files ...file) error {
59
116
if err != nil {
60
117
return err
61
118
}
62
- }
63
-
64
- return nil
65
- }
66
-
67
- func BuildReadme () {
68
-
69
- }
70
-
71
- func dockerTemplate () (* template.Template , error ) {
72
- return template .ParseFS (templates , "templates/docker.tpl" )
73
- }
74
-
75
- type ContainerInput struct {
76
- DockerImage string `json:"docker-image"`
77
- }
78
-
79
- type KubernetesInput struct {
80
- ContainerInput
81
- }
82
-
83
- func BuildDocker (input interface {}) {
84
-
85
- }
86
-
87
- func build (input interface {}) (string , error ) {
88
- tpl , err := dockerTemplate ()
89
- if err != nil {
90
- return "" , err
91
- }
92
119
93
- var buf bytes. Buffer
94
- err = tpl . Execute ( & buf , input )
95
- if err != nil {
96
- return "" , err
120
+ _ , err = w . Write ( f . content )
121
+ if err != nil {
122
+ return err
123
+ }
97
124
}
98
125
99
- return buf . String (), nil
126
+ return nil
100
127
}
0 commit comments