Skip to content

Commit 9653d8a

Browse files
authored
Merge pull request #757 from yorukot/golangci_lint_fixes_2
Golangci lint fixes
2 parents 2854a6b + 8a71560 commit 9653d8a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+538
-560
lines changed

.golangci.yaml

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ linters-settings:
128128
# Ignore comments when counting lines.
129129
# Default false
130130
ignore-comments: true
131-
132131
gochecksumtype:
133132
# Presence of `default` case in switch statements satisfies exhaustiveness, if all members are not listed.
134133
# Default: true
@@ -276,8 +275,7 @@ linters:
276275
- dupl # tool for code clone detection
277276
- durationcheck # checks for two durations multiplied together
278277
- errname # checks that sentinel errors are prefixed with the Err and error types are suffixed with the Error
279-
# Todo enable
280-
#- errorlint # finds code that will cause problems with the error wrapping scheme introduced in Go 1.13
278+
- errorlint # finds code that will cause problems with the error wrapping scheme introduced in Go 1.13
281279
- exhaustive # checks exhaustiveness of enum switch statements
282280
- exptostd # detects functions from golang.org/x/exp/ that can be replaced by std functions
283281
- fatcontext # detects nested contexts in loops
@@ -286,27 +284,24 @@ linters:
286284
#- funlen # tool for detection of long functions
287285
- gocheckcompilerdirectives # validates go compiler directive comments (//go:)
288286
# Todo enable - This is too much for us right now
289-
# - gochecknoglobals # checks that no global variables exist
287+
- gochecknoglobals # checks that no global variables exist
290288
- gochecknoinits # checks that no init functions are present in Go code
291289
- gochecksumtype # checks exhaustiveness on Go "sum types"
292290
# Todo enable - Need to refactor many functions
293291
# - gocognit # computes and checks the cognitive complexity of functions
294292
- goconst # finds repeated strings that could be replaced by a constant
295-
# Todo enable - Some few changes. There are good suggestions
296-
# - gocritic # provides diagnostics that check for bugs, performance and style issues
293+
- gocritic # provides diagnostics that check for bugs, performance and style issues
297294
# Todo enable
298295
#- gocyclo # computes and checks the cyclomatic complexity of functions
299-
# Todo enable - too much for us
300-
#- godot # checks if comments end in a period
296+
#- godot # checks if comments end in a period # Too much for us right now
301297
- goimports # in addition to fixing imports, goimports also formats your code in the same style as gofmt
302298
- gomoddirectives # manages the use of 'replace', 'retract', and 'excludes' directives in go.mod
303299
- goprintffuncname # checks that printf-like functions are named with f at the end
304300
# Todo enable - ignore for a few int overflow, and fix file permissions
305301
# weak md5 cryptography, Subprocess launched with a potential tainted input
306302
# - gosec # inspects source code for security problems
307303
- iface # checks the incorrect use of interfaces, helping developers avoid interface pollution
308-
# Todo enable : Need fixes
309-
# - intrange # finds places where for loops could make use of an integer range
304+
- intrange # finds places where for loops could make use of an integer range
310305
# Todo enable : Need many fixes
311306
# - lll # reports long lines
312307
- loggercheck # checks key value pairs for common logger libraries (kitlog,klog,logr,zap)
@@ -323,39 +318,32 @@ linters:
323318
- nilnil # checks that there is no simultaneous return of nil error and an invalid value
324319
- noctx # finds sending http request without context.Context
325320
- nolintlint # reports ill-formed or insufficient nolint directives
326-
# Todo enable - maybe too strict for us
327-
# - nonamedreturns # reports all named returns
321+
- nonamedreturns # reports all named returns
328322
- nosprintfhostport # checks for misuse of Sprintf to construct a host with port in a URL
329323
- perfsprint # checks that fmt.Sprintf can be replaced with a faster alternative
330324
- predeclared # finds code that shadows one of Go's predeclared identifiers
331325
- promlinter # checks Prometheus metrics naming via promlint
332326
- protogetter # reports direct reads from proto message fields when getters should be used
333-
# Todo enable - Some hard to fix things
334-
# - reassign # checks that package variables are not reassigned
327+
- reassign # checks that package variables are not reassigned
335328
# Todo enable - Issue due to model struct
336329
# - recvcheck # checks for receiver type consistency
337-
# Todo enable : Many changes
338-
# - revive # fast, configurable, extensible, flexible, and beautiful linter for Go, drop-in replacement of golint
330+
- revive # fast, configurable, extensible, flexible, and beautiful linter for Go, drop-in replacement of golint
339331
- rowserrcheck # checks whether Err of rows is checked successfully
340332
# Todo enable - Need to use non-root logger
341333
# - sloglint # ensure consistent code style when using log/slog
342334
- spancheck # checks for mistakes with OpenTelemetry/Census spans
343335
- sqlclosecheck # checks that sql.Rows and sql.Stmt are closed
344-
# Todo enable : Many changes, but good changes.
345-
# - stylecheck # is a replacement for golint
336+
- stylecheck # is a replacement for golint
346337
- testableexamples # checks if examples are testable (have an expected output)
347338
- testifylint # checks usage of github.com/stretchr/testify
348-
# Todo enable - Might be too strict for us
349-
# - testpackage # makes you use a separate _test package
339+
# - testpackage # makes you use a separate _test package # Not needed - Makes it harder to test unexported package functions.
350340
- tparallel # detects inappropriate usage of t.Parallel() method in your Go test codes
351341
- unconvert # removes unnecessary type conversions
352342
- unparam # reports unused function parameters
353343
- usestdlibvars # detects the possibility to use variables/constants from the Go standard library
354344
- usetesting # reports uses of functions with replacement inside the testing package
355-
# Todo enable - many required fixes
356-
# - wastedassign # finds wasted assignment statements
357-
# Todo enable - many fixes
358-
# - whitespace # detects leading and trailing whitespace
345+
- wastedassign # finds wasted assignment statements
346+
- whitespace # detects leading and trailing whitespace
359347

