Skip to content

Commit 2854a6b

Browse files
authored
Merge pull request #756 from yorukot/golangci_lint_fixes
fix: Add some of the remaining linter and fix errors
2 parents dcafa86 + 342c390 commit 2854a6b

40 files changed

+460
-169
lines changed

.golangci.yaml

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -278,12 +278,10 @@ linters:
278278
- errname # checks that sentinel errors are prefixed with the Err and error types are suffixed with the Error
279279
# Todo enable
280280
#- errorlint # finds code that will cause problems with the error wrapping scheme introduced in Go 1.13
281-
# Todo enable - Need on small fix
282-
#- exhaustive # checks exhaustiveness of enum switch statements
281+
- exhaustive # checks exhaustiveness of enum switch statements
283282
- exptostd # detects functions from golang.org/x/exp/ that can be replaced by std functions
284283
- fatcontext # detects nested contexts in loops
285-
# Todo enable - no . this blocks only fmt.Print family and is not useful right now
286-
# - forbidigo # forbids identifiers
284+
# - forbidigo # forbids identifiers # Not needed. this blocks only fmt.Print family and is not useful right now
287285
# Todo enable - need to make functions shorter
288286
#- funlen # tool for detection of long functions
289287
- gocheckcompilerdirectives # validates go compiler directive comments (//go:)
@@ -293,16 +291,14 @@ linters:
293291
- gochecksumtype # checks exhaustiveness on Go "sum types"
294292
# Todo enable - Need to refactor many functions
295293
# - gocognit # computes and checks the cognitive complexity of functions
296-
# Todo enable - Need a few changes - easy
297-
# - goconst # finds repeated strings that could be replaced by a constant
294+
- goconst # finds repeated strings that could be replaced by a constant
298295
# Todo enable - Some few changes. There are good suggestions
299296
# - gocritic # provides diagnostics that check for bugs, performance and style issues
300297
# Todo enable
301298
#- gocyclo # computes and checks the cyclomatic complexity of functions
302299
# Todo enable - too much for us
303300
#- godot # checks if comments end in a period
304-
# Todo enable - one small fix
305-
#- goimports # in addition to fixing imports, goimports also formats your code in the same style as gofmt
301+
- goimports # in addition to fixing imports, goimports also formats your code in the same style as gofmt
306302
- gomoddirectives # manages the use of 'replace', 'retract', and 'excludes' directives in go.mod
307303
- goprintffuncname # checks that printf-like functions are named with f at the end
308304
# Todo enable - ignore for a few int overflow, and fix file permissions
@@ -325,15 +321,12 @@ linters:
325321
- nilerr # finds the code that returns nil even if it checks that the error is not nil
326322
- nilnesserr # reports that it checks for err != nil, but it returns a different nil value error (powered by nilness and nilerr)
327323
- nilnil # checks that there is no simultaneous return of nil error and an invalid value
328-
# Todo enable : Need to fix one client.Get usage
329-
# - noctx # finds sending http request without context.Context
330-
# Todo enable : Needs some fixes
331-
# - nolintlint # reports ill-formed or insufficient nolint directives
324+
- noctx # finds sending http request without context.Context
325+
- nolintlint # reports ill-formed or insufficient nolint directives
332326
# Todo enable - maybe too strict for us
333327
# - nonamedreturns # reports all named returns
334328
- nosprintfhostport # checks for misuse of Sprintf to construct a host with port in a URL
335-
# Todo enable : One small fix
336-
# - perfsprint # checks that fmt.Sprintf can be replaced with a faster alternative
329+
- perfsprint # checks that fmt.Sprintf can be replaced with a faster alternative
337330
- predeclared # finds code that shadows one of Go's predeclared identifiers
338331
- promlinter # checks Prometheus metrics naming via promlint
339332
- protogetter # reports direct reads from proto message fields when getters should be used
@@ -351,13 +344,11 @@ linters:
351344
# Todo enable : Many changes, but good changes.
352345
# - stylecheck # is a replacement for golint
353346
- testableexamples # checks if examples are testable (have an expected output)
354-
# Todo enable - some test bugs. To be fixed
355-
# - testifylint # checks usage of github.com/stretchr/testify
347+
- testifylint # checks usage of github.com/stretchr/testify
356348
# Todo enable - Might be too strict for us
357349
# - testpackage # makes you use a separate _test package
358350
- tparallel # detects inappropriate usage of t.Parallel() method in your Go test codes
359-
# Todo enable - One small change. Good find.
360-
# - unconvert # removes unnecessary type conversions
351+
- unconvert # removes unnecessary type conversions
361352
- unparam # reports unused function parameters
362353
- usestdlibvars # detects the possibility to use variables/constants from the Go standard library
363354
- usetesting # reports uses of functions with replacement inside the testing package
@@ -403,7 +394,7 @@ linters:
403394
# - err113 # [too strict] checks the errors handling expressions
404395
# - errchkjson # [don't see profit + I'm against of omitting errors like in the first example https://github.com/breml/errchkjson] checks types passed to the json encoding functions. Reports unsupported types and optionally reports occasions, where the check for the returned error can be omitted
405396
# - forcetypeassert # [replaced by errcheck] finds forced type assertions
406-
# Todo : rely on this, and skip seperate go fmt check in cicd
397+
# Todo : rely on this, and skip separate go fmt check in cicd
407398
# - gofmt # [replaced by goimports] checks whether code was gofmt-ed
408399
# Todo : Check this
409400
#- gofumpt # [replaced by goimports, gofumports is not available yet] checks whether code was gofumpt-ed
@@ -412,8 +403,7 @@ linters:
412403
- grouper # analyzes expression groups
413404
- importas # enforces consistent import aliases
414405
- maintidx # measures the maintainability index of each function
415-
# Todo - might enable this. This is not so bad
416-
# - misspell # [useless] finds commonly misspelled English words in comments
406+
- misspell # [useless] finds commonly misspelled English words in comments
417407
# - nlreturn # [too strict and mostly code is not more readable] checks for a new line before return and branch statements to increase code clarity
418408
# Todo : might make tests parallel
419409
#- paralleltest # [too many false positives] detects missing usage of t.Parallel() method in your Go test

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"embed"
5+
56
"github.com/yorukot/superfile/src/cmd"
67
)
78

