-
Notifications
You must be signed in to change notification settings - Fork 357
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add remote version check #1735
base: master
Are you sure you want to change the base?
Add remote version check #1735
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,13 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"log" | ||
|
||
"github.com/google/go-github/v35/github" | ||
"github.com/hashicorp/go-version" | ||
"github.com/spf13/afero" | ||
"github.com/terraform-linters/tflint/plugin" | ||
"github.com/terraform-linters/tflint/tflint" | ||
|
@@ -12,6 +16,8 @@ import ( | |
func (cli *CLI) printVersion(opts Options) int { | ||
fmt.Fprintf(cli.outStream, "TFLint version %s\n", tflint.Version) | ||
|
||
cli.printLatestReleaseVersion() | ||
|
||
workingDirs, err := findWorkingDirs(opts) | ||
if err != nil { | ||
cli.formatter.Print(tflint.Issues{}, fmt.Errorf("Failed to find workspaces; %w", err), map[string][]byte{}) | ||
|
@@ -81,3 +87,38 @@ func getPluginVersions(opts Options) []string { | |
|
||
return versions | ||
} | ||
|
||
// Checks GitHub releases and prints new version, if current version is outdated. | ||
// requires GitHub releases to follow semver. | ||
func (cli *CLI) printLatestReleaseVersion() { | ||
latest, err := getLatestVersion() | ||
if err != nil { | ||
cli.formatter.Print(tflint.Issues{}, fmt.Errorf("Failed to check updates; %w", err), map[string][]byte{}) | ||
} | ||
latestVersion, err := version.NewSemver(*latest.Name) | ||
if err != nil { | ||
cli.formatter.Print(tflint.Issues{}, fmt.Errorf("Failed to parse version; %w", err), map[string][]byte{}) | ||
} | ||
compare := tflint.Version.Compare(latestVersion) | ||
if compare < 0 { | ||
fmt.Fprintf(cli.outStream, "New version available: %s\n", *latest.HTMLURL) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Terraform prints, for example:
It would be nice to do something similar, namely:
You already have the latter here. |
||
} | ||
} | ||
|
||
func getLatestVersion() (*github.RepositoryRelease, error) { | ||
ghClient := github.NewClient(nil) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs to be authenticated if a token is provided, for rate limiting purposes. See |
||
releases, _, err := ghClient.Repositories.ListReleases(context.Background(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Keep all args on one line or if that line gets too long each arg should be its own line. Mixing both style is awkward. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should use https://docs.github.com/en/rest/releases/releases?apiVersion=2022-11-28#get-the-latest-release That will handle excluding prereleases and drafts. Alternatively, if we had to use list, we should be fetching smaller pages than the 30 returned by default. |
||
"terraform-linters", "tflint", &github.ListOptions{}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// GitHub sorts releases results. Select first non-prerelease version and return it. | ||
for i := range releases { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
release := releases[i] | ||
if !*release.Prerelease { | ||
return release, nil | ||
} | ||
} | ||
return nil, errors.New("not found") | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There should be a way to disable this, e.g., for users who want to run TFLint offline. See https://developer.hashicorp.com/terraform/cli/commands#upgrade-and-security-bulletin-checks.