360348
## Todo : Enable some of them
361349
## you may want to enable
@@ -390,13 +378,12 @@ linters:
390378
# - dogsled # checks assignments with too many blank identifiers (e.g. x, _, _, _, := f())
391379
# False positives
392380
# - dupword # [useless without config] checks for duplicate words in the source code
393-
# Todo : It gave some useful errors. Should fix
394-
# - err113 # [too strict] checks the errors handling expressions
381+
382+
# It gave some useful errors optimization. Should fix eventually. But this is low priority
383+
# - err113 # [too strict] checks the errors handling expressions #
395384
# - 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
396385
# - forcetypeassert # [replaced by errcheck] finds forced type assertions
397-
# Todo : rely on this, and skip separate go fmt check in cicd
398386
# - gofmt # [replaced by goimports] checks whether code was gofmt-ed
399-
# Todo : Check this
400387
#- gofumpt # [replaced by goimports, gofumports is not available yet] checks whether code was gofumpt-ed
401388
#- gomodguard # [use more powerful depguard] allow and block lists linter for direct Go module dependencies
402389
- gosmopolitan # reports certain i18n/l10n anti-patterns in your Go codebase
@@ -405,14 +392,34 @@ linters:
405392
- maintidx # measures the maintainability index of each function
406393
- misspell # [useless] finds commonly misspelled English words in comments
407394
# - nlreturn # [too strict and mostly code is not more readable] checks for a new line before return and branch statements to increase code clarity
408-
# Todo : might make tests parallel
409395
#- paralleltest # [too many false positives] detects missing usage of t.Parallel() method in your Go test
410-
# Todo : Check this. There are some issues to be fixed
411396
# - tagliatelle # checks the struct tags
412397
# - tenv # [deprecated, replaced by usetesting] detects using os.Setenv instead of t.Setenv since Go1.17
413398
#- thelper # detects golang test helpers without t.Helper() call and checks the consistency of test helpers
414399
# - wsl # [too strict and mostly code is not more readable] whitespace linter forces you to use empty lines
415-
400+
exclusions:
401+
rules:
402+
# Stores globally accessible icon strings
403+
- path: 'src/config/icon/icon.go'
404+
linters:
405+
- gochecknoglobals
406+
# Stores prerendered styles
407+
- path: 'src/internal/common/style.go'
408+
linters:
409+
- gochecknoglobals
410+
# Some fixed variables like default config paths, version, etc.
411+
- path: 'src/config/fixed_variable.go'
412+
linters:
413+
- gochecknoglobals
414+
# Stores predefined variables, like re-used non-const strings
415+
- path: 'src/internal/common/predefined_variable.go'
416+
linters:
417+
- gochecknoglobals
418+
# Global variables storing config
419+
- path: 'src/internal/common/default_config.go'
420+
linters:
421+
- gochecknoglobals
422+
416423

417424
issues:
418425
# Maximum count of issues with the same text.

src/cmd/main.go

