@@ -36,8 +36,8 @@ import (
36
36
// {basepath}/{provider-name}/{version}/{components.yaml}
37
37
//
38
38
// (1): {provider-name} must match the value returned by Provider.Name()
39
- // (2): {version} must exactly obey the syntax and semantics of
40
- // the "Semantic Versioning" specification (http://semver.org/).
39
+ // (2): {version} must obey the syntax and semantics of the "Semantic Versioning"
40
+ // specification (http://semver.org/); however, "latest" is also an acceptable value .
41
41
//
42
42
// Concrete example:
43
43
// /home/user/go/src/sigs.k8s.io/aws/v0.4.7/infrastructure-components.yaml
@@ -52,7 +52,6 @@ type localRepository struct {
52
52
basepath string
53
53
providerName string
54
54
defaultVersion string
55
- rootPath string
56
55
componentsPath string
57
56
}
58
57
@@ -65,7 +64,7 @@ func (r *localRepository) DefaultVersion() string {
65
64
66
65
// RootPath returns rootPath field of localRepository struct
67
66
func (r * localRepository ) RootPath () string {
68
- return r . rootPath
67
+ return "" // Not applicable with localRepository
69
68
}
70
69
71
70
// ComponentsPath returns componentsPath field of localRepository struct
@@ -86,7 +85,7 @@ func (r *localRepository) GetFile(version, fileName string) ([]byte, error) {
86
85
version = r .defaultVersion
87
86
}
88
87
89
- absolutePath := filepath .Join (r .basepath , r .providerName , version , r .rootPath , fileName )
88
+ absolutePath := filepath .Join (r .basepath , r .providerName , version , r .RootPath () , fileName )
90
89
91
90
if f , err := os .Stat (absolutePath ); err == nil {
92
91
if f .IsDir () {
@@ -116,40 +115,40 @@ func newLocalRepository(providerConfig config.Provider, configVariablesClient co
116
115
117
116
var err error
118
117
119
- urlSplit , err := url .Parse (providerConfig .URL ())
118
+ url , err := url .Parse (providerConfig .URL ())
120
119
if err != nil {
121
120
return nil , errors .Wrap (err , "invalid url" )
122
121
}
123
- absPath := urlSplit .Path
124
- if urlSplit .RawPath != "" {
125
- absPath = urlSplit .RawPath
122
+ absPath := url .Path
123
+ if url .RawPath != "" {
124
+ absPath = url .RawPath
126
125
}
127
126
128
127
if ! filepath .IsAbs (absPath ) {
129
128
return nil , errors .Errorf ("invalid path: path %q must be an absolute path" , providerConfig .URL ())
130
129
}
131
130
132
- parts := strings .Split (providerConfig .URL (), "/" )
131
+ urlSplit := strings .Split (providerConfig .URL (), "/" )
133
132
// {basepath}/{provider-name}/{version}/{components.yaml}
134
- if len (parts ) < 3 {
133
+ if len (urlSplit ) < 3 {
135
134
return nil , errors .Errorf ("invalid path: path should be in the form {basepath}/{provider-name}/{version}/{components.yaml}" )
136
135
}
137
136
// We work our way backwards with {components.yaml} being the last part of the path
138
- componentsPath := parts [len (parts )- 1 ]
139
- defaultVersion := parts [len (parts )- 2 ]
137
+ componentsPath := urlSplit [len (urlSplit )- 1 ]
138
+ defaultVersion := urlSplit [len (urlSplit )- 2 ]
140
139
if defaultVersion != "latest" {
141
140
_ , err = version .ParseSemantic (defaultVersion )
142
141
if err != nil {
143
142
return nil , errors .Errorf ("invalid version: %q. Version must obey the syntax and semantics of the \" Semantic Versioning\" specification (http://semver.org/) and path format {basepath}/{provider-name}/{version}/{components.yaml}" , defaultVersion )
144
143
}
145
144
}
146
- providerName := parts [len (parts )- 3 ]
145
+ providerName := urlSplit [len (urlSplit )- 3 ]
147
146
if providerName != providerConfig .Name () {
148
147
return nil , errors .Errorf ("invalid path: path %q must contain provider name %q in the format {basepath}/{provider-name}/{version}/{components.yaml}" , providerConfig .URL (), providerConfig .Name ())
149
148
}
150
149
var basePath string
151
- if len (parts ) > 3 {
152
- basePath = filepath .Join (parts [:len (parts )- 3 ]... )
150
+ if len (urlSplit ) > 3 {
151
+ basePath = filepath .Join (urlSplit [:len (urlSplit )- 3 ]... )
153
152
}
154
153
basePath = filepath .Clean ("/" + basePath ) // ensure basePath starts with "/"
155
154
@@ -159,7 +158,6 @@ func newLocalRepository(providerConfig config.Provider, configVariablesClient co
159
158
basepath : basePath ,
160
159
providerName : providerName ,
161
160
defaultVersion : defaultVersion ,
162
- rootPath : "" , // Not applicable with localRepository
163
161
componentsPath : componentsPath ,
164
162
}
165
163
@@ -176,39 +174,25 @@ func newLocalRepository(providerConfig config.Provider, configVariablesClient co
176
174
// getLatestRelease returns the latest release for the local repository.
177
175
func (r * localRepository ) getLatestRelease () (string , error ) {
178
176
179
- // get all the sub-directories under {basepath}/{provider-name}/
180
- releasesPath := filepath .Join (r .basepath , r .providerName )
181
- files , err := ioutil .ReadDir (releasesPath )
177
+ versions , err := r .getVersions ()
182
178
if err != nil {
183
- return "" , errors .Wrap (err , "failed to list release directories " )
179
+ return "" , errors .Wrapf (err , "failed to get local repository versions " )
184
180
}
185
- // search for the latest release according to semantic version
186
- // releases with names that are not semantic version number are ignored
187
181
var latestTag string
188
182
var latestReleaseVersion * version.Version
189
- for _ , f := range files {
190
- if f .IsDir () {
191
- r := f .Name ()
192
- sv , err := version .ParseSemantic (r )
193
- if err != nil {
194
- // discard releases with tags that are not a valid semantic versions (the user can point explicitly to such releases)
195
- continue
196
- }
197
- if sv .PreRelease () != "" || sv .BuildMetadata () != "" {
198
- // discard pre-releases or build releases (the user can point explicitly to such releases)
199
- continue
200
- }
201
- if latestReleaseVersion == nil || latestReleaseVersion .LessThan (sv ) {
202
- latestTag = r
203
- latestReleaseVersion = sv
204
- }
183
+ for _ , v := range versions {
184
+ sv , err := version .ParseSemantic (v )
185
+ if err != nil {
186
+ continue
187
+ }
188
+ if latestReleaseVersion == nil || latestReleaseVersion .LessThan (sv ) {
189
+ latestTag = v
190
+ latestReleaseVersion = sv
205
191
}
206
192
}
207
-
208
193
if latestTag == "" {
209
194
return "" , errors .New ("failed to find releases tagged with a valid semantic version number" )
210
195
}
211
-
212
196
return latestTag , nil
213
197
}
214
198
@@ -220,21 +204,22 @@ func (r *localRepository) getVersions() ([]string, error) {
220
204
if err != nil {
221
205
return nil , errors .Wrap (err , "failed to list release directories" )
222
206
}
223
- var versions []string
207
+ versions := []string {}
224
208
for _ , f := range files {
225
- if f .IsDir () {
226
- r := f . Name ()
227
- sv , err := version . ParseSemantic ( r )
228
- if err != nil {
229
- // discard releases with tags that are not a valid semantic versions (the user can point explicitly to such releases )
230
- continue
231
- }
232
- if sv . PreRelease () != "" || sv . BuildMetadata () != "" {
233
- // discard pre-releases or build releases (the user can point explicitly to such releases)
234
- continue
235
- }
236
- versions = append ( versions , r )
209
+ if ! f .IsDir () {
210
+ continue
211
+ }
212
+ r := f . Name ()
213
+ sv , err := version . ParseSemantic ( r )
214
+ if err != nil {
215
+ // discard releases with tags that are not a valid semantic versions (the user can point explicitly to such releases)
216
+ continue
217
+ }
218
+ if sv . PreRelease () != "" || sv . BuildMetadata () != "" {
219
+ // discard pre-releases or build releases (the user can point explicitly to such releases)
220
+ continue
237
221
}
222
+ versions = append (versions , r )
238
223
}
239
224
return versions , nil
240
225
}
0 commit comments