Skip to content

Commit cf4a914

Browse files
committed
Fix gocritic lint warnings
- Rewrite if-else chains to switch statements in tests and handlers - Hoist constant regexp to package level with MustCompile - Use += assignment operator - Replace log.Fatal with log.Println + return so deferred cleanup runs
1 parent 4abab3a commit cf4a914

7 files changed

Lines changed: 29 additions & 20 deletions

File tree

internal/handlers/git_server_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,14 +179,15 @@ func TestGitServerHandler_AuthenticatedAccessToGitHubRepos(t *testing.T) {
179179
req := httptest.NewRequest("GET", fmt.Sprintf("https://github.com/%s", tt.repoNWO), nil)
180180
req, _ = handler.HandleRequest(req, nil)
181181

182-
if tt.expectedCredential != nil {
182+
switch {
183+
case tt.expectedCredential != nil:
183184
assertHasBasicAuth(t, req,
184185
tt.expectedCredential.GetString("username"),
185186
tt.expectedCredential.GetString("password"),
186187
"valid github request")
187-
} else if tt.isAuthenticated {
188+
case tt.isAuthenticated:
188189
assertAuthenticated(t, req, "valid github request")
189-
} else {
190+
default:
190191
assertUnauthenticated(t, req, "valid github request")
191192
}
192193
})

internal/handlers/github_api_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,12 @@ func TestGitHubAPIHandler_AuthenticatedAccessToGitHubRepos(t *testing.T) {
159159
req := httptest.NewRequest("GET", fmt.Sprintf("https://api.github.com/%s", tt.repoNWO), nil)
160160
req, _ = handler.HandleRequest(req, nil)
161161

162-
if tt.expectedCredential != nil {
162+
switch {
163+
case tt.expectedCredential != nil:
163164
assertHasTokenAuth(t, req, "token", tt.expectedCredential.GetString("password"), "valid api request")
164-
} else if tt.isAuthenticated {
165+
case tt.isAuthenticated:
165166
assertAuthenticated(t, req, "valid github request")
166-
} else {
167+
default:
167168
assertUnauthenticated(t, req, "valid github request")
168169
}
169170
})

internal/handlers/nuget_feed.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,13 +189,14 @@ func extraUrlsFromSourceResponse(body []byte, url string) []string {
189189
var urls []string
190190
bodyString := strings.TrimSpace(string(body))
191191
bodyReader := bytes.NewReader(body)
192-
if strings.HasPrefix(bodyString, "<") {
192+
switch {
193+
case strings.HasPrefix(bodyString, "<"):
193194
// XML v2 API
194195
urls = handleV2Response(bodyReader, url)
195-
} else if strings.HasPrefix(bodyString, "{") {
196+
case strings.HasPrefix(bodyString, "{"):
196197
// JSON v3 API
197198
urls = handleV3Response(bodyReader, url)
198-
} else {
199+
default:
199200
logging.RequestLogf(nil, "unknown API response: %s...", bodyString[:10])
200201
}
201202

internal/handlers/python_index.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import (
1414
"github.com/dependabot/proxy/internal/oidc"
1515
)
1616

17+
var simpleSuffixRe = regexp.MustCompile(`/\+?simple/?\z`)
18+
1719
// PythonIndexHandler handles requests to Python indexes, adding auth.
1820
type PythonIndexHandler struct {
1921
credentials []pythonIndexCredentials
@@ -89,8 +91,7 @@ func (h *PythonIndexHandler) HandleRequest(req *http.Request, ctx *goproxy.Proxy
8991

9092
// Fall back to static credentials
9193
for _, cred := range h.credentials {
92-
re, _ := regexp.Compile(`/\+?simple/?\z`)
93-
indexURL := re.ReplaceAllString(cred.indexURL, "/")
94+
indexURL := simpleSuffixRe.ReplaceAllString(cred.indexURL, "/")
9495
if !helpers.UrlMatchesRequest(req, indexURL, true) && !helpers.CheckHost(req, cred.host) {
9596
continue
9697
}

internal/logging/logging.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func requestLog(ctx *goproxy.ProxyCtx, message string) {
4747
argv = append([]any{reqId}, argv...)
4848

4949
if cache.WasResponseCached(ctx) {
50-
format = format + " (cached)"
50+
format += " (cached)"
5151
}
5252
}
5353
formatted := fmt.Sprintf(format, argv...)

internal/oidc/oidc_credential.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,17 @@ func CreateOIDCCredential(cred config.Credential) (*OIDCCredential, error) {
8282
// aws values
8383
awsRegion := cred.GetString("aws-region")
8484
accountID := cred.GetString("account-id")
85-
roleName := cred.GetString("role-name")
85+
rolleName := cred.GetString("role-name")
8686
domain := cred.GetString("domain")
8787
domainOwner := cred.GetString("domain-owner")
8888

89-
if tenantID != "" && clientID != "" {
89+
switch {
90+
case tenantID != "" && clientID != "":
9091
parameters = &AzureOIDCParameters{
9192
TenantID: tenantID,
9293
ClientID: clientID,
9394
}
94-
} else if jfrogOidcProviderName != "" && feedUrl != "" {
95+
case jfrogOidcProviderName != "" && feedUrl != "":
9596
// jfrog domain is extracted from feed url
9697
jfrogUrlParsed, err := url.Parse(feedUrl)
9798
if err != nil {
@@ -105,7 +106,7 @@ func CreateOIDCCredential(cred config.Credential) (*OIDCCredential, error) {
105106
Audience: cred.GetString("audience"),
106107
IdentityMappingName: cred.GetString("identity-mapping-name"),
107108
}
108-
} else if awsRegion != "" && accountID != "" && roleName != "" && domain != "" && domainOwner != "" {
109+
case awsRegion != "" && accountID != "" && roleName != "" && domain != "" && domainOwner != "":
109110
audience := cred.GetString("audience")
110111
if audience == "" {
111112
audience = "sts.amazonaws.com" // defaults to this

main.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,14 @@ func main() {
4040

4141
cfg, err := config.Parse(*configPath)
4242
if err != nil {
43-
log.Fatal(err)
43+
log.Println(err)
44+
return
4445
}
4546

4647
sentry, err := setupSentry()
4748
if err != nil {
48-
log.Fatal(err)
49+
log.Println(err)
50+
return
4951
}
5052

5153
envSettings := config.ProxyEnvSettings{
@@ -89,11 +91,13 @@ func main() {
8991

9092
log.Printf("Listening (%s)", *addr)
9193
if err := server.ListenAndServe(); err != http.ErrServerClosed {
92-
log.Fatal(err)
94+
log.Println(err)
95+
return
9396
}
9497

9598
if err := proxy.Close(); err != nil {
96-
log.Fatal(err)
99+
log.Println(err)
100+
return
97101
}
98102
}
99103

0 commit comments

Comments
 (0)