Skip to content

Commit

Permalink
Added autodetect of terrafrom project version constrains
Browse files Browse the repository at this point in the history
  • Loading branch information
dm3ch authored and ketchoop committed Sep 9, 2019
1 parent 1db309a commit 1b69ca8
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 22 deletions.
42 changes: 30 additions & 12 deletions cmd/flora/flora.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,32 @@ func main() {
ArgsUsage: "TERRAFORM_VERSION",
BashComplete: flora.VersionsCompletion,
Action: func(c *cli.Context) error {
if c.NArg() == 0 {
cli.ShowSubcommandHelp(c)
versionConstraint := c.Args().First()

return nil
if versionConstraint == "" {
versionConstraint = flora.GetVersionConstraint()

if versionConstraint == "" {
cli.ShowSubcommandHelp(c)
return nil
}
}

version := c.Args().First()
versionStr := flora.GetLatestVersionMatchingConstraint(versionConstraint)
if versionStr == "" {
log.Printf("Can't find version matching constraint %s\n", versionConstraint)
return nil
}

upgrader := flora.TerraformUpgrader{version}
upgrader := flora.TerraformUpgrader{versionStr}

log.Print("Downloading Terraform " + version)
log.Print("Downloading Terraform " + versionStr)

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

log.Print("Terraform " + version + " was succesfully downloaded")
log.Print("Terraform " + versionStr + " was succesfully downloaded")

return nil
},
Expand All @@ -73,15 +82,24 @@ func main() {
ArgsUsage: "TERRAFORM_VERSION",
BashComplete: flora.VersionsCompletion,
Action: func(c *cli.Context) error {
version := c.Args().First()
versionConstraint := c.Args().First()

if version == "" {
cli.ShowCommandHelp(c, c.Command.Name)
return nil
if versionConstraint == "" {
versionConstraint = flora.GetVersionConstraint()

if versionConstraint == "" {
cli.ShowCommandHelp(c, c.Command.Name)
return nil
}
}

upgrader := flora.TerraformUpgrader{version}
versionStr := flora.GetLatestVersionMatchingConstraint(versionConstraint)
if versionStr == "" {
log.Printf("Can't find version matching constarint %s\n", versionConstraint)
return nil
}

upgrader := flora.TerraformUpgrader{versionStr}
upgrader.Run()

return nil
Expand Down
7 changes: 3 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
module github.com/ketchup/flora
module github.com/ketchoop/flora

go 1.12

require (
github.com/hashicorp/go-version v0.0.0-20171129150820-4fe82ae3040f
github.com/ketchoop/flora v0.0.0-20180403072033-15eef771532a
github.com/mitchellh/go-homedir v0.0.0-20161203194507-b8bc1bf76747
github.com/hashicorp/go-version v1.2.0
github.com/mitchellh/go-homedir v1.1.0
github.com/urfave/cli v1.20.0
)
10 changes: 4 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
github.com/hashicorp/go-version v0.0.0-20171129150820-4fe82ae3040f h1:QnRipjW3Mm+sgD+vyO87cb+1RLzoM0mGVTYClil7mQg=
github.com/hashicorp/go-version v0.0.0-20171129150820-4fe82ae3040f/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/ketchoop/flora v0.0.0-20180403072033-15eef771532a h1:AUFDbm3xjeE0/oFTecZS/mHREcMp8FDnJ16YbtwtpME=
github.com/ketchoop/flora v0.0.0-20180403072033-15eef771532a/go.mod h1:ke1QoAHYMr6+WOlnHQ2YrAWGLLVMIY1J3AexVrkwFz4=
github.com/mitchellh/go-homedir v0.0.0-20161203194507-b8bc1bf76747 h1:eQox4Rh4ewJF+mqYPxCkmBAirRnPaHEB26UkNuPyjlk=
github.com/mitchellh/go-homedir v0.0.0-20161203194507-b8bc1bf76747/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/hashicorp/go-version v1.2.0 h1:3vNe/fWF5CBgRIguda1meWhsZHy3m8gCJ5wx+dIzX/E=
github.com/hashicorp/go-version v1.2.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/urfave/cli v1.20.0 h1:fDqGv3UG/4jbVl/QkFwEdddtEDjh/5Ov6X+0B/3bPaw=
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
57 changes: 57 additions & 0 deletions versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"path/filepath"
"sort"
"strings"
"regexp"
"bufio"

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

return versions, nil
}

func getVersionConstraintFromFile(file string) string {
f, err := os.Open(file)

if err != nil {
return ""
}
defer f.Close()

re := regexp.MustCompile(`required_version[ \t]*=[ \t]*"(.*)"`)

scanner := bufio.NewScanner(f)

for scanner.Scan() {
str := scanner.Text()
result := re.FindStringSubmatch(str)
if len(result) >= 2 && result[1] != "" {
return result[1]
}
}

return ""
}

func GetVersionConstraint() string {
tfFiles, _ := filepath.Glob("*.tf")
for _, file := range tfFiles {
versionConstrains := getVersionConstraintFromFile(file)
if versionConstrains != "" {
return versionConstrains
}
}

return ""
}

func getVersionMatchingConstraint(constraintString string, versions []*version.Version) (*version.Version) {
constraint, _ := version.NewConstraint(constraintString)
for i := len(versions) - 1; i >= 0; i-- {
if constraint.Check(versions[i]) {
return versions[i]
}
}
return nil
}

func GetLatestVersionMatchingConstraint(versionConstraint string) string {
versions, _ := ListRemoteVersions()
version := getVersionMatchingConstraint(versionConstraint, versions)
if version == nil {
return ""
}

return version.String()
}

0 comments on commit 1b69ca8

Please sign in to comment.