@@ -120,14 +120,14 @@ func IsSkaffoldConfig(file string) bool {
120
120
return false
121
121
}
122
122
123
- if config , err := ParseConfig (file , false ); err == nil && config != nil {
123
+ if config , err := ParseConfig (file ); err == nil && config != nil {
124
124
return true
125
125
}
126
126
return false
127
127
}
128
128
129
129
// ParseConfig reads a configuration file.
130
- func ParseConfig (filename string , upgrade bool ) (util.VersionedConfig , error ) {
130
+ func ParseConfig (filename string ) (util.VersionedConfig , error ) {
131
131
buf , err := misc .ReadConfiguration (filename )
132
132
if err != nil {
133
133
return nil , fmt .Errorf ("read skaffold config: %w" , err )
@@ -146,7 +146,7 @@ func ParseConfig(filename string, upgrade bool) (util.VersionedConfig, error) {
146
146
147
147
factory , present := SchemaVersions .Find (apiVersion .Version )
148
148
if ! present {
149
- return nil , fmt .Errorf ("unknown api version: '%s' " , apiVersion .Version )
149
+ return nil , fmt .Errorf ("unknown api version: %q " , apiVersion .Version )
150
150
}
151
151
152
152
// Remove all top-level keys starting with `.` so they can be used as YAML anchors
@@ -169,42 +169,46 @@ func ParseConfig(filename string, upgrade bool) (util.VersionedConfig, error) {
169
169
return nil , fmt .Errorf ("unable to parse config: %w" , err )
170
170
}
171
171
172
- if upgrade && cfg .GetVersion () != latest .Version {
173
- cfg , err = upgradeToLatest (cfg )
174
- if err != nil {
175
- return nil , err
176
- }
177
- }
178
-
179
172
return cfg , nil
180
173
}
181
174
182
- // upgradeToLatest upgrades a configuration to the latest version.
183
- func upgradeToLatest (vc util.VersionedConfig ) (util.VersionedConfig , error ) {
184
- var err error
175
+ // ParseConfigAndUpgrade reads a configuration file and upgrades it to a given version.
176
+ func ParseConfigAndUpgrade (filename , toVersion string ) (util.VersionedConfig , error ) {
177
+ cfg , err := ParseConfig (filename )
178
+ if err != nil {
179
+ return nil , err
180
+ }
181
+
182
+ // Check that the target version exists
183
+ if _ , present := SchemaVersions .Find (toVersion ); ! present {
184
+ return nil , fmt .Errorf ("unknown api version: %q" , toVersion )
185
+ }
185
186
186
- // first, check to make sure config version isn't too new
187
- version , err := apiversion .Parse (vc .GetVersion ())
187
+ // Check that the config's version is not newer than the target version
188
+ currentVersion , err := apiversion .Parse (cfg .GetVersion ())
188
189
if err != nil {
189
- return nil , fmt .Errorf ("parsing api version: %w" , err )
190
+ return nil , err
191
+ }
192
+ targetVersion , err := apiversion .Parse (toVersion )
193
+ if err != nil {
194
+ return nil , err
190
195
}
191
196
192
- semver := apiversion .MustParse (latest .Version )
193
- if version .EQ (semver ) {
194
- return vc , nil
197
+ if currentVersion .EQ (targetVersion ) {
198
+ return cfg , nil
195
199
}
196
- if version .GT (semver ) {
197
- return nil , fmt .Errorf ("config version %s is too new for this version: upgrade Skaffold" , vc .GetVersion ())
200
+ if currentVersion .GT (targetVersion ) {
201
+ return nil , fmt .Errorf ("config version %q is more recent than target version %q : upgrade Skaffold" , cfg .GetVersion (), toVersion )
198
202
}
199
203
200
- logrus .Debugf ("config version (%s) out of date: upgrading to latest (%s) " , vc .GetVersion (), latest . Version )
204
+ logrus .Debugf ("config version %q out of date: upgrading to latest %q " , cfg .GetVersion (), toVersion )
201
205
202
- for vc .GetVersion () != latest . Version {
203
- vc , err = vc .Upgrade ()
206
+ for cfg .GetVersion () != toVersion {
207
+ cfg , err = cfg .Upgrade ()
204
208
if err != nil {
205
209
return nil , fmt .Errorf ("transforming skaffold config: %w" , err )
206
210
}
207
211
}
208
212
209
- return vc , nil
213
+ return cfg , nil
210
214
}
0 commit comments