@@ -21,72 +21,26 @@ import (
2121	"errors" 
2222	"fmt" 
2323	"io" 
24- 	"io/ioutil" 
2524	"log" 
2625	"net/http" 
2726	"os" 
2827	"path/filepath" 
29- 	"strings" 
30- 	"text/template" 
3128
3229	"github.com/magefile/mage/mg" 
3330	"github.com/magefile/mage/sh" 
3431)
3532
3633const  (
37- 	linterInstallURL              =  "https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh" 
38- 	linterInstallFilename         =  "./build/intall-golang-ci.sh" 
39- 	linterBinaryFilename          =  "./build/golangci-lint" 
40- 	linterVersion                 =  "v1.45.2" 
41- 	linterConfigFilename          =  "./.golangci.yml" 
42- 	linterConfigTemplateFilename  =  "./dev-tools/templates/.golangci.yml" 
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" 
4339)
4440
4541// Linter contains targets related to linting the Go code 
4642type  Linter  mg.Namespace 
4743
48- // UpdateGoVersion updates the linter configuration with the new version of Go. 
49- func  (Linter ) UpdateGoVersion () error  {
50- 	goVersionBytes , err  :=  ioutil .ReadFile (goVersionFilename )
51- 	if  err  !=  nil  {
52- 		return  fmt .Errorf ("failed to read the %q file: %w" , goVersionFilename , err )
53- 	}
54- 	goVersion  :=  strings .TrimSpace (string (goVersionBytes ))
55- 	log .Printf ("The Go version is %s\n " , goVersion )
56- 
57- 	templateContext  :=  struct { GoVersion  string  }{GoVersion : goVersion }
58- 	template , err  :=  template .ParseFiles (linterConfigTemplateFilename )
59- 	if  err  !=  nil  {
60- 		return  fmt .Errorf ("failed to read the template file %q: %w" , linterConfigTemplateFilename , err )
61- 	}
62- 
63- 	configFile , err  :=  os .OpenFile (linterConfigFilename , os .O_TRUNC | os .O_CREATE | os .O_WRONLY , 0700 )
64- 	if  err  !=  nil  {
65- 		return  fmt .Errorf ("failed to create/replace the linter config %q: %w" , linterConfigFilename , err )
66- 	}
67- 	defer  configFile .Close ()
68- 
69- 	warning  :=  fmt .Sprintf ("# DO NOT EDIT!\n # This file is a rendered template, the source can be found in %q\n #\n " , linterConfigTemplateFilename )
70- 	_ , err  =  configFile .WriteString (warning )
71- 	if  err  !=  nil  {
72- 		return  fmt .Errorf ("failed to write into the linter config %q: %w" , linterConfigFilename , err )
73- 	}
74- 
75- 	err  =  template .Execute (configFile , templateContext )
76- 	if  err  !=  nil  {
77- 		return  fmt .Errorf ("failed to execute the template %q: %w" , linterConfigTemplateFilename , err )
78- 	}
79- 
80- 	err  =  assertUnchanged (linterConfigFilename )
81- 	if  err  !=  nil  {
82- 		log .Printf ("Successfully updated the linter configuration %q to Go version %s, please commit the changes now" , linterConfigFilename , goVersion )
83- 	} else  {
84- 		log .Printf ("The linter configuration %q is up to date, no changes made" , linterConfigFilename )
85- 	}
86- 
87- 	return  nil 
88- }
89- 
9044// CheckConfig makes sure that the `.golangci.yml` does not have uncommitted changes 
9145func  (Linter ) CheckConfig () error  {
9246	err  :=  assertUnchanged (linterConfigFilename )
@@ -100,18 +54,28 @@ func (Linter) CheckConfig() error {
10054// using the official installation script downloaded from GitHub. 
10155// If the linter binary already exists does nothing. 
10256func  (Linter ) Install () error  {
57+ 	return  install (false )
58+ }
59+ 
60+ // ForceInstall force installs the linter regardless of whether it exists or not. 
61+ // Useful primarily when the linter version should be updated. 
62+ func  (Linter ) ForceInstall () error  {
63+ 	return  install (true )
64+ }
65+ 
66+ func  install (force  bool ) error  {
10367	dirPath  :=  filepath .Dir (linterBinaryFilename )
10468	err  :=  os .MkdirAll (dirPath , 0700 )
10569	if  err  !=  nil  {
10670		return  fmt .Errorf ("failed to create path %q: %w" , dirPath , err )
10771	}
10872
10973	_ , err  =  os .Stat (linterBinaryFilename )
110- 	if  err  ==  nil  {
74+ 	if  ! force   &&   err  ==  nil  {
11175		log .Println ("The linter has been already installed, skipping..." )
11276		return  nil 
11377	}
114- 	if  ! errors .Is (err , os .ErrNotExist ) {
78+ 	if  err   !=   nil   &&   ! errors .Is (err , os .ErrNotExist ) {
11579		return  fmt .Errorf ("failed check if file %q exists: %w" , linterBinaryFilename , err )
11680	}
11781
0 commit comments