src/cmd/main.go

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package cmd
22

33
import (
4+
"context"
45
"embed"
56
"encoding/json"
67
"fmt"
7-
"github.com/yorukot/superfile/src/internal/common"
8-
"github.com/yorukot/superfile/src/internal/common/utils"
98
"io"
109
"log"
1110
"log/slog"
@@ -14,6 +13,9 @@ import (
1413
"runtime"
1514
"time"
1615

16+
"github.com/yorukot/superfile/src/internal/common"
17+
"github.com/yorukot/superfile/src/internal/common/utils"
18+
1719
tea "github.com/charmbracelet/bubbletea"
1820
"github.com/charmbracelet/lipgloss"
1921
"github.com/urfave/cli/v2"
@@ -95,7 +97,7 @@ func Run(content embed.FS) {
9597
// Validate the config file exists
9698
if configFileArg != "" {
9799
if _, err := os.Stat(configFileArg); err != nil {
98-
log.Fatalf("Error: While reading config file '%s' from arguement : %v", configFileArg, err)
100+
log.Fatalf("Error: While reading config file '%s' from argument : %v", configFileArg, err)
99101
} else {
100102
variable.ConfigFile = configFileArg
101103
}
@@ -105,7 +107,7 @@ func Run(content embed.FS) {
105107

106108
if hotkeyFileArg != "" {
107109
if _, err := os.Stat(hotkeyFileArg); err != nil {
108-
log.Fatalf("Error: While reading hotkey file '%s' from arguement : %v", hotkeyFileArg, err)
110+
log.Fatalf("Error: While reading hotkey file '%s' from argument : %v", hotkeyFileArg, err)
109111
} else {
110112
variable.HotkeysFile = hotkeyFileArg
111113
}
@@ -131,7 +133,9 @@ func Run(content embed.FS) {
131133

132134
// This must be after calling internal.InitialModel()
133135
// so that we know `common.Config` is loaded
134-
go CheckForUpdates()
136+
// Should not be a goroutine, Otherwise the main
137+
// goroutine will exit first, and this will not be able to finish
138+
CheckForUpdates()
135139

136140
if variable.PrintLastDir {
137141
fmt.Println(variable.LastDir)
@@ -187,7 +191,7 @@ func InitConfigFile() {
187191
// We are initializing these, but not sure if we are ever using them
188192
func InitTrash() error {
189193
// Create trash directories
190-
if runtime.GOOS != "darwin" {
194+
if runtime.GOOS != variable.OS_DARWIN {
191195
err := createDirectories(
192196
variable.CustomTrashDirectory,
193197
variable.CustomTrashDirectoryFiles,
@@ -302,11 +306,22 @@ func CheckForUpdates() {
302306
defer func() {
303307
writeLastCheckTime(currentTime)
304308
}()
305-
client := &http.Client{
306-
Timeout: 5 * time.Second,
309+
// Create a context with a 5-second timeout
310+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
311+
defer cancel() // Cancel the context when done
312+
client := &http.Client{}
313+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, variable.LatestVersionURL, nil)
314+
if err != nil {
315+
slog.Error("Error forming updates request:", "error", err)
316+
return
307317
}
308-
resp, err := client.Get(variable.LatestVersionURL)
318+
319+
resp, err := client.Do(req)
309320
if err != nil {
321+
if ctx.Err() == context.DeadlineExceeded {
322+
slog.Error("Update check request timed out")
323+
return
324+
}
310325
slog.Error("Error checking for updates:", "error", err)
311326
return
312327
}

src/config/fixed_variable.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ const (
1717
EmbedHotkeysFile string = EmbedConfigDir + "/hotkeys.toml"
1818
EmbedThemeDir string = EmbedConfigDir + "/theme"
1919
EmbedThemeCatppuccinFile string = EmbedThemeDir + "/catppuccin.toml"
20+
21+
// These are used while comparing with runtime.GOOS
22+
// OS_WINDOWS represents the Windows operating system identifier
23+
OS_WINDOWS = "windows"
24+
// OS_DARWIN represents the macOS (Darwin) operating system identifier
25+
OS_DARWIN = "darwin"
2026
)
2127

2228
var (

src/internal/common/load_config.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,6 @@ func WriteThemeFiles(content embed.FS) error {
180180
continue
181181
}
182182
// This will not break in windows. This is a relative path for Embed FS. It uses "/" only
183-
// nolint:govet // Suppress err shadowing
184183
src, err := content.ReadFile(variable.EmbedThemeDir + "/" + file.Name())
185184
if err != nil {
186185
slog.Error("Error reading theme file from embed", "error", err)

src/internal/common/predefined_variable.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package common
22

33
import (
4+
"time"
5+
46
"github.com/charmbracelet/lipgloss"
57
"github.com/charmbracelet/x/exp/term/ansi"
68
"github.com/yorukot/superfile/src/config/icon"
7-
"time"
89
)
910

1011
const WheelRunTime = 5
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package utils
2+
3+
import (
4+
"log/slog"
5+
"os"
6+
"strconv"
7+
)
8+
9+
// This file provides utilities for storing boolean values in a file
10+
11+
// Read file with "true" / "false" as content. In case of issues, return defaultValue
12+
func ReadBoolFile(path string, defaultValue bool) bool {
13+
data, err := os.ReadFile(path)
14+
if err != nil {
15+
slog.Error("Error in readBoolFile", "path", path, "error", err)
16+
return defaultValue
17+
}
18+
19+
// Not using strconv.ParseBool() as it allows other values like : "TRUE"
20+
// Using exact string comparison with predefined constants ensures
21+
// consistent behavior and prevents issues with case-insensitivity or
22+
// unexpected values like "yes", "on", etc. that ParseBool would accept
23+
switch string(data) {
24+
case TRUE_STRING:
25+
return true
26+
case FALSE_STRING:
27+
return false
28+
default:
29+
return defaultValue
30+
}
31+
}
32+
33+
func WriteBoolFile(path string, value bool) error {
34+
return os.WriteFile(path, []byte(strconv.FormatBool(value)), 0644)
35+
36+
}

0 commit comments

Comments
 (0)