Skip to content

Commit 09c5c54

Browse files
committed
refactor: consolidate constants, add thread safety, reduce duplication
- Add config constants: FileContextRC, EnvCtxDir, EnvCtxTokenBudget, ParserPeekLines, Claude API block types and roles - Add RWMutex to rc package for thread-safe rcOverrideDir access - Use t.Setenv in tests instead of os.Setenv/Unsetenv - Extract ReindexFile helper to eliminate decision/learnings duplication - ScanDirectory now delegates to ScanDirectoryWithErrors - Use iota for task match indices - Add EntryPlural map for entry type pluralization - Add comprehensive tests for config and task packages - Add proper documentation following project conventions Signed-off-by: Jose Alekhinne <alekhinejose@gmail.com>
1 parent 59784a0 commit 09c5c54

54 files changed

Lines changed: 1461 additions & 535 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

internal/cli/decision/run.go

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,8 @@
77
package decision
88

99
import (
10-
"fmt"
11-
"os"
1210
"path/filepath"
1311

14-
"github.com/fatih/color"
1512
"github.com/spf13/cobra"
1613

1714
"github.com/ActiveMemory/ctx/internal/config"
@@ -28,33 +25,12 @@ import (
2825
// Returns:
2926
// - error: Non-nil if file read/write fails
3027
func runReindex(cmd *cobra.Command, _ []string) error {
31-
filePath := filepath.Join(rc.GetContextDir(), config.FileDecision)
32-
33-
if _, err := os.Stat(filePath); os.IsNotExist(err) {
34-
return fmt.Errorf("%s not found. Run 'ctx init' first", config.FileDecision)
35-
}
36-
37-
content, err := os.ReadFile(filePath)
38-
if err != nil {
39-
return fmt.Errorf("failed to read %s: %w", filePath, err)
40-
}
41-
42-
updated := index.UpdateDecisions(string(content))
43-
44-
if err := os.WriteFile(filePath, []byte(updated), 0644); err != nil {
45-
return fmt.Errorf("failed to write %s: %w", filePath, err)
46-
}
47-
48-
entries := index.ParseHeaders(string(content))
49-
green := color.New(color.FgGreen).SprintFunc()
50-
if len(entries) == 0 {
51-
cmd.Printf("%s Index cleared (no decisions found)\n", green("✓"))
52-
} else {
53-
cmd.Printf(
54-
"%s Index regenerated with %d entries\n", green("✓"),
55-
len(entries),
56-
)
57-
}
58-
59-
return nil
28+
filePath := filepath.Join(rc.ContextDir(), config.FileDecision)
29+
return index.ReindexFile(
30+
cmd.OutOrStdout(),
31+
filePath,
32+
config.FileDecision,
33+
index.UpdateDecisions,
34+
config.EntryPlural[config.EntryDecision],
35+
)
6036
}

internal/cli/learnings/learnings.go

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,8 @@
88
package learnings
99

1010
import (
11-
"fmt"
12-
"os"
1311
"path/filepath"
1412

15-
"github.com/fatih/color"
1613
"github.com/spf13/cobra"
1714

1815
"github.com/ActiveMemory/ctx/internal/config"
@@ -79,35 +76,12 @@ Examples:
7976
// Returns:
8077
// - error: Non-nil if file read/write fails
8178
func runReindex(cmd *cobra.Command, _ []string) error {
82-
filePath := filepath.Join(rc.GetContextDir(), config.FileLearning)
83-
84-
if _, err := os.Stat(filePath); os.IsNotExist(err) {
85-
return fmt.Errorf(
86-
"%s not found. Run 'ctx init' first", config.FileLearning,
87-
)
88-
}
89-
90-
content, err := os.ReadFile(filePath)
91-
if err != nil {
92-
return fmt.Errorf("failed to read %s: %w", filePath, err)
93-
}
94-
95-
updated := index.UpdateLearnings(string(content))
96-
97-
if err := os.WriteFile(filePath, []byte(updated), 0644); err != nil {
98-
return fmt.Errorf("failed to write %s: %w", filePath, err)
99-
}
100-
101-
entries := index.ParseHeaders(string(content))
102-
green := color.New(color.FgGreen).SprintFunc()
103-
if len(entries) == 0 {
104-
cmd.Printf("%s Index cleared (no learnings found)\n", green("✓"))
105-
} else {
106-
cmd.Printf(
107-
"%s Index regenerated with %d entries\n", green("✓"),
108-
len(entries),
109-
)
110-
}
111-
112-
return nil
79+
filePath := filepath.Join(rc.ContextDir(), config.FileLearning)
80+
return index.ReindexFile(
81+
cmd.OutOrStdout(),
82+
filePath,
83+
config.FileLearning,
84+
index.UpdateLearnings,
85+
config.EntryPlural[config.EntryLearning],
86+
)
11387
}

0 commit comments

Comments
 (0)