-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathwrite.go
More file actions
84 lines (77 loc) · 1.86 KB
/
Copy pathwrite.go
File metadata and controls
84 lines (77 loc) · 1.86 KB
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 (
"encoding/json"
"os"
"text/template"
)
func writeModuleJSON(path string, m ModuleManifest) error {
data, err := json.MarshalIndent(m, "", " ")
if err != nil {
return err
}
data = append(data, '\n')
return os.WriteFile(path, data, 0o600)
}
var tfTmplTemplate = template.Must(template.New("tf.tmpl").Funcs(template.FuncMap{
"hclValue": func(v ModuleVariable) string {
if v.Sensitive {
return "var." + v.Name
}
return "{{ .Variables." + v.Name + " }}"
},
}).Parse(`{{- range .SensitiveVars }}
variable "{{ .Name }}" {
description = "{{ .Description }}"
type = {{ .Type }}
sensitive = true
default = ""
}
{{ end -}}
module "{{ .ID }}" {
count = data.coder_workspace.me.start_count
source = "{{"{{"}} .RegistryBase {{"}}"}}/{{ .Namespace }}/{{ .ID }}/coder"
version = "{{"{{"}} .PinnedVersion {{"}}"}}"
agent_id = coder_agent.{{"{{"}} .AgentResourceName {{"}}"}}.id
{{- range .NonComputedVars }}
{{- if .Sensitive }}
{{ .Name }} = var.{{ .Name }}
{{- else }}
{{ .Name }} = {{"{{"}} .Variables.{{ .Name }} {{"}}"}}
{{- end }}
{{- end }}
}
`))
type tfTmplData struct {
ID string
Namespace string
SensitiveVars []ModuleVariable
NonComputedVars []ModuleVariable
}
func writeTFTmpl(path string, m ModuleManifest) error {
var sensitiveVars []ModuleVariable
var nonComputedVars []ModuleVariable
for _, v := range m.Variables {
if v.Computed {
continue
}
nonComputedVars = append(nonComputedVars, v)
if v.Sensitive {
sensitiveVars = append(sensitiveVars, v)
}
}
data := tfTmplData{
ID: m.ID,
Namespace: m.Namespace,
SensitiveVars: sensitiveVars,
NonComputedVars: nonComputedVars,
}
f, err := os.Create(path)
if err != nil {
return err
}
err = tfTmplTemplate.Execute(f, data)
if closeErr := f.Close(); err == nil {
err = closeErr
}
return err
}