-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupgrades.go
192 lines (154 loc) · 4.7 KB
/
upgrades.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
package config
import (
"errors"
"fmt"
)
var ErrFailedUpgrade = errors.New("failed to upgrade config")
type UpgradeFunc func(target, template, oldVersion, newVersion string, cfg map[string]any) (map[string]any, error)
func upgrade(currentVersion string, cfg map[string]any, lockFile map[string]any, uf UpgradeFunc) (map[string]any, map[string]any, error) {
if currentVersion == "" {
var err error
currentVersion, cfg, err = upgradeToV100(cfg, uf)
if err != nil {
return nil, nil, err
}
}
if currentVersion == v1 {
var err error
currentVersion, cfg, lockFile, err = upgradeToV200(cfg, uf)
if err != nil {
return nil, nil, err
}
}
// Put upgrade logic for future versions here, also upgrade incrementally between versions
if currentVersion != Version {
return nil, nil, ErrFailedUpgrade
}
return cfg, lockFile, nil
}
func upgradeToV100(cfg map[string]any, uf UpgradeFunc) (string, map[string]any, error) {
generation := map[string]any{}
upgraded := map[string]any{
"configVersion": v1,
"generation": generation,
}
if mgmtAny, ok := cfg["management"]; ok {
mgmt, ok := mgmtAny.(map[string]any)
if !ok {
return "", nil, fmt.Errorf("%w: management is not a map", ErrFailedUpgrade)
}
upgraded["management"] = map[string]any{
"docChecksum": mgmt["openapi-checksum"],
"docVersion": mgmt["openapi-version"],
"speakeasyVersion": mgmt["speakeasy-version"],
}
delete(cfg, "management")
}
if commentsAny, ok := cfg["comments"]; ok {
comments, ok := commentsAny.(map[string]any)
if !ok {
return "", nil, fmt.Errorf("%w: comments is not a map", ErrFailedUpgrade)
}
generation["comments"] = map[string]any{
"disableComments": comments["disabled"],
"omitDescriptionIfSummaryPresent": comments["omitdescriptionifsummarypresent"],
}
delete(cfg, "comments")
}
baseServerURL, ok := cfg["baseserverurl"]
if ok {
generation["baseServerUrl"] = baseServerURL
delete(cfg, "baseserverurl")
}
sdkClassName, ok := cfg["sdkclassname"]
if ok {
generation["sdkClassName"] = sdkClassName
delete(cfg, "sdkclassname")
}
tagNamespacingDisabled, ok := cfg["tagnamespacingdisabled"]
if ok {
generation["tagNamespacingDisabled"] = tagNamespacingDisabled
delete(cfg, "tagnamespacingdisabled")
}
// Remaining keys are language configs
for lang, langCfgAny := range cfg {
langCfg, ok := langCfgAny.(map[string]any)
if !ok {
return "", nil, fmt.Errorf("%w: %s is not a map", ErrFailedUpgrade, lang)
}
langCfg, err := uf(lang, lang, "", v1, langCfg)
if err != nil {
return "", nil, err
}
upgraded[lang] = langCfg
}
return v1, upgraded, nil
}
func upgradeToV200(cfg map[string]any, uf UpgradeFunc) (string, map[string]any, map[string]any, error) {
upgradedConfig := map[string]any{
"configVersion": v2,
}
newLockFile := map[string]any{
"lockVersion": v2,
"id": getUUID(),
}
delete(cfg, "configVersion")
management := map[string]any{}
if mgmt, ok := cfg["management"]; ok {
management, ok = mgmt.(map[string]any)
if !ok {
return "", nil, nil, fmt.Errorf("%w: management is not a map", ErrFailedUpgrade)
}
delete(cfg, "management")
}
if features, ok := cfg["features"]; ok {
newLockFile["features"] = features
delete(cfg, "features")
}
if generation, ok := cfg["generation"]; ok {
genMap, ok := generation.(map[string]any)
if !ok {
return "", nil, nil, fmt.Errorf("%w: generation is not a map", ErrFailedUpgrade)
}
if repoURL, ok := genMap["repoURL"]; ok {
management["repoURL"] = repoURL
delete(genMap, "repoURL")
}
delete(genMap, "comments")
delete(genMap, "singleTagPerOp")
delete(genMap, "tagNamespacingDisabled")
upgradedConfig["generation"] = genMap
delete(cfg, "generation")
}
// Remaining keys are language configs
for lang, langCfgAny := range cfg {
langCfg, ok := langCfgAny.(map[string]any)
if !ok {
return "", nil, nil, fmt.Errorf("%w: %s is not a map", ErrFailedUpgrade, lang)
}
if published, ok := langCfg["published"]; ok {
management["published"] = published
delete(langCfg, "published")
}
if repoSubDirectory, ok := langCfg["repoSubDirectory"]; ok {
management["repoSubDirectory"] = repoSubDirectory
delete(langCfg, "repoSubDirectory")
}
if installationURL, ok := langCfg["installationURL"]; ok {
management["installationURL"] = installationURL
delete(langCfg, "installationURL")
}
if version, ok := langCfg["version"]; ok {
management["releaseVersion"] = version
}
langCfg, err := uf(lang, lang, v1, v2, langCfg)
if err != nil {
return "", nil, nil, err
}
upgradedConfig[lang] = langCfg
}
if len(management) > 0 {
newLockFile["management"] = management
}
return v2, upgradedConfig, newLockFile, nil
}