Skip to content

Commit 76d0706

Browse files
committed
Address linter warnings
1 parent 37a045f commit 76d0706

18 files changed

+103
-91
lines changed

.golangci.yml

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,34 @@
11
version: "2"
2+
23
linters:
34
default: standard
45
disable:
5-
- errcheck
6+
- errcheck # Too many issues to fix incrementally (50+)
7+
enable:
8+
- gocyclo
9+
- misspell
10+
- gocritic
11+
- unconvert
12+
- unparam
13+
- revive
14+
- dogsled # Check for too many blank identifiers
15+
- bodyclose # Check HTTP response body is closed
16+
- whitespace # Check for unnecessary whitespace
17+
- wastedassign # Check for wasted assignments
18+
settings:
19+
gocyclo:
20+
min-complexity: 80 # Start lenient, tighten later (default: 30)
21+
gocritic:
22+
disabled-checks:
23+
- ifElseChain # Stylistic preference, not a bug
24+
unparam:
25+
check-exported: false # Only check unexported functions
26+
revive:
27+
rules:
28+
- name: exported
29+
disabled: true # Too many missing comments
30+
- name: package-comments
31+
disabled: true # Too many missing package comments
32+
- name: var-naming
33+
disabled: true # Would require significant refactoring (ApiKey -> APIKey, etc)
634

internal/agent/api/server_test.go

Lines changed: 40 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,17 @@ func TestServer(t *testing.T) {
3737
testErrCh <- err
3838
return
3939
}
40+
defer resp.Body.Close()
4041

41-
if resp.Body != nil {
42-
all, err := io.ReadAll(resp.Body)
43-
if err != nil {
44-
testErrCh <- err
45-
return
46-
}
47-
all = bytes.TrimSpace(all)
48-
if string(all) != `{"Code":0,"Msg":""}` {
49-
testErrCh <- fmt.Errorf("unexpected response: %s", string(all))
50-
return
51-
}
42+
all, err := io.ReadAll(resp.Body)
43+
if err != nil {
44+
testErrCh <- err
45+
return
46+
}
47+
all = bytes.TrimSpace(all)
48+
if string(all) != `{"Code":0,"Msg":""}` {
49+
testErrCh <- fmt.Errorf("unexpected response: %s", string(all))
50+
return
5251
}
5352
testErrCh <- nil
5453
}()
@@ -91,18 +90,17 @@ func TestServerCmdActions(t *testing.T) {
9190
testErrCh <- err
9291
return
9392
}
93+
defer resp.Body.Close()
9494

95-
if resp.Body != nil {
96-
all, err := io.ReadAll(resp.Body)
97-
if err != nil {
98-
testErrCh <- err
99-
return
100-
}
101-
all = bytes.TrimSpace(all)
102-
if !bytes.HasPrefix(all, []byte(`{"Code":0`)) {
103-
testErrCh <- fmt.Errorf("unexpected response: %s, %x", string(all), all)
104-
return
105-
}
95+
all, err := io.ReadAll(resp.Body)
96+
if err != nil {
97+
testErrCh <- err
98+
return
99+
}
100+
all = bytes.TrimSpace(all)
101+
if !bytes.HasPrefix(all, []byte(`{"Code":0`)) {
102+
testErrCh <- fmt.Errorf("unexpected response: %s, %x", string(all), all)
103+
return
106104
}
107105
testErrCh <- nil
108106
}()
@@ -168,18 +166,17 @@ func TestServerForward(t *testing.T) {
168166
testErrCh <- err
169167
return
170168
}
169+
defer resp.Body.Close()
171170

172-
if resp.Body != nil {
173-
all, err := io.ReadAll(resp.Body)
174-
if err != nil {
175-
testErrCh <- err
176-
return
177-
}
178-
all = bytes.TrimSpace(all)
179-
if string(all) != `{"Code":0,"Msg":""}` {
180-
testErrCh <- fmt.Errorf("unexpected response: %s", string(all))
181-
return
182-
}
171+
all, err := io.ReadAll(resp.Body)
172+
if err != nil {
173+
testErrCh <- err
174+
return
175+
}
176+
all = bytes.TrimSpace(all)
177+
if string(all) != `{"Code":0,"Msg":""}` {
178+
testErrCh <- fmt.Errorf("unexpected response: %s", string(all))
179+
return
183180
}
184181
testErrCh <- nil
185182
}()
@@ -227,18 +224,17 @@ func TestAttendanceAPI(t *testing.T) {
227224
testErrCh <- err
228225
return
229226
}
227+
defer resp.Body.Close()
230228

