Skip to content

Commit 99f369c

Browse files
author
Alexey Boyko
committed
Fix lint issues
1 parent dbbfd60 commit 99f369c

File tree

4 files changed

+79
-58
lines changed

4 files changed

+79
-58
lines changed

cmd/flora/flora.go

+50-21
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import (
44
"fmt"
55
"log"
66
"os"
7+
"path"
78

89
version "github.com/hashicorp/go-version"
910
"github.com/ketchoop/flora"
11+
homedir "github.com/mitchellh/go-homedir"
1012
"github.com/urfave/cli"
1113
)
1214

@@ -16,7 +18,7 @@ var (
1618
VersionBuildDate = "Unknown" //nolint:gochecknoglobals
1719
)
1820

19-
func main() {
21+
func main() { //nolint:gocyclo
2022
app := cli.NewApp()
2123
app.Name = "flora"
2224
app.Usage = "Simple app to upgrade your terraform"
@@ -27,15 +29,20 @@ func main() {
2729
Name: "upgrade",
2830
Usage: "Upgrade terraform",
2931
Action: func(c *cli.Context) error {
30-
version, err := flora.GetLatestVersion()
32+
tfVersion, err := flora.GetLatestVersion()
3133

3234
if err != nil {
3335
log.Fatal(err)
3436
}
3537

36-
upgrader := flora.TerraformUpgrader{version}
38+
homeDir, _ := homedir.Dir()
39+
floraPath := path.Join(homeDir, ".flora")
3740

38-
upgrader.Run()
41+
upgrader := flora.TerraformUpgrader{Version: tfVersion, FloraPath: floraPath}
42+
43+
if err := upgrader.Run(); err != nil {
44+
log.Fatal(err)
45+
}
3946

4047
return nil
4148
},
@@ -50,28 +57,33 @@ func main() {
5057

5158
if versionConstraint == "" {
5259
versionConstraint = flora.GetVersionConstraint()
53-
60+
5461
if versionConstraint == "" {
55-
cli.ShowSubcommandHelp(c)
62+
if err := cli.ShowSubcommandHelp(c); err != nil {
63+
log.Fatal(err)
64+
}
5665
return nil
5766
}
5867
}
5968

60-
versionStr := flora.GetLatestVersionMatchingConstraint(versionConstraint)
61-
if versionStr == "" {
69+
tfVersion := flora.GetLatestVersionMatchingConstraint(versionConstraint)
70+
if tfVersion == "" {
6271
log.Printf("Can't find version matching constraint %s\n", versionConstraint)
6372
return nil
6473
}
6574

66-
upgrader := flora.TerraformUpgrader{versionStr}
75+
homeDir, _ := homedir.Dir()
76+
floraPath := path.Join(homeDir, ".flora")
77+
78+
upgrader := flora.TerraformUpgrader{Version: tfVersion, FloraPath: floraPath}
6779

68-
log.Print("Downloading Terraform " + versionStr)
80+
log.Print("Downloading Terraform " + tfVersion)
6981

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

74-
log.Print("Terraform " + versionStr + " was succesfully downloaded")
86+
log.Print("Terraform " + tfVersion + " was successfully downloaded")
7587

7688
return nil
7789
},
@@ -86,21 +98,29 @@ func main() {
8698

8799
if versionConstraint == "" {
88100
versionConstraint = flora.GetVersionConstraint()
89-
101+
90102
if versionConstraint == "" {
91-
cli.ShowCommandHelp(c, c.Command.Name)
103+
if err := cli.ShowCommandHelp(c, c.Command.Name); err != nil {
104+
log.Fatal(err)
105+
}
92106
return nil
93107
}
94108
}
95109

96-
versionStr := flora.GetLatestVersionMatchingConstraint(versionConstraint)
97-
if versionStr == "" {
110+
tfVersion := flora.GetLatestVersionMatchingConstraint(versionConstraint)
111+
if tfVersion == "" {
98112
log.Printf("Can't find version matching constarint %s\n", versionConstraint)
99113
return nil
100114
}
101115

102-
upgrader := flora.TerraformUpgrader{versionStr}
103-
upgrader.Run()
116+
homeDir, _ := homedir.Dir()
117+
floraPath := path.Join(homeDir, ".flora")
118+
119+
upgrader := flora.TerraformUpgrader{Version: tfVersion, FloraPath: floraPath}
120+
121+
if err := upgrader.Run(); err != nil {
122+
log.Fatal(err)
123+
}
104124

105125
return nil
106126
},
@@ -124,7 +144,11 @@ func main() {
124144
Name: "current",
125145
Usage: "Show currently used version of terraform",
126146
Action: func(c *cli.Context) error {
127-
currentVer, err := flora.GetCurrentVersion()
147+
148+
homeDir, _ := homedir.Dir()
149+
floraPath := path.Join(homeDir, ".flora")
150+
151+
currentVer, err := flora.GetCurrentVersion(floraPath)
128152

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

173+
homeDir, _ := homedir.Dir()
174+
floraPath := path.Join(homeDir, ".flora")
175+
149176
if c.Bool("local") {
150-
versions, err = flora.ListLocalVersions()
177+
versions, err = flora.ListLocalVersions(floraPath)
151178
} else {
152179
versions, err = flora.ListRemoteVersions()
153180
}
@@ -166,7 +193,7 @@ func main() {
166193
versions = versions[len(versions)-c.Int("num"):]
167194
}
168195

169-
curVer, err := flora.GetCurrentVersion()
196+
curVer, err := flora.GetCurrentVersion(floraPath)
170197

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

184-
app.Run(os.Args)
211+
if err := app.Run(os.Args); err != nil {
212+
log.Fatal(err)
213+
}
185214
}

unzip.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package flora
55
import (
66
"archive/zip"
77
"io"
8-
"log"
98
"os"
109
"path/filepath"
1110
"strings"
@@ -30,13 +29,14 @@ func unzip(src, dest string) ([]string, error) {
3029
defer rc.Close()
3130

3231
// Store filename/path for returning and using later on
33-
fpath := filepath.Join(dest, f.Name)
32+
fpath := filepath.Join(dest, f.Name) //nolint:gosec
3433
filenames = append(filenames, fpath)
3534

3635
if f.FileInfo().IsDir() {
37-
3836
// Make Folder
39-
os.MkdirAll(fpath, os.ModePerm)
37+
if err = os.MkdirAll(fpath, os.ModePerm); err != nil {
38+
return nil, err
39+
}
4040

4141
} else {
4242

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

4949
err = os.MkdirAll(fdir, os.ModePerm)
5050
if err != nil {
51-
log.Fatal(err)
5251
return filenames, err
5352
}
5453
f, err := os.OpenFile(

upgrader.go

+14-21
Original file line numberDiff line numberDiff line change
@@ -9,37 +9,28 @@ import (
99
"os"
1010
"path"
1111
"runtime"
12-
13-
homedir "github.com/mitchellh/go-homedir"
1412
)
1513

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

21-
var floraPath string
22-
23-
func init() {
24-
homeDir, _ := homedir.Dir()
25-
26-
floraPath = path.Join(homeDir, ".flora")
27-
}
28-
2919
type TerraformUpgrader struct {
30-
Version string
20+
Version string
21+
FloraPath string
3122
}
3223

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

3627
return os.IsNotExist(err)
3728
}
3829

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

42-
r, err := http.Get(tfFileURL)
33+
r, err := http.Get(tfFileURL) //nolint:gosec
4334

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

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

5445
if err != nil {
5546
return err
@@ -69,31 +60,33 @@ func (t TerraformUpgrader) DownloadTerraform() error {
6960
}
7061

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

7465
if err != nil {
7566
return err
7667
}
7768

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

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

8477
return nil
8578
}
8679

8780
func (t TerraformUpgrader) InstallNewTerraform() error {
88-
floraBinPath := path.Join(floraPath, "bin", "terraform")
81+
floraBinPath := path.Join(t.FloraPath, "bin", "terraform")
8982

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

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

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

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

122-
log.Print("Terraform " + t.Version + " was succesfully installed")
115+
log.Print("Terraform " + t.Version + " was successfully installed")
123116

124117
return nil
125118
}

versions.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package flora
22

33
import (
4+
"bufio"
45
"encoding/json"
56
"net/http"
67
"os"
78
"path"
89
"path/filepath"
10+
"regexp"
911
"sort"
1012
"strings"
11-
"regexp"
12-
"bufio"
1313

1414
version "github.com/hashicorp/go-version"
1515
)
@@ -39,7 +39,7 @@ func GetLatestVersion() (string, error) {
3939
return checkResponse.CurrentVersion, nil
4040
}
4141

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

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

54-
func ListLocalVersions() ([]*version.Version, error) {
54+
func ListLocalVersions(floraPath string) ([]*version.Version, error) {
5555
var versions []*version.Version
5656
var rawVersions []string
5757

@@ -117,19 +117,19 @@ func getVersionConstraintFromFile(file string) string {
117117
return ""
118118
}
119119
defer f.Close()
120-
120+
121121
re := regexp.MustCompile(`required_version[ \t]*=[ \t]*"(.*)"`)
122122

123123
scanner := bufio.NewScanner(f)
124-
124+
125125
for scanner.Scan() {
126126
str := scanner.Text()
127127
result := re.FindStringSubmatch(str)
128128
if len(result) >= 2 && result[1] != "" {
129129
return result[1]
130130
}
131131
}
132-
132+
133133
return ""
134134
}
135135

@@ -145,7 +145,7 @@ func GetVersionConstraint() string {
145145
return ""
146146
}
147147

148-
func getVersionMatchingConstraint(constraintString string, versions []*version.Version) (*version.Version) {
148+
func getVersionMatchingConstraint(constraintString string, versions []*version.Version) *version.Version {
149149
constraint, _ := version.NewConstraint(constraintString)
150150
for i := len(versions) - 1; i >= 0; i-- {
151151
if constraint.Check(versions[i]) {
@@ -157,10 +157,10 @@ func getVersionMatchingConstraint(constraintString string, versions []*version.V
157157

158158
func GetLatestVersionMatchingConstraint(versionConstraint string) string {
159159
versions, _ := ListRemoteVersions()
160-
version := getVersionMatchingConstraint(versionConstraint, versions)
161-
if version == nil {
160+
tfVersion := getVersionMatchingConstraint(versionConstraint, versions)
161+
if tfVersion == nil {
162162
return ""
163163
}
164164

165-
return version.String()
165+
return tfVersion.String()
166166
}

0 commit comments

Comments
 (0)