Skip to content

Commit de3d482

Browse files
authoredFeb 11, 2024
Add golangci-lint (#181)
* fix: toolchain not available After updating golang to 1.22 we have error: ``` go: download go1.22 for darwin/arm64: toolchain not available ``` golang/go#65568 Just set full version for golang to fix this issue * fix: add build tag to windows cmd goland added this automatically :) * feat: add golangci-lint to build Using golangci-link makes it easier to check the code and avoid a lot of problems * fix: linters warnings * fix: a more specific definition of exclusion for gocritic
1 parent 0e87ab3 commit de3d482

File tree

9 files changed

+111
-55
lines changed

9 files changed

+111
-55
lines changed
 

‎.github/workflows/golangci-lint.yml

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: golangci-lint
2+
on:
3+
push:
4+
branches:
5+
- '**'
6+
pull_request:
7+
branches:
8+
- '**'
9+
workflow_dispatch:
10+
11+
permissions:
12+
contents: read
13+
jobs:
14+
golangci:
15+
name: lint
16+
runs-on: ubuntu-latest
17+
steps:
18+
-
19+
uses: actions/checkout@v4
20+
-
21+
uses: kevincobain2000/action-gobrew@v2
22+
with:
23+
version: latest
24+
-
25+
uses: golangci/golangci-lint-action@v4
26+
with:
27+
version: latest

‎.golangci.yaml

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
linters:
2+
# Disable all linters.
3+
# Default: false
4+
disable-all: true
5+
# Enable specific linter
6+
# https://golangci-lint.run/usage/linters/#enabled-by-default
7+
enable:
8+
- errcheck
9+
- gosimple
10+
- govet
11+
- ineffassign
12+
- staticcheck
13+
- dupl
14+
- errorlint
15+
- exportloopref
16+
- goconst
17+
- gocritic
18+
- gocyclo
19+
- goprintffuncname
20+
- gosec
21+
- prealloc
22+
- revive
23+
- stylecheck
24+
- whitespace

‎cmd/gobrew/main.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func init() {
6767
// Check if the major version is 1 and the minor version is 21 or greater
6868
if majorVersionNum == 1 && minorVersionNum >= 21 {
6969
// Modify the versionArg to include ".0"
70-
versionArg = versionArg + ".0"
70+
versionArg += ".0"
7171
}
7272
}
7373
}
@@ -88,10 +88,10 @@ func main() {
8888

8989
config := gobrew.Config{
9090
RootDir: rootDir,
91-
RegistryPathUrl: registryPath,
92-
GobrewDownloadUrl: gobrew.DownloadUrl,
93-
GobrewTags: gobrew.TagsApi,
94-
GobrewVersionsUrl: gobrew.VersionsUrl,
91+
RegistryPathURL: registryPath,
92+
GobrewDownloadURL: gobrew.DownloadURL,
93+
GobrewTags: gobrew.TagsAPI,
94+
GobrewVersionsURL: gobrew.VersionsURL,
9595
}
9696

9797
gb := gobrew.NewGoBrew(config)
@@ -108,7 +108,7 @@ func main() {
108108
gb.ListRemoteVersions(true)
109109
case "install":
110110
gb.Install(versionArg)
111-
if gb.CurrentVersion() == "None" {
111+
if gb.CurrentVersion() == gobrew.NoneVersion {
112112
gb.Use(versionArg)
113113
}
114114
case "use":

‎cmd/gobrew/main_windows.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:build windows
2+
13
package main
24

35
const usageMsg = `

‎go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/kevincobain2000/gobrew
22

3-
go 1.22
3+
go 1.22.0
44

55
require (
66
github.com/Masterminds/semver v1.5.0

‎gobrew.go

+26-21
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,14 @@ import (
2020
const (
2121
goBrewDir string = ".gobrew"
2222
DefaultRegistryPath string = "https://go.dev/dl/"
23-
DownloadUrl string = "https://github.com/kevincobain2000/gobrew/releases/latest/download/"
24-
TagsApi = "https://raw.githubusercontent.com/kevincobain2000/gobrew/json/golang-tags.json"
25-
VersionsUrl string = "https://api.github.com/repos/kevincobain2000/gobrew/releases/latest"
23+
DownloadURL string = "https://github.com/kevincobain2000/gobrew/releases/latest/download/"
24+
TagsAPI = "https://raw.githubusercontent.com/kevincobain2000/gobrew/json/golang-tags.json"
25+
VersionsURL string = "https://api.github.com/repos/kevincobain2000/gobrew/releases/latest"
26+
)
27+
28+
const (
29+
NoneVersion = "None"
30+
ProgramName = "gobrew"
2631
)
2732

2833
// check GoBrew implement is Command interface
@@ -31,7 +36,7 @@ var _ Command = (*GoBrew)(nil)
3136
// Command ...
3237
type Command interface {
3338
ListVersions()
34-
ListRemoteVersions(print bool) map[string][]string
39+
ListRemoteVersions(bool) map[string][]string
3540
CurrentVersion() string
3641
Uninstall(version string)
3742
Install(version string) string
@@ -55,10 +60,10 @@ type GoBrew struct {
5560

5661
type Config struct {
5762
RootDir string
58-
RegistryPathUrl string
59-
GobrewDownloadUrl string
63+
RegistryPathURL string
64+
GobrewDownloadURL string
6065
GobrewTags string
61-
GobrewVersionsUrl string
66+
GobrewVersionsURL string
6267
}
6368

6469
// NewGoBrew instance
@@ -85,19 +90,19 @@ func (gb *GoBrew) Interactive(ask bool) {
8590
latestVersion := gb.getLatestVersion()
8691
latestMajorVersion := extractMajorVersion(latestVersion)
8792

88-
modVersion := "None"
93+
modVersion := NoneVersion
8994
if gb.hasModFile() {
9095
modVersion = gb.getModVersion()
9196
modVersion = extractMajorVersion(modVersion)
9297
}
9398

9499
fmt.Println()
95100

96-
if currentVersion == "None" {
101+
if currentVersion == NoneVersion {
97102
color.Warnln("GO Installed Version", ".......", currentVersion)
98103
} else {
99104
var labels []string
100-
if modVersion != "None" && currentMajorVersion != modVersion {
105+
if modVersion != NoneVersion && currentMajorVersion != modVersion {
101106
labels = append(labels, "not same as go.mod")
102107
}
103108
if currentVersion != latestVersion {
@@ -111,7 +116,7 @@ func (gb *GoBrew) Interactive(ask bool) {
111116
color.Successln("GO Installed Version", ".......", currentVersion+label)
112117
}
113118

114-
if modVersion != "None" && latestMajorVersion != modVersion {
119+
if modVersion != NoneVersion && latestMajorVersion != modVersion {
115120
label := " " + color.FgYellow.Render("(not latest)")
116121
color.Successln("GO go.mod Version", " .......", modVersion+label)
117122
} else {
@@ -121,7 +126,7 @@ func (gb *GoBrew) Interactive(ask bool) {
121126
color.Successln("GO Latest Version", " .......", latestVersion)
122127
fmt.Println()
123128

124-
if currentVersion == "None" {
129+
if currentVersion == NoneVersion {
125130
color.Warnln("GO is not installed.")
126131
c := true
127132
if ask {
@@ -133,7 +138,7 @@ func (gb *GoBrew) Interactive(ask bool) {
133138
return
134139
}
135140

136-
if modVersion != "None" && currentMajorVersion != modVersion {
141+
if modVersion != NoneVersion && currentMajorVersion != modVersion {
137142
color.Warnf("GO Installed Version (%s) and go.mod Version (%s) are different.\n", currentMajorVersion, modVersion)
138143
c := true
139144
if ask {
@@ -268,12 +273,12 @@ func (gb *GoBrew) ListRemoteVersions(print bool) map[string][]string {
268273
func (gb *GoBrew) CurrentVersion() string {
269274
fp, err := evalSymlinks(gb.currentBinDir)
270275
if err != nil {
271-
return "None"
276+
return NoneVersion
272277
}
273278
version := strings.TrimSuffix(fp, filepath.Join("go", "bin"))
274279
version = filepath.Base(version)
275280
if version == "." {
276-
return "None"
281+
return NoneVersion
277282
}
278283
return version
279284
}
@@ -294,7 +299,7 @@ func (gb *GoBrew) Uninstall(version string) {
294299

295300
// Install the given version of go
296301
func (gb *GoBrew) Install(version string) string {
297-
if version == "" || version == "None" {
302+
if version == "" || version == NoneVersion {
298303
color.Errorln("[Error] No version provided")
299304
os.Exit(1)
300305
}
@@ -337,11 +342,11 @@ func (gb *GoBrew) Upgrade(currentVersion string) {
337342
return
338343
}
339344

340-
mkdirTemp, _ := os.MkdirTemp("", "gobrew")
341-
tmpFile := filepath.Join(mkdirTemp, "gobrew"+fileExt)
342-
downloadUrl, _ := url.JoinPath(gb.GobrewDownloadUrl, "gobrew-"+gb.getArch()+fileExt)
345+
mkdirTemp, _ := os.MkdirTemp("", ProgramName)
346+
tmpFile := filepath.Join(mkdirTemp, ProgramName+fileExt)
347+
downloadURL, _ := url.JoinPath(gb.GobrewDownloadURL, "gobrew-"+gb.getArch()+fileExt)
343348
utils.CheckError(
344-
utils.DownloadWithProgress(downloadUrl, "gobrew"+fileExt, mkdirTemp),
349+
utils.DownloadWithProgress(downloadURL, ProgramName+fileExt, mkdirTemp),
345350
"[Error] Download GoBrew failed")
346351

347352
source, err := os.Open(tmpFile)
@@ -351,7 +356,7 @@ func (gb *GoBrew) Upgrade(currentVersion string) {
351356
utils.CheckError(os.Remove(source.Name()), "==> [Error] Cannot remove tmp file:")
352357
}(source)
353358

354-
goBrewFile := filepath.Join(gb.installDir, "bin", "gobrew"+fileExt)
359+
goBrewFile := filepath.Join(gb.installDir, "bin", ProgramName+fileExt)
355360
removeFile(goBrewFile)
356361
destination, err := os.Create(goBrewFile)
357362
utils.CheckError(err, "==> [Error] Cannot open file")

‎gobrew_test.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ import (
1313

1414
func setupGobrew(t *testing.T, ts *httptest.Server) GoBrew {
1515
tags, _ := url.JoinPath(ts.URL, "golang-tags.json")
16-
versionUrl, _ := url.JoinPath(ts.URL, "latest")
16+
versionURL, _ := url.JoinPath(ts.URL, "latest")
1717
config := Config{
1818
RootDir: t.TempDir(),
19-
RegistryPathUrl: ts.URL,
20-
GobrewDownloadUrl: ts.URL,
19+
RegistryPathURL: ts.URL,
20+
GobrewDownloadURL: ts.URL,
2121
GobrewTags: tags,
22-
GobrewVersionsUrl: versionUrl,
22+
GobrewVersionsURL: versionURL,
2323
}
2424
gb := NewGoBrew(config)
2525
return gb
@@ -59,7 +59,7 @@ func TestUpgrade(t *testing.T) {
5959
binaryDir := filepath.Join(gb.installDir, "bin")
6060
_ = os.MkdirAll(binaryDir, os.ModePerm)
6161

62-
baseName := "gobrew" + fileExt
62+
baseName := ProgramName + fileExt
6363
binaryFile := filepath.Join(binaryDir, baseName)
6464

6565
if oldFile, err := os.Create(binaryFile); err == nil {
@@ -84,7 +84,7 @@ func TestDoNotUpgradeLatestVersion(t *testing.T) {
8484
binaryDir := filepath.Join(gb.installDir, "bin")
8585
_ = os.MkdirAll(binaryDir, os.ModePerm)
8686

87-
baseName := "gobrew" + fileExt
87+
baseName := ProgramName + fileExt
8888
binaryFile := filepath.Join(binaryDir, baseName)
8989

9090
currentVersion := gb.getGobrewVersion()
@@ -109,7 +109,7 @@ func TestInteractive(t *testing.T) {
109109

110110
currentVersion := gb.CurrentVersion()
111111
latestVersion := gb.getLatestVersion()
112-
assert.Equal(t, "None", currentVersion)
112+
assert.Equal(t, NoneVersion, currentVersion)
113113
assert.NotEqual(t, currentVersion, latestVersion)
114114

115115
gb.Interactive(false)
@@ -148,7 +148,7 @@ func TestGoBrew_CurrentVersion(t *testing.T) {
148148
ts := httptest.NewServer(http.FileServer(http.Dir("testdata")))
149149
defer ts.Close()
150150
gb := setupGobrew(t, ts)
151-
assert.Equal(t, true, gb.CurrentVersion() == "None")
151+
assert.Equal(t, true, gb.CurrentVersion() == NoneVersion)
152152
gb.Install("1.19")
153153
gb.Use("1.19")
154154
assert.Equal(t, true, gb.CurrentVersion() == "1.19")

‎helpers.go

+17-18
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func (gb *GoBrew) getGroupedVersion(versions []string, print bool) map[string][]
7272
sort.Sort(semver.Collection(versionsSemantic))
7373

7474
// match 1.0.0 or 2.0.0
75-
reTopVersion, _ := regexp.Compile("[0-9]+.0.0")
75+
reTopVersion := regexp.MustCompile("[0-9]+.0.0")
7676

7777
for _, versionSemantic := range versionsSemantic {
7878
maxPerLine := 0
@@ -101,7 +101,6 @@ func (gb *GoBrew) getGroupedVersion(versions []string, print bool) map[string][]
101101
if v, err := semver.NewVersion(r); err == nil {
102102
groupedVersionsSemantic = append(groupedVersionsSemantic, v)
103103
}
104-
105104
}
106105
// sort semantic versions
107106
sort.Sort(semver.Collection(groupedVersionsSemantic))
@@ -169,7 +168,7 @@ func (gb *GoBrew) cleanDownloadsDir() {
169168
}
170169

171170
func (gb *GoBrew) judgeVersion(version string) string {
172-
judgedVersion := "None"
171+
judgedVersion := NoneVersion
173172
rcBetaOk := false
174173
reRcOrBeta := regexp.MustCompile("beta.*|rc.*")
175174

@@ -194,7 +193,7 @@ func (gb *GoBrew) judgeVersion(version string) string {
194193
modVersion := gb.getModVersion()
195194
// if modVersion is like 1.19, 1.20, 1.21 then appened @latest to it
196195
if strings.Count(modVersion, ".") == 1 {
197-
modVersion = modVersion + "@latest"
196+
modVersion += "@latest"
198197
}
199198
return gb.judgeVersion(modVersion)
200199
}
@@ -214,7 +213,7 @@ func (gb *GoBrew) judgeVersion(version string) string {
214213
}
215214
}
216215
if len(versionsSemantic) == 0 {
217-
return "None"
216+
return NoneVersion
218217
}
219218

220219
// sort semantic versions
@@ -225,7 +224,7 @@ func (gb *GoBrew) judgeVersion(version string) string {
225224
// get last element
226225
if version == "dev-latest" {
227226
if len(judgedVersions) == 0 {
228-
return "None"
227+
return NoneVersion
229228
}
230229
// Filter versions containing "rc" or "beta"
231230
filteredVersions := gb.filterVersions(judgedVersions, []string{"rc", "beta"})
@@ -252,7 +251,7 @@ func (gb *GoBrew) judgeVersion(version string) string {
252251
return gb.judgeVersion(latest)
253252
}
254253

255-
if judgedVersion != "None" {
254+
if judgedVersion != NoneVersion {
256255
// check if judgedVersion is in the groupedVersions
257256
if _, ok := groupedVersions[judgedVersion]; ok {
258257
// get last item in the groupedVersions excluding rc and beta
@@ -274,7 +273,7 @@ func (gb *GoBrew) judgeVersion(version string) string {
274273
}
275274

276275
func (gb *GoBrew) hasModFile() bool {
277-
modFilePath := filepath.Join("go.mod")
276+
modFilePath := "go.mod"
278277
_, err := os.Stat(modFilePath)
279278
if err == nil {
280279
return true
@@ -289,10 +288,10 @@ func (gb *GoBrew) hasModFile() bool {
289288
// Do not use go to get the version as go list -m -f '{{.GoVersion}}'
290289
// Because go might not be installed
291290
func (gb *GoBrew) getModVersion() string {
292-
modFilePath := filepath.Join("go.mod")
291+
modFilePath := "go.mod"
293292
modFile, err := os.Open(modFilePath)
294293
if err != nil {
295-
return "None"
294+
return NoneVersion
296295
}
297296
defer func(modFile *os.File) {
298297
_ = modFile.Close()
@@ -308,9 +307,9 @@ func (gb *GoBrew) getModVersion() string {
308307

309308
if err = scanner.Err(); err != nil {
310309
color.Errorln(err)
311-
os.Exit(1)
310+
os.Exit(1) // nolint:gocritic
312311
}
313-
return "None"
312+
return NoneVersion
314313
}
315314

316315
func (gb *GoBrew) mkDirs(version string) {
@@ -342,10 +341,10 @@ func (gb *GoBrew) filterVersions(versions []string, contains []string) []string
342341
func (gb *GoBrew) downloadAndExtract(version string) {
343342
tarName := "go" + version + "." + gb.getArch() + tarNameExt
344343

345-
downloadURL, _ := url.JoinPath(gb.RegistryPathUrl, tarName)
344+
downloadURL, _ := url.JoinPath(gb.RegistryPathURL, tarName)
346345
color.Infoln("==> [Info] Downloading from:", downloadURL)
347346

348-
dstDownloadDir := filepath.Join(gb.downloadsDir)
347+
dstDownloadDir := gb.downloadsDir
349348
color.Infoln("==> [Info] Downloading to:", dstDownloadDir)
350349
err := utils.DownloadWithProgress(downloadURL, tarName, dstDownloadDir)
351350

@@ -385,7 +384,7 @@ func (gb *GoBrew) changeSymblinkGo(version string) {
385384
}
386385

387386
func (gb *GoBrew) getGobrewVersion() string {
388-
data := doRequest(gb.GobrewVersionsUrl)
387+
data := doRequest(gb.GobrewVersionsURL)
389388
if len(data) == 0 {
390389
return ""
391390
}
@@ -426,7 +425,7 @@ func doRequest(url string) (data []byte) {
426425
request, err := http.NewRequest("GET", url, nil)
427426
utils.CheckError(err, "==> [Error] Cannot create request")
428427

429-
request.Header.Set("User-Agent", "gobrew")
428+
request.Header.Set("User-Agent", ProgramName)
430429

431430
response, err := client.Do(request)
432431
utils.CheckError(err, "==> [Error] Cannot get response")
@@ -438,12 +437,12 @@ func doRequest(url string) (data []byte) {
438437
if response.StatusCode == http.StatusTooManyRequests ||
439438
response.StatusCode == http.StatusForbidden {
440439
color.Errorln("==> [Error] Rate limit exhausted")
441-
os.Exit(1)
440+
os.Exit(1) // nolint:gocritic
442441
}
443442

444443
if response.StatusCode != http.StatusOK {
445444
color.Errorln("==> [Error] Cannot read response:", response.Status)
446-
os.Exit(1)
445+
os.Exit(1) // nolint:gocritic
447446
}
448447

449448
data, err = io.ReadAll(response.Body)

‎helpers_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ func TestJudgeVersion(t *testing.T) {
6262
gb := setupGobrew(t, ts)
6363
version := gb.judgeVersion(test.version)
6464
assert.Equal(t, test.wantVersion, version)
65-
6665
})
6766
}
6867
t.Log("test finished")

0 commit comments

Comments
 (0)
Please sign in to comment.