Skip to content

Commit 4cd13a4

Browse files
committed
chore: configure and address golangci-lint issues
1 parent 1c663f5 commit 4cd13a4

27 files changed

+1011
-543
lines changed

.golangci.yml

Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
# golangci-lint configuration for nix-auth
2+
# Documentation: https://golangci-lint.run/usage/configuration/
3+
4+
version: "2"
5+
6+
run:
7+
# Timeout for analysis
8+
timeout: 5m
9+
10+
# Include test files
11+
tests: true
12+
13+
# List of build tags to use
14+
build-tags:
15+
- integration
16+
17+
linters:
18+
default: standard
19+
enable:
20+
# Additional linters for code quality
21+
- bodyclose # Checks whether HTTP response body is closed successfully
22+
- dogsled # Checks assignments with too many blank identifiers
23+
- dupl # Tool for code clone detection
24+
- copyloopvar # Checks for unpinned variables in go programs
25+
- gocognit # Computes cognitive complexity of functions
26+
- goconst # Finds repeated strings that could be replaced by constants
27+
- gocritic # Provides diagnostics that check for bugs, performance and style issues
28+
- gocyclo # Computes cyclomatic complexity of functions
29+
- godot # Check if comments end in a period
30+
- mnd # Detects magic numbers (replaces gomnd)
31+
- goprintffuncname # Checks that printf-like functions are named with `f` at the end
32+
- gosec # Security checker
33+
- misspell # Finds commonly misspelled English words in comments
34+
- nakedret # Finds naked returns in functions greater than a specified function length
35+
- nestif # Reports deeply nested if statements
36+
- nilerr # Finds the code that returns nil even if it checks that the error is not nil
37+
- noctx # Finds sending http request without context.Context
38+
- nolintlint # Reports ill-formed or insufficient nolint directives
39+
- prealloc # Finds slice declarations that could potentially be preallocated
40+
- predeclared # Find code that shadows one of Go's predeclared identifiers
41+
- revive # Fast, configurable, extensible, flexible, and beautiful linter for Go
42+
- rowserrcheck # Checks whether Err of rows is checked successfully
43+
- sqlclosecheck # Checks that sql.Rows and sql.Stmt are closed
44+
- thelper # Detects Go test helpers without t.Helper() call
45+
- tparallel # Detects inappropriate usage of t.Parallel() method in your Go test codes
46+
- unconvert # Remove unnecessary type conversions
47+
- unparam # Reports unused function parameters
48+
- whitespace # Tool for detection of leading and trailing whitespace
49+
- wsl # Whitespace Linter
50+
51+
disable:
52+
- depguard # Checks if package imports are in a list of acceptable packages
53+
- exhaustive # Check exhaustiveness of enum switch statements
54+
- funlen # Tool for detection of long functions
55+
- gochecknoglobals # Check that no global variables exist
56+
- godox # Tool for detection of FIXME, TODO and other comment keywords
57+
- err113 # Golang linter to check the errors handling expressions
58+
- wrapcheck # Checks that errors returned from external packages are wrapped
59+
60+
exclusions:
61+
paths:
62+
- vendor
63+
- .git
64+
- testdata
65+
files:
66+
- ".*\\.pb\\.go$"
67+
- ".*\\.gen\\.go$"
68+
69+
formatters:
70+
enable:
71+
- gci # Controls Go package import order and makes it deterministic
72+
- gofmt # Checks whether code was gofmt-ed
73+
- goimports # Check import statements are formatted according to the 'goimport' command
74+
75+
exclusions:
76+
paths:
77+
- vendor
78+
- .git
79+
- testdata
80+
files:
81+
- ".*\\.pb\\.go$"
82+
- ".*\\.gen\\.go$"
83+
84+
linters-settings:
85+
errcheck:
86+
# Don't report errors for defer Close() calls
87+
exclude-functions:
88+
- (io.Closer).Close
89+
- (*os.File).Close
90+
- (*io.PipeWriter).Close
91+
- (*bytes.Buffer).WriteTo
92+
- (*strings.Builder).WriteTo
93+
94+
govet:
95+
# Enable all analyzers
96+
enable-all: true
97+
98+
gocyclo:
99+
# Minimal code complexity to report
100+
min-complexity: 15
101+
102+
dupl:
103+
# Tokens count to trigger issue
104+
threshold: 100
105+
106+
goconst:
107+
# Minimal length of string constant
108+
min-len: 3
109+
# Minimal occurrences count to trigger
110+
min-occurrences: 3
111+
112+
misspell:
113+
# Locale to use
114+
locale: US
115+
116+
mnd:
117+
# Don't include the "operation" and "assign"
118+
checks: [argument,case,condition,return]
119+
120+
gocritic:
121+
enabled-tags:
122+
- diagnostic
123+
- experimental
124+
- opinionated
125+
- performance
126+
- style
127+
disabled-checks:
128+
- dupImport # https://github.com/go-critic/go-critic/issues/845
129+
- ifElseChain
130+
- octalLiteral
131+
- whyNoLint
132+
- wrapperFunc
133+
134+
goimports:
135+
# Put local imports after 3rd-party packages
136+
local-prefixes: github.com/numtide/nix-auth
137+
138+
gci:
139+
sections:
140+
- standard
141+
- default
142+
- prefix(github.com/numtide/nix-auth)
143+
144+
nolintlint:
145+
# Enable to ensure that nolint directives are all used
146+
allow-unused: false
147+
# Exclude following linters from requiring an explanation
148+
allow-no-explanation: []
149+
# Enable to require an explanation of nonzero length for nolint directives
150+
require-explanation: true
151+
# Enable to require nolint directives to mention the specific linter
152+
require-specific: true
153+
154+
revive:
155+
# Accept interfaces, return concrete types
156+
confidence: 0.8
157+
rules:
158+
- name: blank-imports
159+
- name: context-as-argument
160+
- name: context-keys-type
161+
- name: dot-imports
162+
- name: error-return
163+
- name: error-strings
164+
- name: error-naming
165+
- name: if-return
166+
- name: increment-decrement
167+
- name: var-naming
168+
- name: var-declaration
169+
- name: package-comments
170+
- name: range
171+
- name: receiver-naming
172+
- name: time-naming
173+
- name: unexported-return
174+
- name: indent-error-flow
175+
- name: errorf
176+
- name: empty-block
177+
- name: superfluous-else
178+
- name: unused-parameter
179+
- name: unreachable-code
180+
- name: redefines-builtin-id
181+
182+
issues:
183+
# Exclude some linters from running on tests files
184+
exclude-rules:
185+
- path: _test\.go
186+
linters:
187+
- dupl
188+
- mnd
189+
# Exclude G104 (error handling) from specific files
190+
- linters:
191+
- gosec
192+
text: "G104"
193+
194+
# Maximum issues count per one linter. Set to 0 to disable
195+
max-issues-per-linter: 0
196+
197+
# Maximum count of issues with the same text. Set to 0 to disable
198+
max-same-issues: 0
199+
200+
# Show only new issues created after git revision
201+
new: false
202+
203+
# Fix found issues (if it's supported by the linter)
204+
fix: false
205+
206+
severity:
207+
# Set the default severity for issues
208+
default: warning
209+
210+
# The list of ids of default excludes to include or disable
211+
rules:
212+
- linters:
213+
- gosec
214+
text: "G104" # Errors unhandled
215+
severity: info

