Skip to content

Commit 3a6d950

Browse files
committed
Address linter warnings
1 parent 44d9bc7 commit 3a6d950

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
@@ -42,18 +42,17 @@ func TestServer(t *testing.T) {
4242
testErrCh <- err
4343
return
4444
}
45+
defer resp.Body.Close()
4546

46-
if resp.Body != nil {
47-
all, err := io.ReadAll(resp.Body)
48-
if err != nil {
49-
testErrCh <- err
50-
return
51-
}
52-
all = bytes.TrimSpace(all)
53-
if string(all) != `{"Code":0,"Msg":""}` {
54-
testErrCh <- fmt.Errorf("unexpected response: %s", string(all))
55-
return
56-
}
47+
all, err := io.ReadAll(resp.Body)
48+
if err != nil {
49+
testErrCh <- err
50+
return
51+
}
52+
all = bytes.TrimSpace(all)
53+
if string(all) != `{"Code":0,"Msg":""}` {
54+
testErrCh <- fmt.Errorf("unexpected response: %s", string(all))
55+
return
5756
}
5857
testErrCh <- nil
5958
}()
@@ -101,18 +100,17 @@ func TestServerCmdActions(t *testing.T) {
101100
testErrCh <- err
102101
return
103102
}
103+
defer resp.Body.Close()
104104

105-
if resp.Body != nil {
106-
all, err := io.ReadAll(resp.Body)
107-
if err != nil {
108-
testErrCh <- err
109-
return
110-
}
111-
all = bytes.TrimSpace(all)
112-
if !bytes.HasPrefix(all, []byte(`{"Code":0`)) {
113-
testErrCh <- fmt.Errorf("unexpected response: %s, %x", string(all), all)
114-
return
115-
}
105+
all, err := io.ReadAll(resp.Body)
106+
if err != nil {
107+
testErrCh <- err
108+
return
109+
}
110+
all = bytes.TrimSpace(all)
111+
if !bytes.HasPrefix(all, []byte(`{"Code":0`)) {
112+
testErrCh <- fmt.Errorf("unexpected response: %s, %x", string(all), all)
113+
return
116114
}
117115
testErrCh <- nil
118116
}()
@@ -183,18 +181,17 @@ func TestServerForward(t *testing.T) {
183181
testErrCh <- err
184182
return
185183
}
184+
defer resp.Body.Close()
186185

187-
if resp.Body != nil {
188-
all, err := io.ReadAll(resp.Body)
189-
if err != nil {
190-
testErrCh <- err
191-
return
192-
}
193-
all = bytes.TrimSpace(all)
194-
if string(all) != `{"Code":0,"Msg":""}` {
195-
testErrCh <- fmt.Errorf("unexpected response: %s", string(all))
196-
return
197-
}
186+
all, err := io.ReadAll(resp.Body)
187+
if err != nil {
188+
testErrCh <- err
189+
return
190+
}
191+
all = bytes.TrimSpace(all)
192+
if string(all) != `{"Code":0,"Msg":""}` {
193+
testErrCh <- fmt.Errorf("unexpected response: %s", string(all))
194+
return
198195
}
199196
testErrCh <- nil
200197
}()
@@ -247,18 +244,17 @@ func TestAttendanceAPI(t *testing.T) {
247244
testErrCh <- err
248245
return
249246
}
247+
defer resp.Body.Close()
250248

251-
if resp.Body != nil {
252-
all, err := io.ReadAll(resp.Body)
253-
if err != nil {
254-
testErrCh <- err
255-
return
256-
}
257-
all = bytes.TrimSpace(all)
258-
if string(all) != `{"Code":0,"Msg":""}` {
259-
testErrCh <- fmt.Errorf("unexpected response: %s", string(all))
260-
return
261-
}
249+
all, err := io.ReadAll(resp.Body)
250+
if err != nil {
251+
testErrCh <- err
252+
return
253+
}
254+
all = bytes.TrimSpace(all)
255+
if string(all) != `{"Code":0,"Msg":""}` {
256+
testErrCh <- fmt.Errorf("unexpected response: %s", string(all))
257+
return
262258
}
263259
testErrCh <- nil
264260
}()

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
@@ -23,7 +23,7 @@ func TestProcessResp(t *testing.T) {
2323

2424
func TestM3FinPids(t *testing.T) {
2525
var a = func(pids []int) string {
26-
if len(pids) <= 0 {
26+
if len(pids) == 0 {
2727
return ""
2828
}
2929
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
@@ -88,7 +88,6 @@ func TestFindGCLog(t *testing.T) {
8888
if f != "garbage-collection.log" {
8989
t.Fatal("gc log file should be garbage-collection.log")
9090
}
91-
9291
}
9392

9493
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)