Skip to content

Commit 48cfe2d

Browse files
committed
Incorporated latest feedback
1 parent cdbfe1b commit 48cfe2d

File tree

2 files changed

+40
-55
lines changed

2 files changed

+40
-55
lines changed

cmd/clusterctl/pkg/client/repository/repository_local_unix.go

+39-54
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ import (
3636
// {basepath}/{provider-name}/{version}/{components.yaml}
3737
//
3838
// (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.
4141
//
4242
// Concrete example:
4343
// /home/user/go/src/sigs.k8s.io/aws/v0.4.7/infrastructure-components.yaml
@@ -52,7 +52,6 @@ type localRepository struct {
5252
basepath string
5353
providerName string
5454
defaultVersion string
55-
rootPath string
5655
componentsPath string
5756
}
5857

@@ -65,7 +64,7 @@ func (r *localRepository) DefaultVersion() string {
6564

6665
// RootPath returns rootPath field of localRepository struct
6766
func (r *localRepository) RootPath() string {
68-
return r.rootPath
67+
return "" // Not applicable with localRepository
6968
}
7069

7170
// ComponentsPath returns componentsPath field of localRepository struct
@@ -86,7 +85,7 @@ func (r *localRepository) GetFile(version, fileName string) ([]byte, error) {
8685
version = r.defaultVersion
8786
}
8887

89-
absolutePath := filepath.Join(r.basepath, r.providerName, version, r.rootPath, fileName)
88+
absolutePath := filepath.Join(r.basepath, r.providerName, version, r.RootPath(), fileName)
9089

9190
if f, err := os.Stat(absolutePath); err == nil {
9291
if f.IsDir() {
@@ -116,40 +115,40 @@ func newLocalRepository(providerConfig config.Provider, configVariablesClient co
116115

117116
var err error
118117

119-
urlSplit, err := url.Parse(providerConfig.URL())
118+
url, err := url.Parse(providerConfig.URL())
120119
if err != nil {
121120
return nil, errors.Wrap(err, "invalid url")
122121
}
123-
absPath := urlSplit.Path
124-
if urlSplit.RawPath != "" {
125-
absPath = urlSplit.RawPath
122+
absPath := url.Path
123+
if url.RawPath != "" {
124+
absPath = url.RawPath
126125
}
127126

128127
if !filepath.IsAbs(absPath) {
129128
return nil, errors.Errorf("invalid path: path %q must be an absolute path", providerConfig.URL())
130129
}
131130

132-
parts := strings.Split(providerConfig.URL(), "/")
131+
urlSplit := strings.Split(providerConfig.URL(), "/")
133132
// {basepath}/{provider-name}/{version}/{components.yaml}
134-
if len(parts) < 3 {
133+
if len(urlSplit) < 3 {
135134
return nil, errors.Errorf("invalid path: path should be in the form {basepath}/{provider-name}/{version}/{components.yaml}")
136135
}
137136
// 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]
140139
if defaultVersion != "latest" {
141140
_, err = version.ParseSemantic(defaultVersion)
142141
if err != nil {
143142
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)
144143
}
145144
}
146-
providerName := parts[len(parts)-3]
145+
providerName := urlSplit[len(urlSplit)-3]
147146
if providerName != providerConfig.Name() {
148147
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())
149148
}
150149
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]...)
153152
}
154153
basePath = filepath.Clean("/" + basePath) // ensure basePath starts with "/"
155154

@@ -159,7 +158,6 @@ func newLocalRepository(providerConfig config.Provider, configVariablesClient co
159158
basepath: basePath,
160159
providerName: providerName,
161160
defaultVersion: defaultVersion,
162-
rootPath: "", // Not applicable with localRepository
163161
componentsPath: componentsPath,
164162
}
165163

@@ -176,39 +174,25 @@ func newLocalRepository(providerConfig config.Provider, configVariablesClient co
176174
// getLatestRelease returns the latest release for the local repository.
177175
func (r *localRepository) getLatestRelease() (string, error) {
178176

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()
182178
if err != nil {
183-
return "", errors.Wrap(err, "failed to list release directories")
179+
return "", errors.Wrapf(err, "failed to get local repository versions")
184180
}
185-
// search for the latest release according to semantic version
186-
// releases with names that are not semantic version number are ignored
187181
var latestTag string
188182
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
205191
}
206192
}
207-
208193
if latestTag == "" {
209194
return "", errors.New("failed to find releases tagged with a valid semantic version number")
210195
}
211-
212196
return latestTag, nil
213197
}
214198

@@ -220,21 +204,22 @@ func (r *localRepository) getVersions() ([]string, error) {
220204
if err != nil {
221205
return nil, errors.Wrap(err, "failed to list release directories")
222206
}
223-
var versions []string
207+
versions := []string{}
224208
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
237221
}
222+
versions = append(versions, r)
238223
}
239224
return versions, nil
240225
}

cmd/clusterctl/pkg/client/repository/repository_local_unix_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func Test_localRepository_newLocalRepository(t *testing.T) {
109109
t.Run(tt.name, func(t *testing.T) {
110110
got, err := newLocalRepository(tt.fields.provider, tt.fields.configVariablesClient)
111111
if (err != nil) != tt.wantErr {
112-
t.Errorf("error = %v, wantErr %v", err, tt.wantErr)
112+
t.Fatalf("error = %v, wantErr %v", err, tt.wantErr)
113113
return
114114
}
115115

0 commit comments

Comments
 (0)