Skip to content

Commit de3d482

Browse files
authored
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")

0 commit comments

Comments
 (0)