forked from paketo-buildpacks/packit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
279 lines (234 loc) · 9.04 KB
/
config.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package cargo
import (
"encoding/json"
"fmt"
"io"
"time"
"github.com/BurntSushi/toml"
)
type Config struct {
API string `toml:"api" json:"api,omitempty"`
Buildpack ConfigBuildpack `toml:"buildpack" json:"buildpack,omitempty"`
Metadata ConfigMetadata `toml:"metadata" json:"metadata,omitempty"`
Stacks []ConfigStack `toml:"stacks" json:"stacks,omitempty"`
Order []ConfigOrder `toml:"order" json:"order,omitempty"`
}
type ConfigStack struct {
ID string `toml:"id" json:"id,omitempty"`
Mixins []string `toml:"mixins" json:"mixins,omitempty"`
}
type ConfigBuildpack struct {
ID string `toml:"id" json:"id,omitempty"`
Name string `toml:"name" json:"name,omitempty"`
Version string `toml:"version" json:"version,omitempty"`
Homepage string `toml:"homepage,omitempty" json:"homepage,omitempty"`
ClearEnv bool `toml:"clear-env,omitempty" json:"clear-env,omitempty"`
Description string `toml:"description,omitempty" json:"description,omitempty"`
Keywords []string `toml:"keywords,omitempty" json:"keywords,omitempty"`
Licenses []ConfigBuildpackLicense `toml:"licenses,omitempty" json:"licenses,omitempty"`
SBOMFormats []string `toml:"sbom-formats,omitempty" json:"sbom-formats,omitempty"`
}
type ConfigBuildpackLicense struct {
Type string `toml:"type" json:"type"`
URI string `toml:"uri" json:"uri"`
}
type ConfigMetadata struct {
IncludeFiles []string `toml:"include-files" json:"include-files,omitempty"`
PrePackage string `toml:"pre-package" json:"pre-package,omitempty"`
DefaultVersions map[string]string `toml:"default-versions" json:"default-versions,omitempty"`
Dependencies []ConfigMetadataDependency `toml:"dependencies" json:"dependencies,omitempty"`
DependencyConstraints []ConfigMetadataDependencyConstraint `toml:"dependency-constraints" json:"dependency-constraints,omitempty"`
Unstructured map[string]interface{} `toml:"-" json:"-"`
}
type ConfigMetadataDependency struct {
Checksum string `toml:"checksum" json:"checksum,omitempty"`
CPE string `toml:"cpe" json:"cpe,omitempty"`
PURL string `toml:"purl" json:"purl,omitempty"`
DeprecationDate *time.Time `toml:"deprecation_date" json:"deprecation_date,omitempty"`
ID string `toml:"id" json:"id,omitempty"`
Licenses []interface{} `toml:"licenses" json:"licenses,omitempty"`
Name string `toml:"name" json:"name,omitempty"`
SHA256 string `toml:"sha256" json:"sha256,omitempty"`
Source string `toml:"source" json:"source,omitempty"`
SourceChecksum string `toml:"source-checksum" json:"source-checksum,omitempty"`
SourceSHA256 string `toml:"source_sha256" json:"source_sha256,omitempty"`
Stacks []string `toml:"stacks" json:"stacks,omitempty"`
StripComponents int `toml:"strip-components" json:"strip-components,omitempty"`
URI string `toml:"uri" json:"uri,omitempty"`
Version string `toml:"version" json:"version,omitempty"`
}
type ConfigMetadataDependencyConstraint struct {
Constraint string `toml:"constraint" json:"constraint,omitempty"`
ID string `toml:"id" json:"id,omitempty"`
Patches int `toml:"patches" json:"patches,omitempty"`
}
type ConfigOrder struct {
Group []ConfigOrderGroup `toml:"group" json:"group,omitempty"`
}
type ConfigOrderGroup struct {
ID string `toml:"id" json:"id,omitempty"`
Version string `toml:"version" json:"version,omitempty"`
Optional bool `toml:"optional,omitempty" json:"optional,omitempty"`
}
func EncodeConfig(writer io.Writer, config Config) error {
content, err := json.Marshal(config)
if err != nil {
return err
}
c := map[string]interface{}{}
err = json.Unmarshal(content, &c)
if err != nil {
return err
}
c, err = convertPatches(config.Metadata.DependencyConstraints, c)
if err != nil {
return err
}
c, err = convertStripComponents(config.Metadata.Dependencies, c)
if err != nil {
return err
}
return toml.NewEncoder(writer).Encode(c)
}
func DecodeConfig(reader io.Reader, config *Config) error {
var c map[string]interface{}
_, err := toml.NewDecoder(reader).Decode(&c)
if err != nil {
return err
}
content, err := json.Marshal(c)
if err != nil {
return err
}
return json.Unmarshal(content, config)
}
func (m ConfigMetadata) MarshalJSON() ([]byte, error) {
metadata := map[string]interface{}{}
for key, value := range m.Unstructured {
metadata[key] = value
}
if len(m.IncludeFiles) > 0 {
metadata["include-files"] = m.IncludeFiles
}
if len(m.PrePackage) > 0 {
metadata["pre-package"] = m.PrePackage
}
if len(m.Dependencies) > 0 {
metadata["dependencies"] = m.Dependencies
}
if len(m.DependencyConstraints) > 0 {
metadata["dependency-constraints"] = m.DependencyConstraints
}
if len(m.DefaultVersions) > 0 {
metadata["default-versions"] = m.DefaultVersions
}
return json.Marshal(metadata)
}
func (m *ConfigMetadata) UnmarshalJSON(data []byte) error {
var metadata map[string]json.RawMessage
err := json.Unmarshal(data, &metadata)
if err != nil {
return err
}
if includeFiles, ok := metadata["include-files"]; ok {
err = json.Unmarshal(includeFiles, &m.IncludeFiles)
if err != nil {
return err
}
delete(metadata, "include-files")
}
if prePackage, ok := metadata["pre-package"]; ok {
err = json.Unmarshal(prePackage, &m.PrePackage)
if err != nil {
return err
}
delete(metadata, "pre-package")
}
if dependencies, ok := metadata["dependencies"]; ok {
err = json.Unmarshal(dependencies, &m.Dependencies)
if err != nil {
return err
}
delete(metadata, "dependencies")
}
if dependencyConstraints, ok := metadata["dependency-constraints"]; ok {
err = json.Unmarshal(dependencyConstraints, &m.DependencyConstraints)
if err != nil {
return err
}
delete(metadata, "dependency-constraints")
}
if defaultVersions, ok := metadata["default-versions"]; ok {
err = json.Unmarshal(defaultVersions, &m.DefaultVersions)
if err != nil {
return err
}
delete(metadata, "default-versions")
}
if len(metadata) > 0 {
m.Unstructured = map[string]interface{}{}
for key, value := range metadata {
m.Unstructured[key] = value
}
}
return nil
}
func (cd ConfigMetadataDependency) HasStack(stack string) bool {
for _, s := range cd.Stacks {
if s == stack {
return true
}
}
return false
}
// Unmarshal stores json numbers in float64 types, adding an unnecessary decimal point to the patch in the final toml.
// convertPatches converts this float64 into an int and returns a new map that contains an integer value for patches
func convertPatches(cons []ConfigMetadataDependencyConstraint, c map[string]interface{}) (map[string]interface{}, error) {
if len(cons) > 0 {
metadata, ok := c["metadata"].(map[string]interface{})
if !ok {
return nil, fmt.Errorf("failure to assert type: unexpected data in metadata")
}
constraints, ok := metadata["dependency-constraints"].([]interface{})
if !ok {
return nil, fmt.Errorf("failure to assert type: unexpected data in constraints")
}
for _, dependencyConstraint := range constraints {
patches, ok := dependencyConstraint.(map[string]interface{})["patches"]
if !ok {
return nil, fmt.Errorf("failure to assert type: unexpected data in constraint patches")
}
floatPatches, ok := patches.(float64)
if !ok {
return nil, fmt.Errorf("failure to assert type: unexpected data")
}
dependencyConstraint.(map[string]interface{})["patches"] = int(floatPatches)
}
}
return c, nil
}
// Accomplishes the same this as the convertPatches function but for strip components in the dependencies list.
func convertStripComponents(deps []ConfigMetadataDependency, c map[string]interface{}) (map[string]interface{}, error) {
if len(deps) > 0 {
metadata, ok := c["metadata"].(map[string]interface{})
if !ok {
return nil, fmt.Errorf("failure to assert type: unexpected data in metadata")
}
dependencies, ok := metadata["dependencies"].([]interface{})
if !ok {
return nil, fmt.Errorf("failure to assert type: unexpected data in constraints")
}
for _, dependency := range dependencies {
stripComponents, ok := dependency.(map[string]interface{})["strip-components"]
if !ok {
continue
}
floatStripComponents, ok := stripComponents.(float64)
if !ok {
return nil, fmt.Errorf("failure to assert type: unexpected data")
}
dependency.(map[string]interface{})["strip-components"] = int(floatStripComponents)
}
}
return c, nil
}