Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add golangci-lint #181

Merged
merged 5 commits into from
Feb 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: golangci-lint
on:
push:
branches:
- '**'
pull_request:
branches:
- '**'
workflow_dispatch:

permissions:
contents: read
jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
-
uses: actions/checkout@v4
-
uses: kevincobain2000/action-gobrew@v2
with:
version: latest
-
uses: golangci/golangci-lint-action@v4
with:
version: latest
24 changes: 24 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
linters:
# Disable all linters.
# Default: false
disable-all: true
# Enable specific linter
# https://golangci-lint.run/usage/linters/#enabled-by-default
enable:
- errcheck
- gosimple
- govet
- ineffassign
- staticcheck
- dupl
- errorlint
- exportloopref
- goconst
- gocritic
- gocyclo
- goprintffuncname
- gosec
- prealloc
- revive
- stylecheck
- whitespace
12 changes: 6 additions & 6 deletions cmd/gobrew/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func init() {
// Check if the major version is 1 and the minor version is 21 or greater
if majorVersionNum == 1 && minorVersionNum >= 21 {
// Modify the versionArg to include ".0"
versionArg = versionArg + ".0"
versionArg += ".0"
}
}
}
Expand All @@ -88,10 +88,10 @@ func main() {

config := gobrew.Config{
RootDir: rootDir,
RegistryPathUrl: registryPath,
GobrewDownloadUrl: gobrew.DownloadUrl,
GobrewTags: gobrew.TagsApi,
GobrewVersionsUrl: gobrew.VersionsUrl,
RegistryPathURL: registryPath,
GobrewDownloadURL: gobrew.DownloadURL,
GobrewTags: gobrew.TagsAPI,
GobrewVersionsURL: gobrew.VersionsURL,
}

gb := gobrew.NewGoBrew(config)
Expand All @@ -108,7 +108,7 @@ func main() {
gb.ListRemoteVersions(true)
case "install":
gb.Install(versionArg)
if gb.CurrentVersion() == "None" {
if gb.CurrentVersion() == gobrew.NoneVersion {
gb.Use(versionArg)
}
case "use":
Expand Down
2 changes: 2 additions & 0 deletions cmd/gobrew/main_windows.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build windows

package main

const usageMsg = `
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/kevincobain2000/gobrew

go 1.22
go 1.22.0

require (
github.com/Masterminds/semver v1.5.0
Expand Down
47 changes: 26 additions & 21 deletions gobrew.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,14 @@ import (
const (
goBrewDir string = ".gobrew"
DefaultRegistryPath string = "https://go.dev/dl/"
DownloadUrl string = "https://github.com/kevincobain2000/gobrew/releases/latest/download/"
TagsApi = "https://raw.githubusercontent.com/kevincobain2000/gobrew/json/golang-tags.json"
VersionsUrl string = "https://api.github.com/repos/kevincobain2000/gobrew/releases/latest"
DownloadURL string = "https://github.com/kevincobain2000/gobrew/releases/latest/download/"
TagsAPI = "https://raw.githubusercontent.com/kevincobain2000/gobrew/json/golang-tags.json"
VersionsURL string = "https://api.github.com/repos/kevincobain2000/gobrew/releases/latest"
)

const (
NoneVersion = "None"
ProgramName = "gobrew"
)

// check GoBrew implement is Command interface
Expand All @@ -31,7 +36,7 @@ var _ Command = (*GoBrew)(nil)
// Command ...
type Command interface {
ListVersions()
ListRemoteVersions(print bool) map[string][]string
ListRemoteVersions(bool) map[string][]string
CurrentVersion() string
Uninstall(version string)
Install(version string) string
Expand All @@ -55,10 +60,10 @@ type GoBrew struct {

type Config struct {
RootDir string
RegistryPathUrl string
GobrewDownloadUrl string
RegistryPathURL string
GobrewDownloadURL string
GobrewTags string
GobrewVersionsUrl string
GobrewVersionsURL string
}

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

modVersion := "None"
modVersion := NoneVersion
if gb.hasModFile() {
modVersion = gb.getModVersion()
modVersion = extractMajorVersion(modVersion)
}

fmt.Println()

if currentVersion == "None" {
if currentVersion == NoneVersion {
color.Warnln("GO Installed Version", ".......", currentVersion)
} else {
var labels []string
if modVersion != "None" && currentMajorVersion != modVersion {
if modVersion != NoneVersion && currentMajorVersion != modVersion {
labels = append(labels, "not same as go.mod")
}
if currentVersion != latestVersion {
Expand All @@ -111,7 +116,7 @@ func (gb *GoBrew) Interactive(ask bool) {
color.Successln("GO Installed Version", ".......", currentVersion+label)
}

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

if currentVersion == "None" {
if currentVersion == NoneVersion {
color.Warnln("GO is not installed.")
c := true
if ask {
Expand All @@ -133,7 +138,7 @@ func (gb *GoBrew) Interactive(ask bool) {
return
}

if modVersion != "None" && currentMajorVersion != modVersion {
if modVersion != NoneVersion && currentMajorVersion != modVersion {
color.Warnf("GO Installed Version (%s) and go.mod Version (%s) are different.\n", currentMajorVersion, modVersion)
c := true
if ask {
Expand Down Expand Up @@ -268,12 +273,12 @@ func (gb *GoBrew) ListRemoteVersions(print bool) map[string][]string {
func (gb *GoBrew) CurrentVersion() string {
fp, err := evalSymlinks(gb.currentBinDir)
if err != nil {
return "None"
return NoneVersion
}
version := strings.TrimSuffix(fp, filepath.Join("go", "bin"))
version = filepath.Base(version)
if version == "." {
return "None"
return NoneVersion
}
return version
}
Expand All @@ -294,7 +299,7 @@ func (gb *GoBrew) Uninstall(version string) {

// Install the given version of go
func (gb *GoBrew) Install(version string) string {
if version == "" || version == "None" {
if version == "" || version == NoneVersion {
color.Errorln("[Error] No version provided")
os.Exit(1)
}
Expand Down Expand Up @@ -337,11 +342,11 @@ func (gb *GoBrew) Upgrade(currentVersion string) {
return
}

mkdirTemp, _ := os.MkdirTemp("", "gobrew")
tmpFile := filepath.Join(mkdirTemp, "gobrew"+fileExt)
downloadUrl, _ := url.JoinPath(gb.GobrewDownloadUrl, "gobrew-"+gb.getArch()+fileExt)
mkdirTemp, _ := os.MkdirTemp("", ProgramName)
tmpFile := filepath.Join(mkdirTemp, ProgramName+fileExt)
downloadURL, _ := url.JoinPath(gb.GobrewDownloadURL, "gobrew-"+gb.getArch()+fileExt)
utils.CheckError(
utils.DownloadWithProgress(downloadUrl, "gobrew"+fileExt, mkdirTemp),
utils.DownloadWithProgress(downloadURL, ProgramName+fileExt, mkdirTemp),
"[Error] Download GoBrew failed")

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

goBrewFile := filepath.Join(gb.installDir, "bin", "gobrew"+fileExt)
goBrewFile := filepath.Join(gb.installDir, "bin", ProgramName+fileExt)
removeFile(goBrewFile)
destination, err := os.Create(goBrewFile)
utils.CheckError(err, "==> [Error] Cannot open file")
Expand Down
16 changes: 8 additions & 8 deletions gobrew_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import (

func setupGobrew(t *testing.T, ts *httptest.Server) GoBrew {
tags, _ := url.JoinPath(ts.URL, "golang-tags.json")
versionUrl, _ := url.JoinPath(ts.URL, "latest")
versionURL, _ := url.JoinPath(ts.URL, "latest")
config := Config{
RootDir: t.TempDir(),
RegistryPathUrl: ts.URL,
GobrewDownloadUrl: ts.URL,
RegistryPathURL: ts.URL,
GobrewDownloadURL: ts.URL,
GobrewTags: tags,
GobrewVersionsUrl: versionUrl,
GobrewVersionsURL: versionURL,
}
gb := NewGoBrew(config)
return gb
Expand Down Expand Up @@ -59,7 +59,7 @@ func TestUpgrade(t *testing.T) {
binaryDir := filepath.Join(gb.installDir, "bin")
_ = os.MkdirAll(binaryDir, os.ModePerm)

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

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

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

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

currentVersion := gb.CurrentVersion()
latestVersion := gb.getLatestVersion()
assert.Equal(t, "None", currentVersion)
assert.Equal(t, NoneVersion, currentVersion)
assert.NotEqual(t, currentVersion, latestVersion)

gb.Interactive(false)
Expand Down Expand Up @@ -148,7 +148,7 @@ func TestGoBrew_CurrentVersion(t *testing.T) {
ts := httptest.NewServer(http.FileServer(http.Dir("testdata")))
defer ts.Close()
gb := setupGobrew(t, ts)
assert.Equal(t, true, gb.CurrentVersion() == "None")
assert.Equal(t, true, gb.CurrentVersion() == NoneVersion)
gb.Install("1.19")
gb.Use("1.19")
assert.Equal(t, true, gb.CurrentVersion() == "1.19")
Expand Down
Loading
Loading