@@ -31,11 +31,15 @@ import (
3131)
3232
3333const (
34- linterInstallURL = "https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh"
35- linterInstallFilename = "./build/install-golang-ci.sh"
36- linterBinaryFilename = "./build/golangci-lint"
37- linterVersion = "v1.47.2"
38- linterConfigFilename = "./.golangci.yml"
34+ linterVersion = "v1.47.2"
35+ linterInstallURL = "https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh"
36+ )
37+
38+ var (
39+ linterConfigFilename = filepath .Join ("." , ".golangci.yml" )
40+ linterInstallDir = filepath .Join ("." , "build" )
41+ linterInstallFile = filepath .Join (linterInstallDir , "install-golang-ci.sh" )
42+ linterBinaryFile = filepath .Join (linterInstallDir , linterVersion , "golangci-lint" )
3943)
4044
4145// Linter contains targets related to linting the Go code
@@ -58,32 +62,31 @@ func (Linter) Install() error {
5862}
5963
6064// ForceInstall force installs the linter regardless of whether it exists or not.
61- // Useful primarily when the linter version should be updated.
6265func (Linter ) ForceInstall () error {
6366 return install (true )
6467}
6568
6669func install (force bool ) error {
67- dirPath := filepath .Dir (linterBinaryFilename )
70+ dirPath := filepath .Dir (linterBinaryFile )
6871 err := os .MkdirAll (dirPath , 0700 )
6972 if err != nil {
7073 return fmt .Errorf ("failed to create path %q: %w" , dirPath , err )
7174 }
7275
73- _ , err = os .Stat (linterBinaryFilename )
76+ _ , err = os .Stat (linterBinaryFile )
7477 if ! force && err == nil {
7578 log .Println ("The linter has been already installed, skipping..." )
7679 return nil
7780 }
7881 if err != nil && ! errors .Is (err , os .ErrNotExist ) {
79- return fmt .Errorf ("failed check if file %q exists: %w" , linterBinaryFilename , err )
82+ return fmt .Errorf ("failed check if file %q exists: %w" , linterBinaryFile , err )
8083 }
8184
8285 log .Println ("Preparing the installation script file..." )
8386
84- installScript , err := os .OpenFile (linterInstallFilename , os .O_TRUNC | os .O_CREATE | os .O_WRONLY , 0700 )
87+ installScript , err := os .OpenFile (linterInstallFile , os .O_TRUNC | os .O_CREATE | os .O_WRONLY , 0700 )
8588 if err != nil {
86- return fmt .Errorf ("failed to create file %q: %w" , linterInstallFilename , err )
89+ return fmt .Errorf ("failed to create file %q: %w" , linterInstallFile , err )
8790 }
8891 defer installScript .Close ()
8992
@@ -103,17 +106,17 @@ func install(force bool) error {
103106
104107 err = installScript .Close () // otherwise we cannot run the script
105108 if err != nil {
106- return fmt .Errorf ("failed to close file %q: %w" , linterInstallFilename , err )
109+ return fmt .Errorf ("failed to close file %q: %w" , linterInstallFile , err )
107110 }
108111
109- binaryDir := filepath .Dir (linterBinaryFilename )
112+ binaryDir := filepath .Dir (linterBinaryFile )
110113 err = os .MkdirAll (binaryDir , 0700 )
111114 if err != nil {
112115 return fmt .Errorf ("cannot create path %q: %w" , binaryDir , err )
113116 }
114117
115118 // there must be no space after `-b`, otherwise the script does not work correctly ¯\_(ツ)_/¯
116- return sh .Run (linterInstallFilename , "-b" + binaryDir , linterVersion )
119+ return sh .Run (linterInstallFile , "-b" + binaryDir , linterVersion )
117120}
118121
119122// All runs the linter against the entire codebase
@@ -122,6 +125,12 @@ func (l Linter) All() error {
122125 return runLinter ()
123126}
124127
128+ // Prints the version of the linter in use.
129+ func (l Linter ) Version () error {
130+ mg .Deps (l .Install )
131+ return runLinter ("--version" )
132+ }
133+
125134// LastChange runs the linter against all files changed since the fork point from `main`.
126135// If the current branch is `main` then runs against the files changed in the last commit.
127136func (l Linter ) LastChange () error {
@@ -162,5 +171,5 @@ func runLinter(runFlags ...string) error {
162171 args = append (args , "-c" , linterConfigFilename )
163172 args = append (args , "./..." )
164173
165- return runWithStdErr (linterBinaryFilename , args ... )
174+ return runWithStdErr (linterBinaryFile , args ... )
166175}
0 commit comments