Skip to content

Commit 3a79d33

Browse files
committed
simpler solution
Signed-off-by: Austin Abro <[email protected]>
1 parent 42d4dab commit 3a79d33

File tree

1 file changed

+44
-43
lines changed

1 file changed

+44
-43
lines changed

internal/readme/main.go

Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package main
66

77
import (
88
"bytes"
9+
"errors"
910
"fmt"
1011
"io/fs"
1112
"os"
@@ -43,66 +44,66 @@ func findModuleInfo(goModPath string) (module, error) {
4344
dirName := filepath.Base(moduleDir)
4445

4546
description := ""
46-
47-
// Read only files in the root directory of the module
48-
entries, err := os.ReadDir(moduleDir)
49-
if err != nil {
50-
return module{}, fmt.Errorf("failed to read module directory: %w", err)
51-
}
52-
53-
for _, entry := range entries {
54-
// Skip directories and non-Go files
55-
if entry.IsDir() || !strings.HasSuffix(entry.Name(), ".go") {
56-
continue
57-
}
58-
59-
path := filepath.Join(moduleDir, entry.Name())
60-
content, err := os.ReadFile(path)
47+
err = filepath.WalkDir(moduleDir, func(path string, d fs.DirEntry, err error) error {
6148
if err != nil {
62-
continue
49+
return err
6350
}
6451

65-
lines := strings.Split(string(content), "\n")
66-
packageLineIdx := -1
67-
68-
for i, line := range lines {
69-
if strings.HasPrefix(strings.TrimSpace(line), "package ") {
70-
packageLineIdx = i
71-
break
52+
if d.IsDir() && path != moduleDir {
53+
// Only process files in the root directory of the module
54+
if path != moduleDir {
55+
return filepath.SkipDir
7256
}
7357
}
7458

75-
if packageLineIdx > 0 {
76-
for i := packageLineIdx - 1; i >= 0; i-- {
77-
line := strings.TrimSpace(lines[i])
59+
if !d.IsDir() && strings.HasSuffix(d.Name(), ".go") {
60+
content, err := os.ReadFile(path)
61+
if err != nil {
62+
return nil
63+
}
7864

79-
if line == "" || strings.HasPrefix(line, "// SPDX-") {
80-
continue
65+
lines := strings.Split(string(content), "\n")
66+
packageLineIdx := -1
67+
68+
for i, line := range lines {
69+
if strings.HasPrefix(strings.TrimSpace(line), "package ") {
70+
packageLineIdx = i
71+
break
8172
}
73+
}
74+
75+
if packageLineIdx > 0 {
76+
for i := packageLineIdx - 1; i >= 0; i-- {
77+
line := strings.TrimSpace(lines[i])
8278

83-
if strings.HasPrefix(line, "// Package ") {
84-
fields := strings.Fields(line)
85-
if len(fields) < 3 {
86-
break
79+
if line == "" || strings.HasPrefix(line, "// SPDX-") {
80+
continue
8781
}
88-
packageName := fields[2]
8982

90-
pos := strings.Index(line, "// Package ") + len("// Package ") + len(packageName) + 1
83+
if strings.HasPrefix(line, "// Package ") {
84+
fields := strings.Fields(line)
85+
if len(fields) < 3 {
86+
break
87+
}
88+
packageName := fields[2]
9189

92-
if pos < len(line) {
93-
description = line[pos:]
94-
break
90+
pos := strings.Index(line, "// Package ") + len("// Package ") + len(packageName) + 1
91+
92+
if pos < len(line) {
93+
description = line[pos:]
94+
return filepath.SkipAll
95+
}
9596
}
96-
}
9797

98-
break
98+
break
99+
}
99100
}
100101
}
102+
return nil
103+
})
101104

102-
// If we found a description, we can stop searching
103-
if description != "" {
104-
break
105-
}
105+
if err != nil && !errors.Is(err, filepath.SkipAll) {
106+
return module{}, fmt.Errorf("error scanning module directory: %w", err)
106107
}
107108

108109
module := module{

0 commit comments

Comments
 (0)