Skip to content

Commit 18606aa

Browse files
committed
chore: linter fixes and code quality improvements
- Use errors.As() instead of type assertions for error checking - Check return values from fmt.Fprintln calls - Simplify regex patterns (remove unnecessary escapes in char classes) - Use idiomatic variable declarations (var x []T vs x := []T{}) - Name unused parameters with underscore (_) - Improve comment grammar - Standardize shell script indentation Signed-off-by: Jose Alekhinne <alekhinejose@gmail.com>
1 parent ae79063 commit 18606aa

18 files changed

Lines changed: 154 additions & 93 deletions

cmd/ctx/main.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ func init() {
4646

4747
func main() {
4848
if err := rootCmd.Execute(); err != nil {
49-
fmt.Fprintln(os.Stderr, err)
49+
_, err := fmt.Fprintln(os.Stderr, err)
50+
if err != nil {
51+
return
52+
}
5053
os.Exit(1)
5154
}
5255
}

hack/build-all.sh

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ MODULE_PATH="./cmd/ctx"
2323

2424
# Build targets: OS/ARCH pairs
2525
TARGETS=(
26-
"darwin/amd64"
27-
"darwin/arm64"
28-
"linux/amd64"
29-
"linux/arm64"
30-
"windows/amd64"
31-
"windows/arm64"
26+
"darwin/amd64"
27+
"darwin/arm64"
28+
"linux/amd64"
29+
"linux/arm64"
30+
"windows/amd64"
31+
"windows/arm64"
3232
)
3333

3434
echo "Building Context CLI v${VERSION}"
@@ -40,20 +40,20 @@ mkdir -p "${OUTPUT_DIR}"
4040

4141
# Build for each target
4242
for target in "${TARGETS[@]}"; do
43-
GOOS="${target%/*}"
44-
GOARCH="${target#*/}"
43+
GOOS="${target%/*}"
44+
GOARCH="${target#*/}"
4545

46-
output_name="${BINARY_NAME}-${GOOS}-${GOARCH}"
47-
if [ "${GOOS}" = "windows" ]; then
48-
output_name="${output_name}.exe"
49-
fi
46+
output_name="${BINARY_NAME}-${GOOS}-${GOARCH}"
47+
if [ "${GOOS}" = "windows" ]; then
48+
output_name="${output_name}.exe"
49+
fi
5050

51-
echo "Building ${GOOS}/${GOARCH}..."
51+
echo "Building ${GOOS}/${GOARCH}..."
5252

53-
CGO_ENABLED=0 GOOS="${GOOS}" GOARCH="${GOARCH}" go build \
54-
-ldflags="-s -w -X main.Version=${VERSION}" \
55-
-o "${OUTPUT_DIR}/${output_name}" \
56-
"${MODULE_PATH}"
53+
CGO_ENABLED=0 GOOS="${GOOS}" GOARCH="${GOARCH}" go build \
54+
-ldflags="-s -w -X main.Version=${VERSION}" \
55+
-o "${OUTPUT_DIR}/${output_name}" \
56+
"${MODULE_PATH}"
5757
done
5858

5959
echo ""
@@ -65,9 +65,9 @@ echo ""
6565
echo "Creating checksums..."
6666
cd "${OUTPUT_DIR}"
6767
if command -v sha256sum &> /dev/null; then
68-
sha256sum ctx-* > checksums.txt
68+
sha256sum ctx-* > checksums.txt
6969
elif command -v shasum &> /dev/null; then
70-
shasum -a 256 ctx-* > checksums.txt
70+
shasum -a 256 ctx-* > checksums.txt
7171
fi
7272
cd ..
7373

hack/start-dogfood.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,8 @@ echo ""
138138
if [ -d "${SOURCE_DIR}/specs" ]; then
139139
echo "Copying specs/ for reference..."
140140
cp -r "${SOURCE_DIR}/specs" "./specs"
141-
echo -e "${GREEN}Installed:${NC} specs/ ($(find specs -maxdepth 1 -name "*.md" 2>/dev/null | wc -l) spec files)"
141+
echo -e "${GREEN}Installed:${NC} specs/ \
142+
($(find specs -maxdepth 1 -name "*.md" 2>/dev/null | wc -l) spec files)"
142143
echo ""
143144
fi
144145

internal/cli/add.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func runAdd(cmd *cobra.Command, args []string) error {
7373

7474
filePath := filepath.Join(contextDirName, fileName)
7575

76-
// Check if file exists
76+
// Check if the file exists
7777
if _, err := os.Stat(filePath); os.IsNotExist(err) {
7878
return fmt.Errorf("context file %s not found. Run 'ctx init' first", filePath)
7979
}

internal/cli/agent.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package cli
88

99
import (
1010
"encoding/json"
11+
"errors"
1112
"fmt"
1213
"regexp"
1314
"strings"
@@ -59,10 +60,11 @@ type AgentPacket struct {
5960
Decisions []string `json:"decisions"`
6061
}
6162

62-
func runAgent(cmd *cobra.Command, args []string) error {
63+
func runAgent(cmd *cobra.Command, _ []string) error {
6364
ctx, err := context.Load("")
6465
if err != nil {
65-
if _, ok := err.(*context.NotFoundError); ok {
66+
var notFoundError *context.NotFoundError
67+
if errors.As(err, &notFoundError) {
6668
return fmt.Errorf("no .context/ directory found. Run 'ctx init' first")
6769
}
6870
return err
@@ -151,7 +153,7 @@ func outputAgentMarkdown(cmd *cobra.Command, ctx *context.Context) error {
151153
}
152154

153155
func getReadOrder(ctx *context.Context) []string {
154-
order := []string{}
156+
var order []string
155157
for _, name := range fileReadOrder {
156158
for _, f := range ctx.Files {
157159
if f.Name == name && !f.IsEmpty {
@@ -200,7 +202,7 @@ func extractRecentDecisions(ctx *context.Context, limit int) []string {
200202
}
201203

202204
func extractCheckboxItems(content string) []string {
203-
re := regexp.MustCompile(`(?m)^-\s*\[[ x]\]\s*(.+)$`)
205+
re := regexp.MustCompile(`(?m)^-\s*\[[ x]]\s*(.+)$`)
204206
matches := re.FindAllStringSubmatch(content, -1)
205207
items := make([]string, 0, len(matches))
206208
for _, m := range matches {
@@ -210,7 +212,7 @@ func extractCheckboxItems(content string) []string {
210212
}
211213

212214
func extractUncheckedTasks(content string) []string {
213-
re := regexp.MustCompile(`(?m)^-\s*\[\s*\]\s*(.+)$`)
215+
re := regexp.MustCompile(`(?m)^-\s*\[\s*]\s*(.+)$`)
214216
matches := re.FindAllStringSubmatch(content, -1)
215217
items := make([]string, 0, len(matches))
216218
for _, m := range matches {
@@ -237,7 +239,7 @@ func extractBulletItems(content string, limit int) []string {
237239
}
238240

239241
func extractDecisionTitles(content string, limit int) []string {
240-
re := regexp.MustCompile(`(?m)^##\s+\[[\d-]+\]\s*(.+)$`)
242+
re := regexp.MustCompile(`(?m)^##\s+\[[\d-]+]\s*(.+)$`)
241243
matches := re.FindAllStringSubmatch(content, -1)
242244
items := make([]string, 0, limit)
243245
// Get the most recent (last) decisions

internal/cli/compact.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package cli
88

99
import (
10+
"errors"
1011
"fmt"
1112
"os"
1213
"path/filepath"
@@ -47,10 +48,11 @@ Use --archive to create .context/archive/ for old content.`,
4748
return cmd
4849
}
4950

50-
func runCompact(cmd *cobra.Command, args []string) error {
51+
func runCompact(cmd *cobra.Command, _ []string) error {
5152
ctx, err := context.Load("")
5253
if err != nil {
53-
if _, ok := err.(*context.NotFoundError); ok {
54+
var notFoundError *context.NotFoundError
55+
if errors.As(err, &notFoundError) {
5456
return fmt.Errorf("no .context/ directory found. Run 'ctx init' first")
5557
}
5658
return err
@@ -120,7 +122,7 @@ func compactTasks(cmd *cobra.Command, ctx *context.Context, archive bool) (int,
120122
content := string(tasksFile.Content)
121123
lines := strings.Split(content, "\n")
122124

123-
completedPattern := regexp.MustCompile(`^-\s*\[x\]\s*(.+)$`)
125+
completedPattern := regexp.MustCompile(`^-\s*\[x]\s*(.+)$`)
124126

125127
var completedTasks []string
126128
var newLines []string
@@ -140,7 +142,7 @@ func compactTasks(cmd *cobra.Command, ctx *context.Context, archive bool) (int,
140142
inCompletedSection = false
141143
}
142144

143-
// If completed task outside Completed section, collect it
145+
// If completed task outside the Completed section, collect it
144146
if !inCompletedSection && completedPattern.MatchString(line) {
145147
matches := completedPattern.FindStringSubmatch(line)
146148
if len(matches) > 1 {
@@ -216,7 +218,7 @@ func removeEmptySections(content string) (string, int) {
216218

217219
// Check if this is a section header
218220
if strings.HasPrefix(line, "## ") {
219-
// Look ahead to see if section is empty
221+
// Look ahead to see if the section is empty
220222
sectionStart := i
221223
i++
222224

@@ -225,7 +227,7 @@ func removeEmptySections(content string) (string, int) {
225227
i++
226228
}
227229

228-
// Check if we hit another section or end of file
230+
// Check if we hit another section or end of the file
229231
if i >= len(lines) || strings.HasPrefix(lines[i], "## ") || strings.HasPrefix(lines[i], "# ") {
230232
// Section is empty, skip it
231233
removed++
@@ -269,7 +271,7 @@ func preCompactAutoSave(cmd *cobra.Command) error {
269271
// Build minimal session content
270272
content := buildPreCompactSession(now)
271273

272-
// Write file
274+
// Write the file
273275
if err := os.WriteFile(filePath, []byte(content), 0644); err != nil {
274276
return fmt.Errorf("failed to write session file: %w", err)
275277
}

internal/cli/complete.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func runComplete(cmd *cobra.Command, args []string) error {
5656

5757
// Parse tasks and find matching one
5858
lines := strings.Split(string(content), "\n")
59-
taskPattern := regexp.MustCompile(`^(\s*)-\s*\[\s*\]\s*(.+)$`)
59+
taskPattern := regexp.MustCompile(`^(\s*)-\s*\[\s*]\s*(.+)$`)
6060

6161
var taskNumber int
6262
isNumber := false

internal/cli/drift.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package cli
88

99
import (
1010
"encoding/json"
11+
"errors"
1112
"fmt"
1213
"time"
1314

@@ -54,10 +55,11 @@ type DriftJSONOutput struct {
5455
Passed []string `json:"passed"`
5556
}
5657

57-
func runDrift(cmd *cobra.Command, args []string) error {
58+
func runDrift(cmd *cobra.Command, _ []string) error {
5859
ctx, err := context.Load("")
5960
if err != nil {
60-
if _, ok := err.(*context.NotFoundError); ok {
61+
var notFoundError *context.NotFoundError
62+
if errors.As(err, &notFoundError) {
6163
return fmt.Errorf("no .context/ directory found. Run 'ctx init' first")
6264
}
6365
return err
@@ -118,9 +120,9 @@ func outputDriftText(cmd *cobra.Command, report *drift.Report) error {
118120
cmd.Printf("%s WARNINGS (%d)\n\n", yellow("⚠️ "), len(report.Warnings))
119121

120122
// Group by type
121-
pathRefs := []drift.Issue{}
122-
staleness := []drift.Issue{}
123-
other := []drift.Issue{}
123+
var pathRefs []drift.Issue
124+
var staleness []drift.Issue
125+
var other []drift.Issue
124126

125127
for _, w := range report.Warnings {
126128
switch w.Type {

internal/cli/load.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package cli
88

99
import (
10+
"errors"
1011
"fmt"
1112
"sort"
1213
"strings"
@@ -62,10 +63,11 @@ var fileReadOrder = []string{
6263
"AGENT_PLAYBOOK.md",
6364
}
6465

65-
func runLoad(cmd *cobra.Command, args []string) error {
66+
func runLoad(cmd *cobra.Command, _ []string) error {
6667
ctx, err := context.Load("")
6768
if err != nil {
68-
if _, ok := err.(*context.NotFoundError); ok {
69+
var notFoundError *context.NotFoundError
70+
if errors.As(err, &notFoundError) {
6971
return fmt.Errorf("no .context/ directory found. Run 'ctx init' first")
7072
}
7173
return err
@@ -110,15 +112,15 @@ func outputAssembled(cmd *cobra.Command, ctx *context.Context, budget int) error
110112
continue
111113
}
112114

113-
// Check if we have budget for this file
115+
// Check if we have the budget for this file
114116
fileTokens := f.Tokens
115117
if tokensUsed+fileTokens > budget {
116-
// Add truncation notice
118+
// Add a truncation notice
117119
sb.WriteString(fmt.Sprintf("\n---\n\n*[Truncated: %s and remaining files excluded due to token budget]*\n", f.Name))
118120
break
119121
}
120122

121-
// Add file section
123+
// Add the file section
122124
sb.WriteString(fmt.Sprintf("## %s\n\n", fileNameToTitle(f.Name)))
123125
sb.Write(f.Content)
124126
if !strings.HasSuffix(string(f.Content), "\n") {

0 commit comments

Comments
 (0)