Skip to content

Commit 9e6891b

Browse files
committed
Simplify installFromConstraint
1 parent 635813b commit 9e6891b

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

pkg/install.go

+17-13
Original file line numberDiff line numberDiff line change
@@ -167,22 +167,26 @@ func installFromConstraint(tfconstraint *string, mirrorURL string) string {
167167
}
168168

169169
sort.Sort(sort.Reverse(semver.Collection(versions)))
170-
171170
for _, element := range versions {
172-
if constrains.Check(element) { // Validate a version against a constraint
173-
tfversion := element.String()
174-
if ValidVersionFormat(tfversion) { // check if version format is correct
175-
out, err := Install(tfversion, mirrorURL)
176-
if err != nil {
177-
log.Printf("Error during install %v", err)
178-
os.Exit(1)
179-
}
180-
181-
return out
182-
}
183-
log.Errorf("Invalid terraform version format.")
171+
// Validate a version against a constraint
172+
if !constrains.Check(element) {
173+
continue
174+
}
175+
tfversion := element.String()
176+
177+
// check if version format is correct
178+
if !ValidVersionFormat(tfversion) {
179+
log.Debugf("Version not with invalid format %v", tfversion)
180+
181+
continue
182+
}
183+
out, err := Install(tfversion, mirrorURL)
184+
if err != nil {
185+
log.Printf("Error during install %v", err)
184186
os.Exit(1)
185187
}
188+
189+
return out
186190
}
187191

188192
log.Errorf("No version found to match constraint. Follow the README.md instructions for setup. https://github.com/terraform-tools/simple-tfswitch/blob/main/README.md")

0 commit comments

Comments
 (0)