Skip to content

Commit 88a0ba6

Browse files
authored
Updates golang lint config to v2 (#3436)
1 parent e7958f3 commit 88a0ba6

File tree

281 files changed

+11349
-4232
lines changed

Some content is hidden

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

281 files changed

+11349
-4232
lines changed

Diff for: .golangci.yaml

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ version: "2"
22
run:
33
tests: false
44
linters:
5-
default: none
5+
default: standard
66
enable:
77
- bodyclose
88
- dogsled
@@ -47,7 +47,7 @@ linters:
4747
- performance
4848
- style
4949
gocyclo:
50-
min-complexity: 20
50+
min-complexity: 10
5151
lll:
5252
line-length: 140
5353
misspell:
@@ -105,11 +105,14 @@ formatters:
105105
enable:
106106
- gofmt
107107
- goimports
108+
- golines
108109
settings:
109110
gofmt:
110111
rewrite-rules:
111112
- pattern: interface{}
112113
replacement: any
114+
golines:
115+
max-len: 140
113116
exclusions:
114117
generated: lax
115118
paths:

Diff for: backend/internal/auth/apikey/client.go

+24-9
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,16 @@ type TokenContextData struct {
2424
}
2525

2626
var (
27-
InvalidApiKeyErr = errors.New("token is not a valid neosync api key")
28-
ApiKeyExpiredErr = nucleuserrors.NewUnauthenticated("token is expired")
27+
ErrInvalidApiKey = errors.New("token is not a valid neosync api key")
28+
ErrApiKeyExpired = nucleuserrors.NewUnauthenticated("token is expired")
2929
)
3030

3131
type Queries interface {
32-
GetAccountApiKeyByKeyValue(ctx context.Context, db db_queries.DBTX, apiKey string) (db_queries.NeosyncApiAccountApiKey, error)
32+
GetAccountApiKeyByKeyValue(
33+
ctx context.Context,
34+
db db_queries.DBTX,
35+
apiKey string,
36+
) (db_queries.NeosyncApiAccountApiKey, error)
3337
}
3438

3539
type Client struct {
@@ -49,10 +53,19 @@ func New(
4953
for _, procedure := range allowedWorkerProcedures {
5054
allowedWorkerProcedureSet[procedure] = struct{}{}
5155
}
52-
return &Client{q: queries, db: db, allowedWorkerApiKeys: allowedWorkerApiKeys, allowedWorkerProcedures: allowedWorkerProcedureSet}
56+
return &Client{
57+
q: queries,
58+
db: db,
59+
allowedWorkerApiKeys: allowedWorkerApiKeys,
60+
allowedWorkerProcedures: allowedWorkerProcedureSet,
61+
}
5362
}
5463

55-
func (c *Client) InjectTokenCtx(ctx context.Context, header http.Header, spec connect.Spec) (context.Context, error) {
64+
func (c *Client) InjectTokenCtx(
65+
ctx context.Context,
66+
header http.Header,
67+
spec connect.Spec,
68+
) (context.Context, error) {
5669
token, err := utils.GetBearerTokenFromHeader(header, "Authorization")
5770
if err != nil {
5871
return nil, err
@@ -66,11 +79,11 @@ func (c *Client) InjectTokenCtx(ctx context.Context, header http.Header, spec co
6679
if err != nil && !neosyncdb.IsNoRows(err) {
6780
return nil, err
6881
} else if err != nil && neosyncdb.IsNoRows(err) {
69-
return nil, InvalidApiKeyErr
82+
return nil, ErrInvalidApiKey
7083
}
7184

7285
if time.Now().After(apiKey.ExpiresAt.Time) {
73-
return nil, ApiKeyExpiredErr
86+
return nil, ErrApiKeyExpired
7487
}
7588

7689
return SetTokenData(ctx, &TokenContextData{
@@ -87,13 +100,15 @@ func (c *Client) InjectTokenCtx(ctx context.Context, header http.Header, spec co
87100
ApiKeyType: apikey.WorkerApiKey,
88101
}), nil
89102
}
90-
return nil, InvalidApiKeyErr
103+
return nil, ErrInvalidApiKey
91104
}
92105

93106
func GetTokenDataFromCtx(ctx context.Context) (*TokenContextData, error) {
94107
data, ok := ctx.Value(TokenContextKey{}).(*TokenContextData)
95108
if !ok {
96-
return nil, nucleuserrors.NewUnauthenticated("ctx does not contain TokenContextData or unable to cast struct")
109+
return nil, nucleuserrors.NewUnauthenticated(
110+
"ctx does not contain TokenContextData or unable to cast struct",
111+
)
97112
}
98113
return data, nil
99114
}

Diff for: backend/internal/auth/apikey/client_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func Test_Client_InjectTokenCtx_Account_Expired(t *testing.T) {
8888
"Authorization": []string{fmt.Sprintf("Bearer %s", fakeToken)},
8989
}, connect.Spec{})
9090
assert.Error(t, err)
91-
assert.True(t, errors.Is(err, ApiKeyExpiredErr))
91+
assert.True(t, errors.Is(err, ErrApiKeyExpired))
9292
assert.Nil(t, newctx)
9393
}
9494

Diff for: backend/internal/auth/authmw/auth.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ import (
1010
)
1111

1212
type AuthClient interface {
13-
InjectTokenCtx(ctx context.Context, header http.Header, spec connect.Spec) (context.Context, error)
13+
InjectTokenCtx(
14+
ctx context.Context,
15+
header http.Header,
16+
spec connect.Spec,
17+
) (context.Context, error)
1418
}
1519

1620
type AuthMiddleware struct {
@@ -25,11 +29,15 @@ func New(
2529
return &AuthMiddleware{jwtClient: jwtClient, apiKeyClient: apiKeyClient}
2630
}
2731

28-
func (n *AuthMiddleware) InjectTokenCtx(ctx context.Context, header http.Header, spec connect.Spec) (context.Context, error) {
32+
func (n *AuthMiddleware) InjectTokenCtx(
33+
ctx context.Context,
34+
header http.Header,
35+
spec connect.Spec,
36+
) (context.Context, error) {
2937
apiKeyCtx, err := n.apiKeyClient.InjectTokenCtx(ctx, header, spec)
30-
if err != nil && !errors.Is(err, auth_apikey.InvalidApiKeyErr) {
38+
if err != nil && !errors.Is(err, auth_apikey.ErrInvalidApiKey) {
3139
return nil, err
32-
} else if err != nil && errors.Is(err, auth_apikey.InvalidApiKeyErr) {
40+
} else if err != nil && errors.Is(err, auth_apikey.ErrInvalidApiKey) {
3341
return n.jwtClient.InjectTokenCtx(ctx, header, spec)
3442
}
3543
return apiKeyCtx, nil

Diff for: backend/internal/auth/authmw/auth_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func Test_AuthMiddleware_InjectTokenCtx_ApiKey_JwtFallback(t *testing.T) {
5656

5757
ctx := context.Background()
5858
mockApiKey.On("InjectTokenCtx", ctx, mock.Anything, mock.Anything).
59-
Return(nil, auth_apikey.InvalidApiKeyErr)
59+
Return(nil, auth_apikey.ErrInvalidApiKey)
6060
mockJwt.On("InjectTokenCtx", ctx, mock.Anything, mock.Anything).
6161
Return(context.Background(), nil)
6262

Diff for: backend/internal/auth/client/client.go

+27-6
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,17 @@ import (
1212
)
1313

1414
type Interface interface {
15-
GetTokenResponse(ctx context.Context, clientId string, code string, redirecturi string) (*AuthTokenResponse, error)
16-
GetRefreshedAccessToken(ctx context.Context, clientId string, refreshToken string) (*AuthTokenResponse, error)
15+
GetTokenResponse(
16+
ctx context.Context,
17+
clientId string,
18+
code string,
19+
redirecturi string,
20+
) (*AuthTokenResponse, error)
21+
GetRefreshedAccessToken(
22+
ctx context.Context,
23+
clientId string,
24+
refreshToken string,
25+
) (*AuthTokenResponse, error)
1726
GetUserInfo(ctx context.Context, accessToken string) (*UserInfo, error)
1827
GetTokenEndpoint(ctx context.Context) (string, error)
1928
GetAuthorizationEndpoint(ctx context.Context) (string, error)
@@ -147,7 +156,10 @@ func (c *Client) GetRefreshedAccessToken(
147156
clientSecret := c.clientIdSecretMap[clientId]
148157
payload := strings.NewReader(
149158
fmt.Sprintf(
150-
"grant_type=refresh_token&client_id=%s&client_secret=%s&refresh_token=%s", clientId, clientSecret, refreshToken,
159+
"grant_type=refresh_token&client_id=%s&client_secret=%s&refresh_token=%s",
160+
clientId,
161+
clientSecret,
162+
refreshToken,
151163
),
152164
)
153165
tokenurl, err := c.GetTokenEndpoint(ctx)
@@ -179,14 +191,20 @@ func (c *Client) GetRefreshedAccessToken(
179191
err = json.Unmarshal(body, &tokenResponse)
180192

181193
if err != nil {
182-
return nil, fmt.Errorf("unable to unmarshal token response from refresh token request: %w", err)
194+
return nil, fmt.Errorf(
195+
"unable to unmarshal token response from refresh token request: %w",
196+
err,
197+
)
183198
}
184199

185200
if tokenResponse.AccessToken == "" {
186201
var errorResponse AuthTokenErrorData
187202
err = json.Unmarshal(body, &errorResponse)
188203
if err != nil {
189-
return nil, fmt.Errorf("unable to unmarshal error response from refresh token request: %w", err)
204+
return nil, fmt.Errorf(
205+
"unable to unmarshal error response from refresh token request: %w",
206+
err,
207+
)
190208
}
191209
return &AuthTokenResponse{
192210
Result: nil,
@@ -274,7 +292,10 @@ type openIdConfiguration struct {
274292
}
275293

276294
func (c *Client) getOpenIdConfiguration(ctx context.Context) (*openIdConfiguration, error) {
277-
configUrl := fmt.Sprintf("%s/.well-known/openid-configuration", strings.TrimSuffix(c.authBaseUrl, "/"))
295+
configUrl := fmt.Sprintf(
296+
"%s/.well-known/openid-configuration",
297+
strings.TrimSuffix(c.authBaseUrl, "/"),
298+
)
278299

279300
req, err := http.NewRequestWithContext(ctx, http.MethodGet, configUrl, http.NoBody)
280301
if err != nil {

Diff for: backend/internal/auth/clientcred_token_provider/client.go

+13-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ type tokenProviderClient struct {
2525
clientSecret string
2626
}
2727

28-
func (c *tokenProviderClient) GetToken(ctx context.Context) (*auth_client.AuthTokenResponse, error) {
28+
func (c *tokenProviderClient) GetToken(
29+
ctx context.Context,
30+
) (*auth_client.AuthTokenResponse, error) {
2931
values := url.Values{
3032
"grant_type": []string{"client_credentials"},
3133
"client_id": []string{c.clientId},
@@ -87,9 +89,17 @@ type ClientCredentialsTokenProvider struct {
8789
expiresAt *time.Time
8890
}
8991

90-
func New(tokenurl, clientId, clientSecret string, tokenExpirationBuffer time.Duration, logger *slog.Logger) *ClientCredentialsTokenProvider {
92+
func New(
93+
tokenurl, clientId, clientSecret string,
94+
tokenExpirationBuffer time.Duration,
95+
logger *slog.Logger,
96+
) *ClientCredentialsTokenProvider {
9197
return &ClientCredentialsTokenProvider{
92-
tokenprovider: &tokenProviderClient{tokenurl: tokenurl, clientId: clientId, clientSecret: clientSecret},
98+
tokenprovider: &tokenProviderClient{
99+
tokenurl: tokenurl,
100+
clientId: clientId,
101+
clientSecret: clientSecret,
102+
},
93103
tokenExpBuffer: tokenExpirationBuffer,
94104
logger: logger,
95105
}

Diff for: backend/internal/auth/jwt/client.go

+18-5
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,19 @@ func New(
7373
}
7474

7575
// Validates and returns a parsed access token (if available)
76-
func (j *Client) validateToken(ctx context.Context, accessToken string) (*validator.ValidatedClaims, error) {
76+
func (j *Client) validateToken(
77+
ctx context.Context,
78+
accessToken string,
79+
) (*validator.ValidatedClaims, error) {
7780
rawParsedToken, err := j.jwtValidator.ValidateToken(ctx, accessToken)
7881
if err != nil {
7982
return nil, nucleuserrors.NewUnauthenticated(err.Error())
8083
}
8184
validatedClaims, ok := rawParsedToken.(*validator.ValidatedClaims)
8285
if !ok {
83-
return nil, nucleuserrors.NewInternalError("unable to convert token claims what was expected")
86+
return nil, nucleuserrors.NewInternalError(
87+
"unable to convert token claims what was expected",
88+
)
8489
}
8590
return validatedClaims, nil
8691
}
@@ -111,7 +116,11 @@ func hasScope(scopes []string, expectedScope string) bool {
111116
}
112117

113118
// Validates the ctx is authenticated. Stuffs the parsed token onto the context
114-
func (j *Client) InjectTokenCtx(ctx context.Context, header http.Header, spec connect.Spec) (context.Context, error) {
119+
func (j *Client) InjectTokenCtx(
120+
ctx context.Context,
121+
header http.Header,
122+
spec connect.Spec,
123+
) (context.Context, error) {
115124
token, err := utils.GetBearerTokenFromHeader(header, "Authorization")
116125
if err != nil {
117126
return nil, err
@@ -124,7 +133,9 @@ func (j *Client) InjectTokenCtx(ctx context.Context, header http.Header, spec co
124133

125134
claims, ok := parsedToken.CustomClaims.(*CustomClaims)
126135
if !ok {
127-
return nil, nucleuserrors.NewInternalError("unable to cast custom token claims to CustomClaims struct")
136+
return nil, nucleuserrors.NewInternalError(
137+
"unable to cast custom token claims to CustomClaims struct",
138+
)
128139
}
129140

130141
scopes := getCombinedScopesAndPermissions(claims.Scope, claims.Permissions)
@@ -161,7 +172,9 @@ func GetTokenDataFromCtx(ctx context.Context) (*TokenContextData, error) {
161172
val := ctx.Value(TokenContextKey{})
162173
data, ok := val.(*TokenContextData)
163174
if !ok {
164-
return nil, nucleuserrors.NewUnauthenticated(fmt.Sprintf("ctx does not contain TokenContextData or unable to cast struct: %T", val))
175+
return nil, nucleuserrors.NewUnauthenticated(
176+
fmt.Sprintf("ctx does not contain TokenContextData or unable to cast struct: %T", val),
177+
)
165178
}
166179
return data, nil
167180
}

Diff for: backend/internal/cmds/mgmt/migrate/down/down.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,10 @@ func NewCmd() *cobra.Command {
4747
)
4848
},
4949
}
50-
cmd.Flags().StringP("database", "d", "", "optionally set the database url, otherwise it will pull from the environment")
51-
cmd.Flags().StringP("source", "s", "", "optionally set the migrations dir, otherwise pull from DB_SCHEMA_DIR env")
50+
cmd.Flags().
51+
StringP("database", "d", "", "optionally set the database url, otherwise it will pull from the environment")
52+
cmd.Flags().
53+
StringP("source", "s", "", "optionally set the migrations dir, otherwise pull from DB_SCHEMA_DIR env")
5254
return cmd
5355
}
5456

Diff for: backend/internal/cmds/mgmt/migrate/up/up.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,10 @@ func NewCmd() *cobra.Command {
4747
)
4848
},
4949
}
50-
cmd.Flags().StringP("database", "d", "", "optionally set the database url, otherwise it will pull from the environment")
51-
cmd.Flags().StringP("source", "s", "", "optionally set the migrations dir, otherwise pull from DB_SCHEMA_DIR env")
50+
cmd.Flags().
51+
StringP("database", "d", "", "optionally set the database url, otherwise it will pull from the environment")
52+
cmd.Flags().
53+
StringP("source", "s", "", "optionally set the migrations dir, otherwise pull from DB_SCHEMA_DIR env")
5254
return cmd
5355
}
5456

0 commit comments

Comments
 (0)