-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathappjson.go
198 lines (180 loc) · 4.85 KB
/
appjson.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package build
import (
"context"
"encoding/json"
"os"
"github.com/rs/zerolog/log"
)
type Environment struct {
Scripts map[string]string `json:"scripts"`
Env map[string]string `json:"env"`
Addons []string `json:"addons"`
}
type Buildpack struct {
URL string `json:"url"`
}
type AppJSON struct {
Buildpacks []Buildpack `json:"buildpacks"`
Stack string `json:"stack"`
Scripts map[string]string `json:"scripts"`
Environments map[string]Environment `json:"environments"`
reader func() ([]byte, error)
ctx context.Context
}
const DefaultStack = "heroku-20"
// buildpacks included in builder
var IncludedBuildpacks = map[string][]string{
"heroku-20": {
// $ pack builder inspect heroku/buildpacks:20 -o json | jq '.remote_info.buildpacks[].id'
"heroku/go",
"heroku/gradle",
"heroku/java",
"heroku/java-function",
"heroku/jvm",
"heroku/jvm-function-invoker",
"heroku/maven",
"heroku/nodejs",
"heroku/nodejs-engine",
"heroku/nodejs-function",
"heroku/nodejs-function-invoker",
"heroku/nodejs-npm",
"heroku/nodejs-yarn",
"heroku/php",
"heroku/procfile",
"heroku/python",
"heroku/ruby",
"heroku/scala",
},
"heroku-22": {
// $ pack builder inspect heroku/builder-classic:22 -o json | jq '.remote_info.buildpacks[].id'
"heroku/clojure",
"heroku/go",
"heroku/gradle",
"heroku/java",
"heroku/nodejs",
"heroku/php",
"heroku/procfile",
"heroku/python",
"heroku/ruby",
"heroku/scala",
},
}
// contains returns true if the string is in the slice
func contains(slice []string, s string) bool {
for _, item := range slice {
if item == s {
return true
}
}
return false
}
// patchBuildpack makes sure buildpacks which are included in the builder are used
// https://github.com/heroku/builder/issues/298
func patchBuildpack(buildpack string, stack string) string {
CNBBuildpacks, ok := IncludedBuildpacks[stack]
if !ok {
return buildpack
}
if contains(CNBBuildpacks, buildpack) {
return "urn:cnb:builder:" + buildpack
}
return buildpack
}
func (a *AppJSON) Unmarshal() error {
content, err := a.reader()
if err != nil {
// app.json is optional - default to empty
log.Ctx(a.ctx).Debug().Err(err).Msg("failed to read app.json")
content = []byte("{}")
}
// set default stack
a.Stack = DefaultStack
err = json.Unmarshal(content, &a)
if err != nil {
log.Ctx(a.ctx).Error().Err(err).Msg("failed to parse app.json")
return err
}
return nil
}
func ParseAppJson(ctx context.Context) (*AppJSON, error) {
appJson := AppJSON{
ctx: ctx,
reader: func() ([]byte, error) {
return os.ReadFile("app.json")
},
}
err := appJson.Unmarshal()
if err != nil {
return nil, err
}
return &appJson, nil
}
// GetBuilders returns the builders from app.json in a format pack can use
// the first item in the list is the builder, followed by the stack image
// the stack image is only used for prefetching, so non-heroku stacks should still work
func (a *AppJSON) GetBuilders() []string {
if a.Stack == "heroku-18" {
return []string{"heroku/buildpacks:18", "heroku/heroku:18-cnb"}
}
if a.Stack == "heroku-20" {
return []string{"heroku/buildpacks:20", "heroku/heroku:20-cnb"}
}
// TODO: use heroku/builder:22 can be used when all buildpacks are CNB
if a.Stack == "heroku-22" {
return []string{"heroku/builder-classic:22", "heroku/heroku:22-cnb"}
}
return []string{a.Stack}
}
// GetBuildpacks returns the buildpacks from app.json in a format pack can use
func (a *AppJSON) GetBuildpacks() []string {
var buildpacks []string
for _, bp := range a.Buildpacks {
buildpacks = append(buildpacks, patchBuildpack(bp.URL, a.Stack))
}
return buildpacks
}
// TestScript returns the test script from app.json
func (a *AppJSON) TestScript() string {
script, ok := a.Environments["test"].Scripts["test"]
if !ok {
return ""
}
return script
}
func (a *AppJSON) GetEnv() map[string]string {
env := map[string]string{}
for k, v := range a.Environments["test"].Env {
env[k] = v
}
return env
}
// GetTestEnv returns the test environment from app.json
func (a *AppJSON) GetTestEnv() map[string]string {
env := a.GetEnv()
env["CI"] = "true"
return env
}
// GetTestAddons returns the test addons from app.json
func (a *AppJSON) GetTestAddons() []string {
return a.Environments["test"].Addons
}
// ToApppackToml converts app.json to an apppack.toml
func (a *AppJSON) ToApppackToml() *AppPackToml {
t := AppPackToml{}
t.Build.System = "buildpack"
t.Build.Builder = a.GetBuilders()[0]
t.Build.Buildpacks = a.GetBuildpacks()
if a.Scripts["postdeploy"] != "" {
t.ReviewApp.InitializeCommand = a.Scripts["postdeploy"]
}
if a.Scripts["pr-predestroy"] != "" {
t.ReviewApp.PreDestroyCommand = a.Scripts["pr-predestroy"]
}
if a.TestScript() != "" {
t.Test.Command = a.TestScript()
for k, v := range a.GetEnv() {
t.Test.Env = append(t.Test.Env, k+"="+v)
}
}
return &t
}