Skip to content

Commit b9127bc

Browse files
committed
fix: resolve CI lint findings for drive sync/reporting
1 parent 4b6cbe7 commit b9127bc

5 files changed

Lines changed: 44 additions & 46 deletions

File tree

internal/cmd/contacts_dedupe.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func (c *ContactsDedupeCmd) Run(ctx context.Context, flags *RootFlags) error {
3636
return err
3737
}
3838

39-
contacts, err := listContacts(ctx, svc, c.Max)
39+
contacts, err := listContacts(svc, c.Max)
4040
if err != nil {
4141
return err
4242
}
@@ -109,13 +109,13 @@ func parseDedupeMatch(value string) (dedupeMatch, error) {
109109
return out, nil
110110
}
111111

112-
func listContacts(ctx context.Context, svc *people.Service, max int64) ([]*people.Person, error) {
112+
func listContacts(svc *people.Service, maxResults int64) ([]*people.Person, error) {
113113
out := make([]*people.Person, 0, 128)
114114
var pageToken string
115115
for {
116116
pageSize := int64(500)
117-
if max > 0 && max < pageSize {
118-
pageSize = max
117+
if maxResults > 0 && maxResults < pageSize {
118+
pageSize = maxResults
119119
}
120120
call := svc.People.Connections.List(peopleMeResource).
121121
PersonFields(contactsReadMask).
@@ -131,7 +131,7 @@ func listContacts(ctx context.Context, svc *people.Service, max int64) ([]*peopl
131131
continue
132132
}
133133
out = append(out, p)
134-
if max > 0 && int64(len(out)) >= max {
134+
if maxResults > 0 && int64(len(out)) >= maxResults {
135135
return out, nil
136136
}
137137
}

internal/cmd/drive.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ var (
3131
)
3232

3333
const (
34+
driveRootID = "root"
3435
driveMimeFolder = "application/vnd.google-apps.folder"
3536
driveMimeGoogleDoc = "application/vnd.google-apps.document"
3637
driveMimeGoogleSheet = "application/vnd.google-apps.spreadsheet"

internal/cmd/drive_reporting.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func (c *DriveTreeCmd) Run(ctx context.Context, flags *RootFlags) error {
3232

3333
rootID := strings.TrimSpace(c.Parent)
3434
if rootID == "" {
35-
rootID = "root"
35+
rootID = driveRootID
3636
}
3737
depth := c.Depth
3838
if depth < 0 {
@@ -109,7 +109,7 @@ func (c *DriveInventoryCmd) Run(ctx context.Context, flags *RootFlags) error {
109109

110110
rootID := strings.TrimSpace(c.Parent)
111111
if rootID == "" {
112-
rootID = "root"
112+
rootID = driveRootID
113113
}
114114
depth := c.Depth
115115
if depth < 0 {
@@ -193,7 +193,7 @@ func (c *DriveDuCmd) Run(ctx context.Context, flags *RootFlags) error {
193193

194194
rootID := strings.TrimSpace(c.Parent)
195195
if rootID == "" {
196-
rootID = "root"
196+
rootID = driveRootID
197197
}
198198
depth := c.Depth
199199
if depth < 0 {
@@ -291,7 +291,7 @@ const (
291291
func listDriveTree(ctx context.Context, svc *drive.Service, opts driveTreeOptions) ([]driveTreeItem, bool, error) {
292292
rootID := strings.TrimSpace(opts.RootID)
293293
if rootID == "" {
294-
rootID = "root"
294+
rootID = driveRootID
295295
}
296296
fields := strings.TrimSpace(opts.Fields)
297297
if fields == "" {
@@ -351,7 +351,7 @@ func listDriveTree(ctx context.Context, svc *drive.Service, opts driveTreeOption
351351

352352
func listDriveChildren(ctx context.Context, svc *drive.Service, parentID string, fields string) ([]*drive.File, error) {
353353
if parentID == "" {
354-
parentID = "root"
354+
parentID = driveRootID
355355
}
356356
q := buildDriveListQuery(parentID, "")
357357
out := make([]*drive.File, 0, 64)

internal/cmd/drive_reporting_test.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,21 @@ package cmd
33
import "testing"
44

55
func TestSanitizeDriveName(t *testing.T) {
6-
cases := map[string]string{
7-
"": "_",
8-
".": "_",
9-
"..": "_",
10-
"hello": "hello",
11-
"a/b": "a_b",
12-
"a\\b": "a_b",
13-
" foo ": "foo",
14-
}
15-
for input, expected := range cases {
16-
if got := sanitizeDriveName(input); got != expected {
17-
t.Fatalf("sanitizeDriveName(%q) = %q, want %q", input, got, expected)
6+
cases := []struct {
7+
in string
8+
want string
9+
}{
10+
{in: "", want: "_"},
11+
{in: ".", want: "_"},
12+
{in: "..", want: "_"},
13+
{in: "hello", want: "hello"},
14+
{in: "a/b", want: "a_b"},
15+
{in: "a\\b", want: "a_b"},
16+
{in: " foo ", want: "foo"},
17+
}
18+
for _, tc := range cases {
19+
if got := sanitizeDriveName(tc.in); got != tc.want {
20+
t.Fatalf("sanitizeDriveName(%q) = %q, want %q", tc.in, got, tc.want)
1821
}
1922
}
2023
}

internal/cmd/drive_sync.go

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package cmd
22

33
import (
44
"context"
5-
"crypto/md5"
5+
"crypto/md5" // #nosec G501 -- Drive API exposes MD5 checksums; used only for sync change detection.
66
"encoding/hex"
77
"encoding/json"
88
"errors"
@@ -67,8 +67,8 @@ func (c *DriveSyncPullCmd) Run(ctx context.Context, flags *RootFlags) error {
6767
if cfg.FolderID == "" || rootPath == "" {
6868
return usage("missing --folder or --out (or state file)")
6969
}
70-
if err := os.MkdirAll(rootPath, 0o755); err != nil {
71-
return err
70+
if mkdirErr := os.MkdirAll(rootPath, 0o750); mkdirErr != nil {
71+
return mkdirErr
7272
}
7373

7474
svc, err := newDriveService(ctx, account)
@@ -234,7 +234,7 @@ func loadDriveSyncConfig(statePath string, rootPath string, direction string, ac
234234
return "", rootPath, cfg, nil
235235
}
236236

237-
if data, err := os.ReadFile(statePath); err == nil {
237+
if data, err := os.ReadFile(statePath); err == nil { //nolint:gosec // state path is explicit CLI input or derived from local sync root
238238
var stored driveSyncConfig
239239
if jsonErr := json.Unmarshal(data, &stored); jsonErr == nil {
240240
if cfg.FolderID == "" {
@@ -271,8 +271,8 @@ func resolveDriveSyncStatePath(explicit string, rootPath string) (string, error)
271271
return filepath.Join(rootPath, driveSyncStateFile), nil
272272
}
273273

274-
func saveDriveSyncState(path string, cfg driveSyncConfig) error {
275-
if path == "" {
274+
func saveDriveSyncState(statePath string, cfg driveSyncConfig) error {
275+
if statePath == "" {
276276
return nil
277277
}
278278
cfg.Version = driveSyncVersion
@@ -284,7 +284,7 @@ func saveDriveSyncState(path string, cfg driveSyncConfig) error {
284284
}
285285
data = append(data, '\n')
286286

287-
if err := os.WriteFile(path, data, 0o600); err != nil {
287+
if err := os.WriteFile(statePath, data, 0o600); err != nil {
288288
return fmt.Errorf("write sync state: %w", err)
289289
}
290290
return nil
@@ -308,13 +308,13 @@ func splitDriveItems(items []driveTreeItem, exportDocs bool) (map[string]driveTr
308308
folders[it.Path] = it
309309
continue
310310
}
311-
path := it.Path
311+
relPath := it.Path
312312
if exportDocs && strings.HasPrefix(it.MimeType, "application/vnd.google-apps.") {
313313
exportExt := driveExportExtension(driveExportMimeType(it.MimeType))
314-
path = replaceExt(path, exportExt)
314+
relPath = replaceExt(relPath, exportExt)
315315
}
316-
it.Path = path
317-
files[path] = it
316+
it.Path = relPath
317+
files[relPath] = it
318318
}
319319
return files, folders
320320
}
@@ -432,8 +432,8 @@ func ensureDriveSyncExcludes(excludes []string) []string {
432432
return out
433433
}
434434

435-
func fileMD5(path string) (string, error) {
436-
f, err := os.Open(path) //nolint:gosec // user-provided path
435+
func fileMD5(filePath string) (string, error) {
436+
f, err := os.Open(filePath) //nolint:gosec // user-provided path
437437
if err != nil {
438438
return "", err
439439
}
@@ -585,9 +585,9 @@ func ensurePlanDirs(plan *driveSyncPlan, dir string, actionType string) {
585585
}
586586
}
587587

588-
func hasAction(actions []driveSyncAction, actionType string, path string) bool {
588+
func hasAction(actions []driveSyncAction, actionType string, actionPath string) bool {
589589
for _, a := range actions {
590-
if a.Type == actionType && a.Path == path {
590+
if a.Type == actionType && a.Path == actionPath {
591591
return true
592592
}
593593
}
@@ -596,10 +596,7 @@ func hasAction(actions []driveSyncAction, actionType string, path string) bool {
596596

597597
func needsPull(remote driveTreeItem, local localFileInfo, checksum bool) bool {
598598
if checksum && remote.MD5 != "" && local.MD5 != "" {
599-
if remote.MD5 != local.MD5 {
600-
return true
601-
}
602-
return false
599+
return remote.MD5 != local.MD5
603600
}
604601
if remote.Size > 0 && remote.Size != local.Size {
605602
return true
@@ -616,10 +613,7 @@ func needsPush(remote driveTreeItem, local localFileInfo, checksum bool) bool {
616613
return false
617614
}
618615
if checksum && remote.MD5 != "" && local.MD5 != "" {
619-
if remote.MD5 != local.MD5 {
620-
return true
621-
}
622-
return false
616+
return remote.MD5 != local.MD5
623617
}
624618
if remote.Size > 0 && remote.Size != local.Size {
625619
return true
@@ -673,7 +667,7 @@ func applyDrivePullPlan(ctx context.Context, svc *drive.Service, rootPath string
673667
continue
674668
}
675669
dir := filepath.Join(rootPath, filepath.FromSlash(action.Path))
676-
if err := os.MkdirAll(dir, 0o755); err != nil {
670+
if err := os.MkdirAll(dir, 0o750); err != nil {
677671
return err
678672
}
679673
}

0 commit comments

Comments
 (0)