cmd/login.go

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1+
// Package cmd provides the CLI commands for nix-auth.
12
package cmd
23

34
import (
45
"context"
6+
"errors"
57
"fmt"
68
"strings"
79

810
"github.com/numtide/nix-auth/internal/config"
911
"github.com/numtide/nix-auth/internal/provider"
1012
"github.com/numtide/nix-auth/internal/ui"
11-
1213
"github.com/spf13/cobra"
1314
)
1415

@@ -60,7 +61,7 @@ func init() {
6061
loginCmd.Flags().BoolVar(&loginDryRun, "dry-run", false, "Preview what would happen without authenticating")
6162
}
6263

63-
func runLogin(cmd *cobra.Command, args []string) error {
64+
func runLogin(_ *cobra.Command, args []string) error {
6465
// Parse the input
6566
input := "github" // default
6667
if len(args) > 0 {
@@ -81,11 +82,14 @@ func runLogin(cmd *cobra.Command, args []string) error {
8182
fmt.Printf("- Provider: %s\n", prov.Name())
8283
fmt.Printf("- Host: %s\n", host)
8384
fmt.Printf("- OAuth scopes: %s\n", strings.Join(prov.GetScopes(), ", "))
85+
8486
if loginClientID != "" {
8587
fmt.Printf("- Client ID: %s\n", loginClientID)
8688
}
89+
8790
fmt.Printf("- Config file: %s\n", configPath)
8891
fmt.Println("\nNo authentication performed. Run without --dry-run to authenticate.")
92+
8993
return nil
9094
}
9195

@@ -101,6 +105,7 @@ func runLogin(cmd *cobra.Command, args []string) error {
101105
if err != nil {
102106
return fmt.Errorf("failed to read confirmation: %w", err)
103107
}
108+
104109
if !confirm {
105110
fmt.Println("Login cancelled.")
106111
return nil
@@ -109,25 +114,30 @@ func runLogin(cmd *cobra.Command, args []string) error {
109114

110115
// Perform authentication
111116
ctx := context.Background()
117+
112118
token, err := prov.Authenticate(ctx)
113119
if err != nil {
114120
errMsg := fmt.Sprintf("authentication failed: %v", err)
115121
if strings.Contains(err.Error(), "client ID") {
116122
errMsg += "\n\nFor self-hosted instances, you need to create an OAuth application.\n" +
117123
"See the instructions above or use --dry-run to preview the configuration."
118124
}
119-
return fmt.Errorf(errMsg)
125+
126+
return errors.New(errMsg)
120127
}
121128

122129
// Validate token
123130
fmt.Println("\nValidating token...")
131+
124132
status, err := prov.ValidateToken(ctx, token)
125133
if err != nil && status != provider.ValidationStatusUnknown {
126134
return fmt.Errorf("token validation failed: %w", err)
127135
}
136+
128137
if status == provider.ValidationStatusInvalid {
129138
return fmt.Errorf("token is invalid")
130139
}
140+
131141
if status == provider.ValidationStatusUnknown {
132142
fmt.Println("Warning: Token cannot be verified (unknown provider)")
133143
}
@@ -143,7 +153,7 @@ func runLogin(cmd *cobra.Command, args []string) error {
143153
return nil
144154
}
145155

146-
// resolveProviderAndHost determines the provider and host from the input
156+
// resolveProviderAndHost determines the provider and host from the input.
147157
func resolveProviderAndHost(input, providerFlag string) (provider.Provider, string, error) {
148158
// Check if input is a provider alias
149159
if reg, ok := provider.GetRegistration(input); ok {
@@ -160,23 +170,25 @@ func resolveProviderAndHost(input, providerFlag string) (provider.Provider, stri
160170
}
161171

162172
// Create provider with config
163-
cfg := provider.ProviderConfig{
173+
cfg := provider.Config{
164174
Host: host,
165175
ClientID: loginClientID,
166176
}
167177
prov := reg.New(cfg)
178+
168179
return prov, host, nil
169180
}
170181

171182
// Input is a host
172183
return resolveProviderForHost(input, providerFlag)
173184
}
174185

175-
// resolveProviderForHost handles the case where input is a host
186+
// resolveProviderForHost handles the case where input is a host.
176187
func resolveProviderForHost(host, providerFlag string) (provider.Provider, string, error) {
177188
if providerFlag == "auto" {
178189
// Auto-detect provider type
179190
fmt.Printf("Detecting provider type for %s by querying API...\n", host)
191+
180192
ctx := context.Background()
181193

182194
prov, err := provider.Detect(ctx, host, loginClientID)
@@ -187,14 +199,16 @@ func resolveProviderForHost(host, providerFlag string) (provider.Provider, strin
187199
}
188200

189201
fmt.Printf("Detected: %s\n\n", prov.Name())
202+
190203
return prov, host, nil
191204
}
192205

193206
// Use explicitly specified provider
194-
cfg := provider.ProviderConfig{
207+
cfg := provider.Config{
195208
Host: host,
196209
ClientID: loginClientID,
197210
}
211+
198212
prov, ok := provider.GetWithConfig(providerFlag, cfg)
199213
if !ok {
200214
available := strings.Join(provider.List(), ", ")

0 commit comments

Comments
 (0)