231-
if resp.Body != nil {
232-
all, err := io.ReadAll(resp.Body)
233-
if err != nil {
234-
testErrCh <- err
235-
return
236-
}
237-
all = bytes.TrimSpace(all)
238-
if string(all) != `{"Code":0,"Msg":""}` {
239-
testErrCh <- fmt.Errorf("unexpected response: %s", string(all))
240-
return
241-
}
229+
all, err := io.ReadAll(resp.Body)
230+
if err != nil {
231+
testErrCh <- err
232+
return
233+
}
234+
all = bytes.TrimSpace(all)
235+
if string(all) != `{"Code":0,"Msg":""}` {
236+
testErrCh <- fmt.Errorf("unexpected response: %s", string(all))
237+
return
242238
}
243239
testErrCh <- nil
244240
}()

internal/agent/m3/m3.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ func (m3 *M3App) RunSingle() error {
140140
return err
141141
}
142142

143-
if len(resp) <= 0 {
143+
if len(resp) == 0 {
144144
logger.Log("WARNING: skip empty resp")
145145
return err
146146
}
@@ -184,7 +184,7 @@ func GetM3FinEndpoint(timestamp string, timezone string, pids map[int]string) st
184184
/// append pod name and namespace in Kubernetes
185185
if config.GlobalConfig.Kubernetes {
186186
podName := getPodName()
187-
//parameters += "&pod=" + podName
187+
// parameters += "&pod=" + podName
188188
ns := getMatchingNamespace(podName)
189189
if ns != "" {
190190
// parameters += "&ns=" + ns
@@ -211,7 +211,8 @@ func GetM3CommonEndpointParameters(timestamp string, timezone string) string {
211211
return parameters
212212
}
213213

214-
func (m3 *M3App) captureAndTransmit(pids map[int]string, endpoint string) (err error) {
214+
//nolint:unparam // error return kept for future error handling
215+
func (m3 *M3App) captureAndTransmit(pids map[int]string, endpoint string) error {
215216
logger.Log("yc-360 script version: " + executils.SCRIPT_VERSION)
216217
logger.Log("yc-360 script starting in m3 mode...")
217218

@@ -264,7 +265,7 @@ Resp: %s
264265
--------------------------------
265266
`, lpM3Result.Ok, lpM3Result.Msg)
266267

267-
return
268+
return nil
268269
}
269270

270271
func uploadGCLogM3(endpoint string, pid int) string {
@@ -506,7 +507,7 @@ Resps: %s
506507

507508
func (m3 *M3App) uploadAccessLogM3(endpoint string, pid int, appName string) {
508509
var accessLogM3Chan chan capture.Result
509-
if len(config.GlobalConfig.AccessLogs) <= 0 {
510+
if len(config.GlobalConfig.AccessLogs) == 0 {
510511
return
511512
}
512513

internal/agent/m3/m3_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func TestProcessResp(t *testing.T) {
1818

1919
func TestM3FinPids(t *testing.T) {
2020
var a = func(pids []int) string {
21-
if len(pids) <= 0 {
21+
if len(pids) == 0 {
2222
return ""
2323
}
2424
var ps strings.Builder

internal/agent/ondemand/ondemand.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import (
3636
var Wg sync.WaitGroup
3737

3838
func ProcessPids(pids []int, pid2Name map[int]string, hd bool, tags string, timestamps []string) (rUrls []string, err error) {
39-
if len(pids) <= 0 {
39+
if len(pids) == 0 {
4040
logger.Log("Empty pids, no action needed.")
4141
return
4242
}
@@ -103,7 +103,7 @@ func FullCapture(pid int, appName string, hd bool, tags string, tsParam string)
103103
if tsParam == "" {
104104
tsParam = timestamp
105105
}
106-
//parameters = fmt.Sprintf("de=%s&ts=%s", getOutboundIP().String(), tsParam)
106+
// parameters = fmt.Sprintf("de=%s&ts=%s", getOutboundIP().String(), tsParam)
107107
timezoneBase64 := base64.StdEncoding.EncodeToString([]byte(timezone))
108108
parameters = fmt.Sprintf("de=%s&ts=%s&timezoneId=%s", getOutboundIP().String(), tsParam, timezoneBase64)
109109
endpoint = fmt.Sprintf("%s/ycrash-receiver?%s", config.GlobalConfig.Server, parameters)
@@ -179,7 +179,6 @@ func FullCapture(pid int, appName string, hd bool, tags string, tsParam string)
179179
// A.3 Agent log file
180180
var agentLogFile *os.File
181181
if !config.GlobalConfig.M3 {
182-
183182
// Renaming the log file name to yc360Logs from agentlog
184183
agentLogFile, err = logger.StartWritingToFile("yc360Logs.out")
185184
if err != nil {
@@ -376,7 +375,6 @@ Ignored errors: %v
376375
// ------------------------------------------------------------------------------
377376
var appLogs chan capture.Result
378377
if len(config.GlobalConfig.AppLogs) > 0 && config.GlobalConfig.AppLogLineCount != 0 {
379-
380378
appLogsContainDollarSign := false
381379
for _, configAppLog := range config.GlobalConfig.AppLogs {
382380
if strings.Contains(string(configAppLog), "$") {
@@ -417,7 +415,6 @@ Falling back to capture all configured appLogs without appName filtering.`)
417415
if found {
418416
appLogsMatchingAppName = append(appLogsMatchingAppName, config.AppLog(beforeSearchToken))
419417
}
420-
421418
}
422419

423420
if len(appLogsMatchingAppName) > 0 {
@@ -454,7 +451,7 @@ Falling back to capture all configured appLogs without appName filtering.`)
454451

455452
paths := config.AppLogs{}
456453
for _, f := range discoveredLogFiles {
457-
logger.Debug().Msgf("OnDemand FullCapture: discovered log file: %s", string(f))
454+
logger.Debug().Msgf("OnDemand FullCapture: discovered log file: %s", f)
458455
isGCLog := false
459456
for _, fileName := range globFiles {
460457
// To exclude discovered gc log such f as /tmp/buggyapp-%p-%t.log
@@ -778,7 +775,6 @@ Resp: %s
778775
resp, err := RequestFin(finEp)
779776
if err != nil {
780777
logger.Log("post yc-fin err %s", err.Error())
781-
err = nil
782778
}
783779

784780
endTime := time.Now()

internal/agent/ondemand/ondemand_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ func TestFindGCLog(t *testing.T) {
8383
if f != "garbage-collection.log" {
8484
t.Fatal("gc log file should be garbage-collection.log")
8585
}
86-
8786
}
8887

8988
func TestPostData(t *testing.T) {

internal/capture/access_log.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
const accessLogOut = "accesslog.out"
1111

1212
// AccessLog represents struct that captures and uploads the specified log file.
13+
//
1314
// Deprecated: use App logs auto discovery instead.
1415
type AccessLog struct {
1516
Capture

internal/capture/accesslogm3.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func (a *AccessLogM3) Run() (Result, error) {
7878
// %t = current date
7979
p := strings.ReplaceAll(string(path.Path), "%t", currentDateStr)
8080

81-
matches, err := zglob.Glob(string(p))
81+
matches, err := zglob.Glob(p)
8282

8383
if err != nil {
8484
results = append(results, Result{

internal/capture/app_logs_auto_discovery.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func DiscoverOpenedLogFilesByProcess(pid int) ([]string, error) {
3535
}
3636

3737
for _, filePath := range openedFiles {
38-
logger.Debug().Msgf("DiscoverOpenedLogFilesByProcess: opened file by process (pid=%d): %s", pid, string(filePath))
38+
logger.Debug().Msgf("DiscoverOpenedLogFilesByProcess: opened file by process (pid=%d): %s", pid, filePath)
3939

4040
fileBaseName := filepath.Base(filePath)
4141
if matchLogPattern(fileBaseName) {

internal/capture/applog.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func (al *AppLog) Run() (Result, error) {
5252

5353
// Process each expanded file path
5454
for _, filePath := range expandedPaths {
55-
logger.Debug().Msgf("AppLog: expanded path: %s", string(filePath))
55+
logger.Debug().Msgf("AppLog: expanded path: %s", filePath)
5656

5757
r, err := al.CaptureSingleAppLog(filePath)
5858
results = append(results, r)

0 commit comments

Comments
 (0)