Skip to content

Commit 6e348a9

Browse files
committed
style: standardize multiline formatting for functions and assertions
- Add golines as an additional formatter to the linter configuration - Refactor all multi-argument function signatures to use one-argument-per-line formatting - Improve formatting in test assertions for clarity and consistency - Refactor groupings of tokens in notification test cases for readability - Standardize fmt.Sprintf calls to multi-line format where arguments are long - Make function signatures consistent across files, enhancing code readability Signed-off-by: appleboy <[email protected]>
1 parent 18b9e07 commit 6e348a9

16 files changed

+165
-36
lines changed

.golangci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ formatters:
4242
- gofmt
4343
- gofumpt
4444
- goimports
45+
- golines
4546
exclusions:
4647
generated: lax
4748
paths:

app/options.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,12 @@ func (o *Options) BindFlags() {
5757

5858
// Huawei options
5959
flag.StringVar(&o.Conf.Huawei.AppSecret, "hk", "", "Huawei api key configuration for gorush")
60-
flag.StringVar(&o.Conf.Huawei.AppSecret, "hmskey", "", "Huawei api key configuration for gorush")
60+
flag.StringVar(
61+
&o.Conf.Huawei.AppSecret,
62+
"hmskey",
63+
"",
64+
"Huawei api key configuration for gorush",
65+
)
6166
flag.StringVar(&o.Conf.Huawei.AppID, "hid", "", "HMS app id configuration for gorush")
6267
flag.StringVar(&o.Conf.Huawei.AppID, "hmsid", "", "HMS app id configuration for gorush")
6368
flag.BoolVar(&o.Conf.Huawei.Enabled, "huawei", false, "send huawei notification")

app/sender.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,12 @@ func SendIOSNotification(ctx context.Context, cfg *config.ConfYaml, opts CLISend
118118
}
119119

120120
// SendNotification sends a notification based on platform type.
121-
func SendNotification(ctx context.Context, platform int, cfg *config.ConfYaml, opts CLISendOptions) error {
121+
func SendNotification(
122+
ctx context.Context,
123+
platform int,
124+
cfg *config.ConfYaml,
125+
opts CLISendOptions,
126+
) error {
122127
switch platform {
123128
case core.PlatFormAndroid:
124129
return SendAndroidNotification(ctx, cfg, opts)

config/config_test.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,11 @@ func (suite *ConfigTestSuite) TestValidateConf() {
202202
assert.Equal(suite.T(), "", suite.ConfGorush.Core.FeedbackURL)
203203
assert.Equal(suite.T(), int64(10), suite.ConfGorush.Core.FeedbackTimeout)
204204
assert.Equal(suite.T(), 1, len(suite.ConfGorush.Core.FeedbackHeader))
205-
assert.Equal(suite.T(), "x-gorush-token:4e989115e09680f44a645519fed6a976", suite.ConfGorush.Core.FeedbackHeader[0])
205+
assert.Equal(
206+
suite.T(),
207+
"x-gorush-token:4e989115e09680f44a645519fed6a976",
208+
suite.ConfGorush.Core.FeedbackHeader[0],
209+
)
206210
assert.Equal(suite.T(), false, suite.ConfGorush.Core.SSL)
207211
assert.Equal(suite.T(), "cert.pem", suite.ConfGorush.Core.CertPath)
208212
assert.Equal(suite.T(), "key.pem", suite.ConfGorush.Core.KeyPath)
@@ -474,7 +478,11 @@ func TestValidateAddress(t *testing.T) {
474478
return
475479
}
476480
if tt.errMsg != "" && !strings.Contains(err.Error(), tt.errMsg) {
477-
t.Errorf("ValidateAddress() error = %v, want error containing %v", err, tt.errMsg)
481+
t.Errorf(
482+
"ValidateAddress() error = %v, want error containing %v",
483+
err,
484+
tt.errMsg,
485+
)
478486
}
479487
} else if err != nil {
480488
t.Errorf("ValidateAddress() error = %v, want nil", err)
@@ -574,7 +582,11 @@ func TestValidatePIDPath(t *testing.T) {
574582
return
575583
}
576584
if tt.errMsg != "" && !strings.Contains(err.Error(), tt.errMsg) {
577-
t.Errorf("ValidatePIDPath() error = %v, want error containing %v", err, tt.errMsg)
585+
t.Errorf(
586+
"ValidatePIDPath() error = %v, want error containing %v",
587+
err,
588+
tt.errMsg,
589+
)
578590
}
579591
} else if err != nil {
580592
t.Errorf("ValidatePIDPath() error = %v, want nil", err)
@@ -625,7 +637,11 @@ func TestValidateConfig(t *testing.T) {
625637
}
626638

627639
if tt.errMsg != "" && !strings.Contains(err.Error(), tt.errMsg) {
628-
t.Errorf("ValidateConfig() error = %v, want error containing %v", err, tt.errMsg)
640+
t.Errorf(
641+
"ValidateConfig() error = %v, want error containing %v",
642+
err,
643+
tt.errMsg,
644+
)
629645
}
630646
} else if err != nil {
631647
t.Errorf("ValidateConfig() error = %v, want nil", err)

logx/log.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type LogPushEntry struct {
3333

3434
var isTerm bool
3535

36-
//nolint
36+
// nolint
3737
func init() {
3838
isTerm = isatty.IsTerminal(os.Stdout.Fd())
3939
}

notify/feedback.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,13 @@ func extractHeaders(headers []string) map[string]string {
3434
//
3535
// Returns:
3636
// - error: An error if the request fails or the response status is not OK.
37-
func DispatchFeedback(ctx context.Context, log logx.LogPushEntry, url string, timeout int64, header []string) error {
37+
func DispatchFeedback(
38+
ctx context.Context,
39+
log logx.LogPushEntry,
40+
url string,
41+
timeout int64,
42+
header []string,
43+
) error {
3844
if url == "" {
3945
return errors.New("url can't be empty")
4046
}

notify/notification.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ type PushNotification struct {
6868
ID string `json:"notif_id,omitempty"`
6969
To string `json:"to,omitempty"`
7070
Topic string `json:"topic,omitempty"` // FCM and iOS only
71-
Tokens []string `json:"tokens" binding:"required"`
72-
Platform int `json:"platform" binding:"required"`
71+
Tokens []string `json:"tokens" binding:"required"`
72+
Platform int `json:"platform" binding:"required"`
7373
Message string `json:"message,omitempty"`
7474
Title string `json:"title,omitempty"`
7575
Image string `json:"image,omitempty"`
@@ -286,7 +286,13 @@ func SendNotification(
286286
}
287287

288288
for _, l := range logs {
289-
err := DispatchFeedback(ctx, l, cfg.Core.FeedbackURL, cfg.Core.FeedbackTimeout, cfg.Core.FeedbackHeader)
289+
err := DispatchFeedback(
290+
ctx,
291+
l,
292+
cfg.Core.FeedbackURL,
293+
cfg.Core.FeedbackTimeout,
294+
cfg.Core.FeedbackHeader,
295+
)
290296
if err != nil {
291297
logx.LogError.Error(err)
292298
}

notify/notification_apns.go

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ type Sound struct {
5959
}
6060

6161
// loadCertFromFile loads certificate or auth key from a file path.
62-
func loadCertFromFile(keyPath, password string) (tls.Certificate, *ecdsa.PrivateKey, string, error) {
62+
func loadCertFromFile(
63+
keyPath, password string,
64+
) (tls.Certificate, *ecdsa.PrivateKey, string, error) {
6365
var cert tls.Certificate
6466
var authKey *ecdsa.PrivateKey
6567
var err error
@@ -80,7 +82,9 @@ func loadCertFromFile(keyPath, password string) (tls.Certificate, *ecdsa.Private
8082
}
8183

8284
// loadCertFromBase64 loads certificate or auth key from base64 encoded data.
83-
func loadCertFromBase64(keyBase64, keyType, password string) (tls.Certificate, *ecdsa.PrivateKey, string, error) {
85+
func loadCertFromBase64(
86+
keyBase64, keyType, password string,
87+
) (tls.Certificate, *ecdsa.PrivateKey, string, error) {
8488
var cert tls.Certificate
8589
var authKey *ecdsa.PrivateKey
8690

@@ -105,7 +109,10 @@ func loadCertFromBase64(keyBase64, keyType, password string) (tls.Certificate, *
105109
}
106110

107111
// createAPNSClientWithToken creates an APNS client using a p8 token.
108-
func createAPNSClientWithToken(cfg *config.ConfYaml, authKey *ecdsa.PrivateKey) (*apns2.Client, error) {
112+
func createAPNSClientWithToken(
113+
cfg *config.ConfYaml,
114+
authKey *ecdsa.PrivateKey,
115+
) (*apns2.Client, error) {
109116
if cfg.Ios.KeyID == "" || cfg.Ios.TeamID == "" {
110117
return nil, errors.New("you should provide ios.KeyID and ios.TeamID for p8 token")
111118
}
@@ -134,7 +141,11 @@ func InitAPNSClient(ctx context.Context, cfg *config.ConfYaml) error {
134141
case cfg.Ios.KeyPath != "":
135142
certificateKey, authKey, ext, err = loadCertFromFile(cfg.Ios.KeyPath, cfg.Ios.Password)
136143
case cfg.Ios.KeyBase64 != "":
137-
certificateKey, authKey, ext, err = loadCertFromBase64(cfg.Ios.KeyBase64, cfg.Ios.KeyType, cfg.Ios.Password)
144+
certificateKey, authKey, ext, err = loadCertFromBase64(
145+
cfg.Ios.KeyBase64,
146+
cfg.Ios.KeyType,
147+
cfg.Ios.Password,
148+
)
138149
}
139150

140151
if err != nil {
@@ -316,7 +327,10 @@ func setLiveActivityFields(p *payload.Payload, req *PushNotification) {
316327
}
317328
}
318329

319-
func iosAlertDictionary(notificationPayload *payload.Payload, req *PushNotification) *payload.Payload {
330+
func iosAlertDictionary(
331+
notificationPayload *payload.Payload,
332+
req *PushNotification,
333+
) *payload.Payload {
320334
if len(req.InterruptionLevel) > 0 {
321335
notificationPayload.InterruptionLevel(payload.EInterruptionLevel(req.InterruptionLevel))
322336
}
@@ -446,7 +460,11 @@ func getApnsClient(cfg *config.ConfYaml, req *PushNotification) (client *apns2.C
446460
}
447461

448462
// PushToIOS provide send notification to APNs server.
449-
func PushToIOS(ctx context.Context, req *PushNotification, cfg *config.ConfYaml) (resp *ResponsePush, err error) {
463+
func PushToIOS(
464+
ctx context.Context,
465+
req *PushNotification,
466+
cfg *config.ConfYaml,
467+
) (resp *ResponsePush, err error) {
450468
logx.LogAccess.Debug("Start push notification for iOS")
451469

452470
var (

notify/notification_apns_test.go

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,16 @@ func TestIOSSummaryArg(t *testing.T) {
264264
assert.Equal(t, testMessage, notification.ApnsID)
265265
assert.Equal(t, testMessage, notification.Topic)
266266
assert.Equal(t, ApnsPriorityLow, notification.Priority)
267-
assert.Equal(t, "test", dat["aps"].(map[string]interface{})["alert"].(map[string]interface{})["summary-arg"])
268-
assert.Equal(t, float64(3), dat["aps"].(map[string]interface{})["alert"].(map[string]interface{})["summary-arg-count"])
267+
assert.Equal(
268+
t,
269+
"test",
270+
dat["aps"].(map[string]interface{})["alert"].(map[string]interface{})["summary-arg"],
271+
)
272+
assert.Equal(
273+
t,
274+
float64(3),
275+
dat["aps"].(map[string]interface{})["alert"].(map[string]interface{})["summary-arg-count"],
276+
)
269277
}
270278

271279
// Silent Notification which payload’s aps dictionary must not contain the alert, sound, or badge keys.
@@ -414,9 +422,21 @@ func TestAlertStringExample2ForIos(t *testing.T) {
414422
panic(err)
415423
}
416424

417-
assert.Equal(t, title, dat["aps"].(map[string]interface{})["alert"].(map[string]interface{})["title"])
418-
assert.Equal(t, body, dat["aps"].(map[string]interface{})["alert"].(map[string]interface{})["body"])
419-
assert.Equal(t, actionLocKey, dat["aps"].(map[string]interface{})["alert"].(map[string]interface{})["action-loc-key"])
425+
assert.Equal(
426+
t,
427+
title,
428+
dat["aps"].(map[string]interface{})["alert"].(map[string]interface{})["title"],
429+
)
430+
assert.Equal(
431+
t,
432+
body,
433+
dat["aps"].(map[string]interface{})["alert"].(map[string]interface{})["body"],
434+
)
435+
assert.Equal(
436+
t,
437+
actionLocKey,
438+
dat["aps"].(map[string]interface{})["alert"].(map[string]interface{})["action-loc-key"],
439+
)
420440
}
421441

422442
// URL: https://goo.gl/5xFo3C
@@ -769,7 +789,10 @@ func TestPushToIOS(t *testing.T) {
769789

770790
req := &PushNotification{
771791
//nolint
772-
Tokens: []string{"11aa01229f15f0f0c52029d8cf8cd0aeaf2365fe4cebc4af26cd6d76b7919ef7", "11aa01229f15f0f0c52029d8cf8cd0aeaf2365fe4cebc4af26cd6d76b7919ef1"},
792+
Tokens: []string{
793+
"11aa01229f15f0f0c52029d8cf8cd0aeaf2365fe4cebc4af26cd6d76b7919ef7",
794+
"11aa01229f15f0f0c52029d8cf8cd0aeaf2365fe4cebc4af26cd6d76b7919ef1",
795+
},
773796
Platform: 1,
774797
Message: "Welcome",
775798
}
@@ -1125,7 +1148,11 @@ func TestLoadCertFromBase64(t *testing.T) {
11251148
assert.Equal(t, ".invalid", invalidExt)
11261149

11271150
// Test invalid base64
1128-
badBase64Cert, badBase64AuthKey, badBase64Ext, err := loadCertFromBase64("not-valid-base64!!!", "p12", "")
1151+
badBase64Cert, badBase64AuthKey, badBase64Ext, err := loadCertFromBase64(
1152+
"not-valid-base64!!!",
1153+
"p12",
1154+
"",
1155+
)
11291156
assert.Error(t, err)
11301157
assert.Empty(t, badBase64Cert.Certificate)
11311158
assert.Nil(t, badBase64AuthKey)

notify/notification_fcm.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,11 @@ func logDevMessages(messages []*messaging.Message) {
259259
}
260260

261261
// PushToAndroid provide send notification to Android server.
262-
func PushToAndroid(ctx context.Context, req *PushNotification, cfg *config.ConfYaml) (resp *ResponsePush, err error) {
262+
func PushToAndroid(
263+
ctx context.Context,
264+
req *PushNotification,
265+
cfg *config.ConfYaml,
266+
) (resp *ResponsePush, err error) {
263267
logx.LogAccess.Debug("Start push notification for Android")
264268

265269
if err = CheckMessage(req); err != nil {
@@ -298,7 +302,13 @@ Retry:
298302
return resp, newErr
299303
}
300304

301-
logx.LogAccess.Debug(fmt.Sprintf("Android Success count: %d, Failure count: %d", res.SuccessCount, res.FailureCount))
305+
logx.LogAccess.Debug(
306+
fmt.Sprintf(
307+
"Android Success count: %d, Failure count: %d",
308+
res.SuccessCount,
309+
res.FailureCount,
310+
),
311+
)
302312
status.StatStorage.AddAndroidSuccess(int64(res.SuccessCount))
303313
status.StatStorage.AddAndroidError(int64(res.FailureCount))
304314

@@ -318,7 +328,12 @@ Retry:
318328
return resp, nil
319329
}
320330

321-
func logPush(cfg *config.ConfYaml, status, token string, req *PushNotification, err error) logx.LogPushEntry {
331+
func logPush(
332+
cfg *config.ConfYaml,
333+
status, token string,
334+
req *PushNotification,
335+
err error,
336+
) logx.LogPushEntry {
322337
return logx.LogPush(&logx.InputLog{
323338
ID: req.ID,
324339
Status: status,

0 commit comments

Comments
 (0)