Skip to content

Commit

Permalink
Fix lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexey Boyko committed Sep 9, 2019
1 parent dbbfd60 commit 99f369c
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 58 deletions.
71 changes: 50 additions & 21 deletions cmd/flora/flora.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
"fmt"
"log"
"os"
"path"

version "github.com/hashicorp/go-version"
"github.com/ketchoop/flora"
homedir "github.com/mitchellh/go-homedir"
"github.com/urfave/cli"
)

Expand All @@ -16,7 +18,7 @@ var (
VersionBuildDate = "Unknown" //nolint:gochecknoglobals
)

func main() {
func main() { //nolint:gocyclo
app := cli.NewApp()
app.Name = "flora"
app.Usage = "Simple app to upgrade your terraform"
Expand All @@ -27,15 +29,20 @@ func main() {
Name: "upgrade",
Usage: "Upgrade terraform",
Action: func(c *cli.Context) error {
version, err := flora.GetLatestVersion()
tfVersion, err := flora.GetLatestVersion()

if err != nil {
log.Fatal(err)
}

upgrader := flora.TerraformUpgrader{version}
homeDir, _ := homedir.Dir()
floraPath := path.Join(homeDir, ".flora")

upgrader.Run()
upgrader := flora.TerraformUpgrader{Version: tfVersion, FloraPath: floraPath}

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

return nil
},
Expand All @@ -50,28 +57,33 @@ func main() {

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

if versionConstraint == "" {
cli.ShowSubcommandHelp(c)
if err := cli.ShowSubcommandHelp(c); err != nil {
log.Fatal(err)
}
return nil
}
}

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

upgrader := flora.TerraformUpgrader{versionStr}
homeDir, _ := homedir.Dir()
floraPath := path.Join(homeDir, ".flora")

upgrader := flora.TerraformUpgrader{Version: tfVersion, FloraPath: floraPath}

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

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

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

return nil
},
Expand All @@ -86,21 +98,29 @@ func main() {

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

if versionConstraint == "" {
cli.ShowCommandHelp(c, c.Command.Name)
if err := cli.ShowCommandHelp(c, c.Command.Name); err != nil {
log.Fatal(err)
}
return nil
}
}

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

upgrader := flora.TerraformUpgrader{versionStr}
upgrader.Run()
homeDir, _ := homedir.Dir()
floraPath := path.Join(homeDir, ".flora")

upgrader := flora.TerraformUpgrader{Version: tfVersion, FloraPath: floraPath}

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

return nil
},
Expand All @@ -124,7 +144,11 @@ func main() {
Name: "current",
Usage: "Show currently used version of terraform",
Action: func(c *cli.Context) error {
currentVer, err := flora.GetCurrentVersion()

homeDir, _ := homedir.Dir()
floraPath := path.Join(homeDir, ".flora")

currentVer, err := flora.GetCurrentVersion(floraPath)

if err != nil {
switch err.(type) {
Expand All @@ -146,8 +170,11 @@ func main() {
var versions []*version.Version
var err error

homeDir, _ := homedir.Dir()
floraPath := path.Join(homeDir, ".flora")

if c.Bool("local") {
versions, err = flora.ListLocalVersions()
versions, err = flora.ListLocalVersions(floraPath)
} else {
versions, err = flora.ListRemoteVersions()
}
Expand All @@ -166,7 +193,7 @@ func main() {
versions = versions[len(versions)-c.Int("num"):]
}

curVer, err := flora.GetCurrentVersion()
curVer, err := flora.GetCurrentVersion(floraPath)

for _, version := range versions {
if err == nil && version.Equal(curVer) {
Expand All @@ -181,5 +208,7 @@ func main() {
},
}

app.Run(os.Args)
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
9 changes: 4 additions & 5 deletions unzip.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package flora
import (
"archive/zip"
"io"
"log"
"os"
"path/filepath"
"strings"
Expand All @@ -30,13 +29,14 @@ func unzip(src, dest string) ([]string, error) {
defer rc.Close()

// Store filename/path for returning and using later on
fpath := filepath.Join(dest, f.Name)
fpath := filepath.Join(dest, f.Name) //nolint:gosec
filenames = append(filenames, fpath)

if f.FileInfo().IsDir() {

// Make Folder
os.MkdirAll(fpath, os.ModePerm)
if err = os.MkdirAll(fpath, os.ModePerm); err != nil {
return nil, err
}

} else {

Expand All @@ -48,7 +48,6 @@ func unzip(src, dest string) ([]string, error) {

err = os.MkdirAll(fdir, os.ModePerm)
if err != nil {
log.Fatal(err)
return filenames, err
}
f, err := os.OpenFile(
Expand Down
35 changes: 14 additions & 21 deletions upgrader.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,28 @@ import (
"os"
"path"
"runtime"

homedir "github.com/mitchellh/go-homedir"
)

const (
tfBaseURL string = "https://releases.hashicorp.com/terraform/%s/terraform_%s.zip"
tfCheckpointURL string = "https://checkpoint-api.hashicorp.com/v1/check/terraform"
)

var floraPath string

func init() {
homeDir, _ := homedir.Dir()

floraPath = path.Join(homeDir, ".flora")
}

type TerraformUpgrader struct {
Version string
Version string
FloraPath string
}

func (t TerraformUpgrader) IsDownloadNeeded() bool {
_, err := os.Stat(floraPath + "/terraform_" + t.Version)
_, err := os.Stat(t.FloraPath + "/terraform_" + t.Version)

return os.IsNotExist(err)
}

func (t TerraformUpgrader) DownloadTerraform() error {
tfFileURL := fmt.Sprintf(tfBaseURL, t.Version, t.Version+"_"+runtime.GOOS+"_"+runtime.GOARCH)

r, err := http.Get(tfFileURL)
r, err := http.Get(tfFileURL) //nolint:gosec

if err != nil {
return err
Expand All @@ -49,7 +40,7 @@ func (t TerraformUpgrader) DownloadTerraform() error {
return errors.New("can't download terraform")
}

zipFile, err := os.Create(path.Join(floraPath, "terraform_"+t.Version+".zip")) // use pathlib
zipFile, err := os.Create(path.Join(t.FloraPath, "terraform_"+t.Version+".zip")) // use pathlib

if err != nil {
return err
Expand All @@ -69,31 +60,33 @@ func (t TerraformUpgrader) DownloadTerraform() error {
}

func (t TerraformUpgrader) UnzipAndClean() error {
_, err := unzip(path.Join(floraPath, "terraform_"+t.Version+".zip"), floraPath)
_, err := unzip(path.Join(t.FloraPath, "terraform_"+t.Version+".zip"), t.FloraPath)

if err != nil {
return err
}

if err = os.Remove(path.Join(floraPath, "terraform_"+t.Version+".zip")); err != nil {
if err := os.Remove(path.Join(t.FloraPath, "terraform_"+t.Version+".zip")); err != nil {
return err
}

os.Rename(path.Join(floraPath, "terraform"), path.Join(floraPath, "terraform_"+t.Version))
if err := os.Rename(path.Join(t.FloraPath, "terraform"), path.Join(t.FloraPath, "terraform_"+t.Version)); err != nil {
return err
}

return nil
}

func (t TerraformUpgrader) InstallNewTerraform() error {
floraBinPath := path.Join(floraPath, "bin", "terraform")
floraBinPath := path.Join(t.FloraPath, "bin", "terraform")

if _, err := os.Lstat(floraBinPath); err == nil {
os.Remove(floraBinPath)
}

log.Print("Adding symlink " + path.Join(floraPath, "terraform_"+t.Version) + "->" + floraBinPath)
log.Print("Adding symlink " + path.Join(t.FloraPath, "terraform_"+t.Version) + "->" + floraBinPath)

if err := os.Symlink(path.Join(floraPath, "terraform_"+t.Version), floraBinPath); err != nil {
if err := os.Symlink(path.Join(t.FloraPath, "terraform_"+t.Version), floraBinPath); err != nil {
return err
}

Expand All @@ -119,7 +112,7 @@ func (t TerraformUpgrader) Run() error {
log.Fatal(err)
}

log.Print("Terraform " + t.Version + " was succesfully installed")
log.Print("Terraform " + t.Version + " was successfully installed")

return nil
}
22 changes: 11 additions & 11 deletions versions.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package flora

import (
"bufio"
"encoding/json"
"net/http"
"os"
"path"
"path/filepath"
"regexp"
"sort"
"strings"
"regexp"
"bufio"

version "github.com/hashicorp/go-version"
)
Expand Down Expand Up @@ -39,7 +39,7 @@ func GetLatestVersion() (string, error) {
return checkResponse.CurrentVersion, nil
}

func GetCurrentVersion() (ver *version.Version, err error) {
func GetCurrentVersion(floraPath string) (ver *version.Version, err error) {
link, err := os.Readlink(path.Join(floraPath, "bin", "terraform"))

if err != nil {
Expand All @@ -51,7 +51,7 @@ func GetCurrentVersion() (ver *version.Version, err error) {
return
}

func ListLocalVersions() ([]*version.Version, error) {
func ListLocalVersions(floraPath string) ([]*version.Version, error) {
var versions []*version.Version
var rawVersions []string

Expand Down Expand Up @@ -117,19 +117,19 @@ func getVersionConstraintFromFile(file string) string {
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 ""
}

Expand All @@ -145,7 +145,7 @@ func GetVersionConstraint() string {
return ""
}

func getVersionMatchingConstraint(constraintString string, versions []*version.Version) (*version.Version) {
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]) {
Expand All @@ -157,10 +157,10 @@ func getVersionMatchingConstraint(constraintString string, versions []*version.V

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

return version.String()
return tfVersion.String()
}

0 comments on commit 99f369c

Please sign in to comment.