Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions cmd/version.go
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"
Expand All @@ -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{})
Expand Down Expand Up @@ -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() {
Copy link
Member

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.

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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Terraform prints, for example:

Your version of Terraform is out of date! The latest version
is 1.4.4. You can update by downloading from https://www.terraform.io/downloads.html

It would be nice to do something similar, namely:

  • The new version
  • The URL

You already have the latter here.

}
}

func getLatestVersion() (*github.RepositoryRelease, error) {
ghClient := github.NewClient(nil)
Copy link
Member

Choose a reason for hiding this comment

The 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 plugin.newGitHubClient.

releases, _, err := ghClient.Repositories.ListReleases(context.Background(),
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should use /releases/latest and not list releases:

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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for _, release := range

release := releases[i]
if !*release.Prerelease {
return release, nil
}
}
return nil, errors.New("not found")
}