Skip to content

Commit 6abeee1

Browse files
committed
Add remote version check
1 parent 34f8af8 commit 6abeee1

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

cmd/version.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package cmd
22

33
import (
4+
"context"
5+
"errors"
46
"fmt"
57
"log"
68

9+
"github.com/google/go-github/v35/github"
10+
"github.com/hashicorp/go-version"
711
"github.com/spf13/afero"
812
"github.com/terraform-linters/tflint/plugin"
913
"github.com/terraform-linters/tflint/tflint"
@@ -12,6 +16,8 @@ import (
1216
func (cli *CLI) printVersion(opts Options) int {
1317
fmt.Fprintf(cli.outStream, "TFLint version %s\n", tflint.Version)
1418

19+
cli.printLatestReleaseVersion()
20+
1521
workingDirs, err := findWorkingDirs(opts)
1622
if err != nil {
1723
cli.formatter.Print(tflint.Issues{}, fmt.Errorf("Failed to find workspaces; %w", err), map[string][]byte{})
@@ -81,3 +87,35 @@ func getPluginVersions(opts Options) []string {
8187

8288
return versions
8389
}
90+
91+
// Checks GitHub releases and prints new version, if current version is outdated.
92+
// requires GitHub releases to follow semver.
93+
func (cli *CLI) printLatestReleaseVersion() {
94+
latest, err := getLatestVersion()
95+
if err != nil {
96+
cli.formatter.Print(tflint.Issues{}, fmt.Errorf("Failed to check updates; %w", err), map[string][]byte{})
97+
}
98+
latestVersion, err := version.NewSemver(*latest.Name)
99+
compare := tflint.Version.Compare(latestVersion)
100+
if compare < 0 {
101+
fmt.Fprintf(cli.outStream, "New version available: %s\n", *latest.HTMLURL)
102+
}
103+
}
104+
105+
func getLatestVersion() (*github.RepositoryRelease, error) {
106+
ghClient := github.NewClient(nil)
107+
releases, _, err := ghClient.Repositories.ListReleases(context.Background(),
108+
"terraform-linters", "tflint", &github.ListOptions{})
109+
if err != nil {
110+
return nil, err
111+
}
112+
113+
// GitHub sorts releases results. Select first non-prerelease version and return it.
114+
for i := range releases {
115+
release := releases[i]
116+
if !*release.Prerelease {
117+
return release, nil
118+
}
119+
}
120+
return nil, errors.New("not found")
121+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ require (
4949
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
5050
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
5151
github.com/golang/protobuf v1.5.2 // indirect
52-
github.com/google/go-querystring v1.0.0 // indirect
52+
github.com/google/go-querystring v1.1.0 // indirect
5353
github.com/googleapis/enterprise-certificate-proxy v0.2.0 // indirect
5454
github.com/googleapis/gax-go/v2 v2.7.0 // indirect
5555
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect

go.sum

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,9 @@ github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
304304
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
305305
github.com/google/go-github/v35 v35.3.0 h1:fU+WBzuukn0VssbayTT+Zo3/ESKX9JYWjbZTLOTEyho=
306306
github.com/google/go-github/v35 v35.3.0/go.mod h1:yWB7uCcVWaUbUP74Aq3whuMySRMatyRmq5U9FTNlbio=
307-
github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk=
308307
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
308+
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
309+
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
309310
github.com/google/martian v2.1.0+incompatible h1:/CP5g8u/VJHijgedC/Legn3BAbAaWPgecwXBIDzw5no=
310311
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
311312
github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=

0 commit comments

Comments
 (0)