Skip to content

Commit

Permalink
Auto-install the correct linter version. (#73)
Browse files Browse the repository at this point in the history
* Auto-install the correct lint version.

Also add a version command to install and inspect the version of the
linter in use.

* Prep changelog for v0.2.11
  • Loading branch information
cmacknz authored Aug 10, 2022
1 parent 211dd3d commit 2397bda
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ This project adheres to [Semantic Versioning](http://semver.org/).

### Fixed

## [0.2.11]

### Changed

- The mage linter targets now automatically upgrades the linter when necessary. #73

## [0.2.10]

### Added
Expand Down
39 changes: 24 additions & 15 deletions dev-tools/mage/linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,15 @@ import (
)

const (
linterInstallURL = "https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh"
linterInstallFilename = "./build/install-golang-ci.sh"
linterBinaryFilename = "./build/golangci-lint"
linterVersion = "v1.47.2"
linterConfigFilename = "./.golangci.yml"
linterVersion = "v1.47.2"
linterInstallURL = "https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh"
)

var (
linterConfigFilename = filepath.Join(".", ".golangci.yml")
linterInstallDir = filepath.Join(".", "build")
linterInstallFile = filepath.Join(linterInstallDir, "install-golang-ci.sh")
linterBinaryFile = filepath.Join(linterInstallDir, linterVersion, "golangci-lint")
)

// Linter contains targets related to linting the Go code
Expand All @@ -58,32 +62,31 @@ func (Linter) Install() error {
}

// ForceInstall force installs the linter regardless of whether it exists or not.
// Useful primarily when the linter version should be updated.
func (Linter) ForceInstall() error {
return install(true)
}

func install(force bool) error {
dirPath := filepath.Dir(linterBinaryFilename)
dirPath := filepath.Dir(linterBinaryFile)
err := os.MkdirAll(dirPath, 0700)
if err != nil {
return fmt.Errorf("failed to create path %q: %w", dirPath, err)
}

_, err = os.Stat(linterBinaryFilename)
_, err = os.Stat(linterBinaryFile)
if !force && err == nil {
log.Println("The linter has been already installed, skipping...")
return nil
}
if err != nil && !errors.Is(err, os.ErrNotExist) {
return fmt.Errorf("failed check if file %q exists: %w", linterBinaryFilename, err)
return fmt.Errorf("failed check if file %q exists: %w", linterBinaryFile, err)
}

log.Println("Preparing the installation script file...")

installScript, err := os.OpenFile(linterInstallFilename, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0700)
installScript, err := os.OpenFile(linterInstallFile, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0700)
if err != nil {
return fmt.Errorf("failed to create file %q: %w", linterInstallFilename, err)
return fmt.Errorf("failed to create file %q: %w", linterInstallFile, err)
}
defer installScript.Close()

Expand All @@ -103,17 +106,17 @@ func install(force bool) error {

err = installScript.Close() // otherwise we cannot run the script
if err != nil {
return fmt.Errorf("failed to close file %q: %w", linterInstallFilename, err)
return fmt.Errorf("failed to close file %q: %w", linterInstallFile, err)
}

binaryDir := filepath.Dir(linterBinaryFilename)
binaryDir := filepath.Dir(linterBinaryFile)
err = os.MkdirAll(binaryDir, 0700)
if err != nil {
return fmt.Errorf("cannot create path %q: %w", binaryDir, err)
}

// there must be no space after `-b`, otherwise the script does not work correctly ¯\_(ツ)_/¯
return sh.Run(linterInstallFilename, "-b"+binaryDir, linterVersion)
return sh.Run(linterInstallFile, "-b"+binaryDir, linterVersion)
}

// All runs the linter against the entire codebase
Expand All @@ -122,6 +125,12 @@ func (l Linter) All() error {
return runLinter()
}

// Prints the version of the linter in use.
func (l Linter) Version() error {
mg.Deps(l.Install)
return runLinter("--version")
}

// LastChange runs the linter against all files changed since the fork point from `main`.
// If the current branch is `main` then runs against the files changed in the last commit.
func (l Linter) LastChange() error {
Expand Down Expand Up @@ -162,5 +171,5 @@ func runLinter(runFlags ...string) error {
args = append(args, "-c", linterConfigFilename)
args = append(args, "./...")

return runWithStdErr(linterBinaryFilename, args...)
return runWithStdErr(linterBinaryFile, args...)
}

0 comments on commit 2397bda

Please sign in to comment.