-
Notifications
You must be signed in to change notification settings - Fork 241
/
serde.go
251 lines (224 loc) · 6 KB
/
serde.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
package appconfig
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"os"
"time"
"github.com/BurntSushi/toml"
"github.com/samber/lo"
"github.com/superfly/flyctl/helpers"
"github.com/superfly/flyctl/iostreams"
)
// LoadConfig loads the app config at the given path.
func LoadConfig(path string) (cfg *Config, err error) {
buf, err := os.ReadFile(path)
if err != nil {
return nil, err
}
cfg, err = unmarshalTOML(buf)
if err != nil {
return nil, err
}
cfg.configFilePath = path
// cfg.WriteToFile("patched-fly.toml")
return cfg, nil
}
func (c *Config) WriteTo(w io.Writer) error {
b, err := c.marshalTOML()
if err != nil {
return err
}
_, err = fmt.Fprintf(w, "# fly.toml file generated for %s on %s\n\n", c.AppName, time.Now().Format(time.RFC3339))
if err != nil {
return err
}
_, err = bytes.NewBuffer(b).WriteTo(w)
return err
}
func (c *Config) WriteToFile(filename string) (err error) {
if err = helpers.MkdirAll(filename); err != nil {
return
}
var file *os.File
if file, err = os.Create(filename); err != nil {
return
}
defer func() {
if e := file.Close(); err == nil {
err = e
}
}()
err = c.WriteTo(file)
return
}
func (c *Config) WriteToDisk(ctx context.Context, path string) (err error) {
io := iostreams.FromContext(ctx)
err = c.WriteToFile(path)
fmt.Fprintf(io.Out, "Wrote config file %s\n", helpers.PathRelativeToCWD(path))
return
}
// MarshalJSON implements the json.Marshaler interface
func (c *Config) MarshalJSON() ([]byte, error) {
switch {
case c == nil:
return json.Marshal(nil)
case c.platformVersion == MachinesPlatform:
return json.Marshal(*c)
default:
sections, err := c.rawSections()
if err != nil {
return nil, err
}
return json.Marshal(lo.Assign(sections...))
}
}
// marshalTOML serializes the configuration to TOML format
// NOTES:
// * It can't be called `MarshalTOML` because toml libraries don't support marshaler interface on root values
// * Needs to reimplements most of MarshalJSON to enforce order of fields
// * Instead of this, you usually need one WriteTo(), WriteToFile() or WriteToDisk()
//
func (c *Config) marshalTOML() ([]byte, error) {
var b bytes.Buffer
encoder := toml.NewEncoder(&b)
switch {
case c == nil:
break
case c.platformVersion == MachinesPlatform:
if err := encoder.Encode(c); err != nil {
return nil, err
}
default:
// FallBack for Nomad apps
sections, err := c.rawSections()
if err != nil {
return nil, err
}
for _, section := range sections {
if err := encoder.Encode(section); err != nil {
return nil, err
}
}
}
return b.Bytes(), nil
}
// rawSections returns configuration parts in serialization order for Nomad apps
func (c *Config) rawSections() ([]map[string]any, error) {
// Write app name first to be sure it will be there at the top
sections := []map[string]any{
{"app": c.AppName},
}
rawData := c.SanitizedDefinition()
// Restore sections removed by SanitizedDefinition
if c.Build != nil {
rawData["build"] = c.Build
}
if c.PrimaryRegion != "" {
rawData["primary_region"] = c.PrimaryRegion
}
if c.HTTPService != nil {
rawData["http_service"] = c.HTTPService
}
if len(rawData) > 0 {
// roundtrip through json encoder to convert float64 numbers to json.Number,
// otherwise numbers are floats in toml
var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(rawData); err != nil {
return nil, err
}
d := json.NewDecoder(&buf)
d.UseNumber()
if err := d.Decode(&rawData); err != nil {
return nil, err
}
sections = append(sections, rawData)
}
return sections, nil
}
func unmarshalTOML(buf []byte) (*Config, error) {
// Keep this map as vanilla as possible
// This is what we send to Web API for Nomad apps
rawDefinition := map[string]any{}
if err := toml.Unmarshal(buf, &rawDefinition); err != nil {
return nil, err
}
// Unmarshal twice due to in-place updates
cfgMap := map[string]any{}
if err := toml.Unmarshal(buf, &cfgMap); err != nil {
return nil, err
}
cfg, err := applyPatches(cfgMap)
// In case of parsing error fallback to Nomad only compatibility
if err != nil {
cfg = &Config{v2UnmarshalError: err}
if name, ok := (rawDefinition["app"]).(string); ok {
cfg.AppName = name
}
cfg.Build = unmarshalBuild(rawDefinition)
}
cfg.RawDefinition = rawDefinition
return cfg, nil
}
// Fallback method when we fail to parse fly.toml into Config
// XXX: High chances we can ditch and unmarshal directly into Build struct
func unmarshalBuild(data map[string]interface{}) *Build {
buildConfig, ok := (data["build"]).(map[string]interface{})
if !ok {
return nil
}
b := &Build{
Args: map[string]string{},
Settings: map[string]interface{}{},
Buildpacks: []string{},
}
configValueSet := false
for k, v := range buildConfig {
switch k {
case "builder":
b.Builder = fmt.Sprint(v)
configValueSet = configValueSet || b.Builder != ""
case "buildpacks":
if bpSlice, ok := v.([]interface{}); ok {
for _, argV := range bpSlice {
b.Buildpacks = append(b.Buildpacks, fmt.Sprint(argV))
}
}
case "args":
if argMap, ok := v.(map[string]interface{}); ok {
for argK, argV := range argMap {
b.Args[argK] = fmt.Sprint(argV)
}
}
case "builtin":
b.Builtin = fmt.Sprint(v)
configValueSet = configValueSet || b.Builtin != ""
case "settings":
if settingsMap, ok := v.(map[string]interface{}); ok {
for settingK, settingV := range settingsMap {
b.Settings[settingK] = settingV // fmt.Sprint(argV)
}
}
case "image":
b.Image = fmt.Sprint(v)
configValueSet = configValueSet || b.Image != ""
case "dockerfile":
b.Dockerfile = fmt.Sprint(v)
configValueSet = configValueSet || b.Dockerfile != ""
case "ignorefile":
b.Ignorefile = fmt.Sprint(v)
configValueSet = configValueSet || b.Ignorefile != ""
case "build_target", "build-target":
b.DockerBuildTarget = fmt.Sprint(v)
configValueSet = configValueSet || b.DockerBuildTarget != ""
default:
b.Args[k] = fmt.Sprint(v)
}
}
if !configValueSet && len(b.Args) == 0 {
return nil
}
return b
}