Skip to content

Commit 1b69ca8

Browse files
dm3chketchoop
authored andcommitted
Added autodetect of terrafrom project version constrains
1 parent 1db309a commit 1b69ca8

File tree

4 files changed

+94
-22
lines changed

4 files changed

+94
-22
lines changed

cmd/flora/flora.go

+30-12
Original file line numberDiff line numberDiff line change
@@ -46,23 +46,32 @@ func main() {
4646
ArgsUsage: "TERRAFORM_VERSION",
4747
BashComplete: flora.VersionsCompletion,
4848
Action: func(c *cli.Context) error {
49-
if c.NArg() == 0 {
50-
cli.ShowSubcommandHelp(c)
49+
versionConstraint := c.Args().First()
5150

52-
return nil
51+
if versionConstraint == "" {
52+
versionConstraint = flora.GetVersionConstraint()
53+
54+
if versionConstraint == "" {
55+
cli.ShowSubcommandHelp(c)
56+
return nil
57+
}
5358
}
5459

55-
version := c.Args().First()
60+
versionStr := flora.GetLatestVersionMatchingConstraint(versionConstraint)
61+
if versionStr == "" {
62+
log.Printf("Can't find version matching constraint %s\n", versionConstraint)
63+
return nil
64+
}
5665

57-
upgrader := flora.TerraformUpgrader{version}
66+
upgrader := flora.TerraformUpgrader{versionStr}
5867

59-
log.Print("Downloading Terraform " + version)
68+
log.Print("Downloading Terraform " + versionStr)
6069

6170
if err := upgrader.DownloadTerraform(); err != nil {
6271
log.Fatal(err)
6372
}
6473

65-
log.Print("Terraform " + version + " was succesfully downloaded")
74+
log.Print("Terraform " + versionStr + " was succesfully downloaded")
6675

6776
return nil
6877
},
@@ -73,15 +82,24 @@ func main() {
7382
ArgsUsage: "TERRAFORM_VERSION",
7483
BashComplete: flora.VersionsCompletion,
7584
Action: func(c *cli.Context) error {
76-
version := c.Args().First()
85+
versionConstraint := c.Args().First()
7786

78-
if version == "" {
79-
cli.ShowCommandHelp(c, c.Command.Name)
80-
return nil
87+
if versionConstraint == "" {
88+
versionConstraint = flora.GetVersionConstraint()
89+
90+
if versionConstraint == "" {
91+
cli.ShowCommandHelp(c, c.Command.Name)
92+
return nil
93+
}
8194
}
8295

83-
upgrader := flora.TerraformUpgrader{version}
96+
versionStr := flora.GetLatestVersionMatchingConstraint(versionConstraint)
97+
if versionStr == "" {
98+
log.Printf("Can't find version matching constarint %s\n", versionConstraint)
99+
return nil
100+
}
84101

102+
upgrader := flora.TerraformUpgrader{versionStr}
85103
upgrader.Run()
86104

87105
return nil

go.mod

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
module github.com/ketchup/flora
1+
module github.com/ketchoop/flora
22

33
go 1.12
44

55
require (
6-
github.com/hashicorp/go-version v0.0.0-20171129150820-4fe82ae3040f
7-
github.com/ketchoop/flora v0.0.0-20180403072033-15eef771532a
8-
github.com/mitchellh/go-homedir v0.0.0-20161203194507-b8bc1bf76747
6+
github.com/hashicorp/go-version v1.2.0
7+
github.com/mitchellh/go-homedir v1.1.0
98
github.com/urfave/cli v1.20.0
109
)

go.sum

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
github.com/hashicorp/go-version v0.0.0-20171129150820-4fe82ae3040f h1:QnRipjW3Mm+sgD+vyO87cb+1RLzoM0mGVTYClil7mQg=
2-
github.com/hashicorp/go-version v0.0.0-20171129150820-4fe82ae3040f/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
3-
github.com/ketchoop/flora v0.0.0-20180403072033-15eef771532a h1:AUFDbm3xjeE0/oFTecZS/mHREcMp8FDnJ16YbtwtpME=
4-
github.com/ketchoop/flora v0.0.0-20180403072033-15eef771532a/go.mod h1:ke1QoAHYMr6+WOlnHQ2YrAWGLLVMIY1J3AexVrkwFz4=
5-
github.com/mitchellh/go-homedir v0.0.0-20161203194507-b8bc1bf76747 h1:eQox4Rh4ewJF+mqYPxCkmBAirRnPaHEB26UkNuPyjlk=
6-
github.com/mitchellh/go-homedir v0.0.0-20161203194507-b8bc1bf76747/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
1+
github.com/hashicorp/go-version v1.2.0 h1:3vNe/fWF5CBgRIguda1meWhsZHy3m8gCJ5wx+dIzX/E=
2+
github.com/hashicorp/go-version v1.2.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
3+
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
4+
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
75
github.com/urfave/cli v1.20.0 h1:fDqGv3UG/4jbVl/QkFwEdddtEDjh/5Ov6X+0B/3bPaw=
86
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=

versions.go

+57
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"path/filepath"
99
"sort"
1010
"strings"
11+
"regexp"
12+
"bufio"
1113

1214
version "github.com/hashicorp/go-version"
1315
)
@@ -107,3 +109,58 @@ func ListRemoteVersions() ([]*version.Version, error) {
107109

108110
return versions, nil
109111
}
112+
113+
func getVersionConstraintFromFile(file string) string {
114+
f, err := os.Open(file)
115+
116+
if err != nil {
117+
return ""
118+
}
119+
defer f.Close()
120+
121+
re := regexp.MustCompile(`required_version[ \t]*=[ \t]*"(.*)"`)
122+
123+
scanner := bufio.NewScanner(f)
124+
125+
for scanner.Scan() {
126+
str := scanner.Text()
127+
result := re.FindStringSubmatch(str)
128+
if len(result) >= 2 && result[1] != "" {
129+
return result[1]
130+
}
131+
}
132+
133+
return ""
134+
}
135+
136+
func GetVersionConstraint() string {
137+
tfFiles, _ := filepath.Glob("*.tf")
138+
for _, file := range tfFiles {
139+
versionConstrains := getVersionConstraintFromFile(file)
140+
if versionConstrains != "" {
141+
return versionConstrains
142+
}
143+
}
144+
145+
return ""
146+
}
147+
148+
func getVersionMatchingConstraint(constraintString string, versions []*version.Version) (*version.Version) {
149+
constraint, _ := version.NewConstraint(constraintString)
150+
for i := len(versions) - 1; i >= 0; i-- {
151+
if constraint.Check(versions[i]) {
152+
return versions[i]
153+
}
154+
}
155+
return nil
156+
}
157+
158+
func GetLatestVersionMatchingConstraint(versionConstraint string) string {
159+
versions, _ := ListRemoteVersions()
160+
version := getVersionMatchingConstraint(versionConstraint, versions)
161+
if version == nil {
162+
return ""
163+
}
164+
165+
return version.String()
166+
}

0 commit comments

Comments
 (0)