Lines changed: 14 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"encoding/json"
77
"fmt"
88
"io"
9-
"log"
109
"log/slog"
1110
"net/http"
1211
"os"
@@ -26,11 +25,10 @@ import (
2625

2726
// Run superfile app
2827
func Run(content embed.FS) {
29-
3028
// Before we open log file, set all "non debug" logs to stdout
3129
utils.SetRootLoggerToStdout(false)
3230

33-
common.LoadInitial_PrerenderedVariables()
31+
common.LoadInitialPrerenderedVariables()
3432
common.LoadAllDefaultConfig(content)
3533

3634
app := &cli.App{
@@ -43,7 +41,7 @@ func Run(content embed.FS) {
4341
Name: "path-list",
4442
Aliases: []string{"pl"},
4543
Usage: "Print the path to the configuration and directory",
46-
Action: func(c *cli.Context) error {
44+
Action: func(_ *cli.Context) error {
4745
fmt.Printf("%-*s %s\n", 55, lipgloss.NewStyle().Foreground(lipgloss.Color("#66b2ff")).Render("[Configuration file path]"), variable.ConfigFile)
4846
fmt.Printf("%-*s %s\n", 55, lipgloss.NewStyle().Foreground(lipgloss.Color("#ffcc66")).Render("[Hotkeys file path]"), variable.HotkeysFile)
4947
fmt.Printf("%-*s %s\n", 55, lipgloss.NewStyle().Foreground(lipgloss.Color("#66ff66")).Render("[Log file path]"), variable.LogFile)
@@ -91,27 +89,7 @@ func Run(content embed.FS) {
9189
path = c.Args().First()
9290
}
9391

94-
// Setting the config file path
95-
configFileArg := c.String("config-file")
96-
97-
// Validate the config file exists
98-
if configFileArg != "" {
99-
if _, err := os.Stat(configFileArg); err != nil {
100-
log.Fatalf("Error: While reading config file '%s' from argument : %v", configFileArg, err)
101-
} else {
102-
variable.ConfigFile = configFileArg
103-
}
104-
}
105-
106-
hotkeyFileArg := c.String("hotkey-file")
107-
108-
if hotkeyFileArg != "" {
109-
if _, err := os.Stat(hotkeyFileArg); err != nil {
110-
log.Fatalf("Error: While reading hotkey file '%s' from argument : %v", hotkeyFileArg, err)
111-
} else {
112-
variable.HotkeysFile = hotkeyFileArg
113-
}
114-
}
92+
variable.UpdateVarFromCliArgs(c)
11593

11694
InitConfigFile()
11795

@@ -120,15 +98,11 @@ func Run(content embed.FS) {
12098
hasTrash = false
12199
}
122100

123-
variable.FixHotkeys = c.Bool("fix-hotkeys")
124-
variable.FixConfigFile = c.Bool("fix-config-file")
125-
variable.PrintLastDir = c.Bool("print-last-dir")
126-
127101
firstUse := checkFirstUse()
128102

129103
p := tea.NewProgram(internal.InitialModel(path, firstUse, hasTrash), tea.WithAltScreen(), tea.WithMouseCellMotion())
130104
if _, err := p.Run(); err != nil {
131-
log.Fatalf("Alas, there's been an error: %v", err)
105+
utils.PrintfAndExit("Alas, there's been an error: %v", err)
132106
}
133107

134108
// This must be after calling internal.InitialModel()
@@ -147,7 +121,7 @@ func Run(content embed.FS) {
147121

148122
err := app.Run(os.Args)
149123
if err != nil {
150-
log.Fatalln(err)
124+
utils.PrintlnAndExit(err)
151125
}
152126
}
153127

@@ -161,7 +135,7 @@ func InitConfigFile() {
161135
variable.SuperFileStateDir,
162136
variable.ThemeFolder,
163137
); err != nil {
164-
log.Fatalln("Error creating directories:", err)
138+
utils.PrintlnAndExit("Error creating directories:", err)
165139
}
166140

167141
// Create files
@@ -171,27 +145,27 @@ func InitConfigFile() {
171145
variable.ThemeFileVersion,
172146
variable.ToggleFooter,
173147
); err != nil {
174-
log.Fatalln("Error creating files:", err)
148+
utils.PrintlnAndExit("Error creating files:", err)
175149
}
176150

177151
// Write config file
178152
if err := writeConfigFile(variable.ConfigFile, common.ConfigTomlString); err != nil {
179-
log.Fatalln("Error writing config file:", err)
153+
utils.PrintlnAndExit("Error writing config file:", err)
180154
}
181155

182156
if err := writeConfigFile(variable.HotkeysFile, common.HotkeysTomlString); err != nil {
183-
log.Fatalln("Error writing config file:", err)
157+
utils.PrintlnAndExit("Error writing config file:", err)
184158
}
185159

186-
if err := initJsonFile(variable.PinnedFile); err != nil {
187-
log.Fatalln("Error initializing json file:", err)
160+
if err := initJSONFile(variable.PinnedFile); err != nil {
161+
utils.PrintlnAndExit("Error initializing json file:", err)
188162
}
189163
}
190164

191165
// We are initializing these, but not sure if we are ever using them
192166
func InitTrash() error {
193167
// Create trash directories
194-
if runtime.GOOS != variable.OS_DARWIN {
168+
if runtime.GOOS != utils.OsDarwin {
195169
err := createDirectories(
196170
variable.CustomTrashDirectory,
197171
variable.CustomTrashDirectoryFiles,
@@ -240,7 +214,7 @@ func checkFirstUse() bool {
240214
if _, err := os.Stat(file); os.IsNotExist(err) {
241215
firstUse = true
242216
if err = os.WriteFile(file, nil, 0644); err != nil {
243-
log.Fatalf("Failed to create file: %v", err)
217+
utils.PrintfAndExit("Failed to create file: %v", err)
244218
}
245219
}
246220
return firstUse
@@ -256,7 +230,7 @@ func writeConfigFile(path, data string) error {
256230
return nil
257231
}
258232

259-
func initJsonFile(path string) error {
233+
func initJSONFile(path string) error {
260234
if _, err := os.Stat(path); os.IsNotExist(err) {
261235
if err = os.WriteFile(path, []byte("null"), 0644); err != nil {
262236
return fmt.Errorf("failed to initialize json file %s: %w", path, err)

0 commit comments

Comments
 (0)