refactor(processor): inject cfg into file-mutating methods

This commit is contained in:
2026-07-02 09:20:06 -05:00
parent 27681bc56a
commit 9e9b292a6f
3 changed files with 29 additions and 25 deletions
+4 -4
View File
@@ -62,7 +62,7 @@ func (i *Ignore) loadFromFileWithSource(path string, source string) error {
scanner := bufio.NewScanner(file) scanner := bufio.NewScanner(file)
for scanner.Scan() { for scanner.Scan() {
line := strings.TrimSpace(scanner.Text()) line := strings.TrimSpace(scanner.Text())
if line == "" || strings.HasPrefix(line, "#") { if line == "" || strings.HasPrefix(line, "#") {
continue continue
} }
@@ -100,7 +100,7 @@ func (i *Ignore) addPatternWithSource(patternStr string, source string) {
func (i *Ignore) IsIgnored(filePath string) bool { func (i *Ignore) IsIgnored(filePath string) bool {
relPath := filepath.Clean(filePath) relPath := filepath.Clean(filePath)
if strings.HasPrefix(relPath, "./") { if strings.HasPrefix(relPath, "./") {
relPath = relPath[2:] relPath = relPath[2:]
} }
@@ -152,9 +152,9 @@ func (i *Ignore) globMatch(pattern, path string) bool {
} }
func (i *Ignore) wildcardMatch(pattern, path string) bool { func (i *Ignore) wildcardMatch(pattern, path string) bool {
parts := strings.Split(pattern, "*") parts := strings.Split(pattern, "*")
if len(parts) == 1 { if len(parts) == 1 {
return pattern == path return pattern == path
} }
+10 -8
View File
@@ -80,6 +80,14 @@ func (p *Processor) processGitChanges() error {
fmt.Printf("Found %d supported files with changes to process\n", len(supportedChanges)) fmt.Printf("Found %d supported files with changes to process\n", len(supportedChanges))
} }
cfg, _, cfgErr := config.Load()
if cfgErr != nil {
if p.cli.Verbose {
fmt.Printf("Warning: failed to load config, using defaults: %v\n", cfgErr)
}
cfg = config.Default()
}
totals := &GitTotals{} totals := &GitTotals{}
for _, change := range supportedChanges { for _, change := range supportedChanges {
@@ -94,7 +102,7 @@ func (p *Processor) processGitChanges() error {
continue continue
} }
} else { } else {
if err := p.processFileWithLineRanges(change.Path, change.LineRanges); err != nil { if err := p.processFileWithLineRanges(change.Path, change.LineRanges, cfg); err != nil {
fmt.Fprintf(os.Stderr, "Error processing %s: %v\n", change.Path, err) fmt.Fprintf(os.Stderr, "Error processing %s: %v\n", change.Path, err)
continue continue
} }
@@ -109,18 +117,12 @@ func (p *Processor) processGitChanges() error {
return nil return nil
} }
func (p *Processor) processFileWithLineRanges(filename string, lineRanges []git.LineRange) error { func (p *Processor) processFileWithLineRanges(filename string, lineRanges []git.LineRange, cfg *config.Config) error {
language, err := DetectLanguage(filename) language, err := DetectLanguage(filename)
if err != nil { if err != nil {
return err return err
} }
cfg, _, err := config.Load()
if err != nil && p.cli.Verbose {
fmt.Printf("Warning: failed to load config, using defaults: %v\n", err)
cfg = config.Default()
}
if p.cli.Verbose { if p.cli.Verbose {
fmt.Printf("Processing %s (language: %s)\n", filename, GetLanguageName(filename)) fmt.Printf("Processing %s (language: %s)\n", filename, GetLanguageName(filename))
if len(lineRanges) == 0 { if len(lineRanges) == 0 {
+15 -13
View File
@@ -28,6 +28,14 @@ func (p *Processor) Process() error {
return p.processGitChanges() return p.processGitChanges()
} }
cfg, _, err := config.Load()
if err != nil {
if p.cli.Verbose {
fmt.Printf("Warning: failed to load config, using defaults: %v\n", err)
}
cfg = config.Default()
}
info, err := os.Stat(p.cli.Path) info, err := os.Stat(p.cli.Path)
if os.IsNotExist(err) { if os.IsNotExist(err) {
return fmt.Errorf("path not found: %s", p.cli.Path) return fmt.Errorf("path not found: %s", p.cli.Path)
@@ -37,7 +45,7 @@ func (p *Processor) Process() error {
} }
if info.IsDir() { if info.IsDir() {
return p.processDirectory(p.cli.Path) return p.processDirectory(p.cli.Path, cfg)
} }
if IsIgnored(p.cli.Path) { if IsIgnored(p.cli.Path) {
@@ -45,10 +53,10 @@ func (p *Processor) Process() error {
return nil return nil
} }
return p.processFile(p.cli.Path) return p.processFile(p.cli.Path, cfg)
} }
func (p *Processor) processDirectory(dirPath string) error { func (p *Processor) processDirectory(dirPath string, cfg *config.Config) error {
var files []string var files []string
if p.cli.Recursive { if p.cli.Recursive {
@@ -93,7 +101,7 @@ func (p *Processor) processDirectory(dirPath string) error {
fmt.Printf("Processing: %s\n", file) fmt.Printf("Processing: %s\n", file)
} }
if err := p.processFile(file); err != nil { if err := p.processFile(file, cfg); err != nil {
fmt.Fprintf(os.Stderr, "Error processing %s: %v\n", file, err) fmt.Fprintf(os.Stderr, "Error processing %s: %v\n", file, err)
continue continue
} }
@@ -102,7 +110,7 @@ func (p *Processor) processDirectory(dirPath string) error {
return nil return nil
} }
func (p *Processor) processFile(filename string) error { func (p *Processor) processFile(filename string, cfg *config.Config) error {
language, err := DetectLanguage(filename) language, err := DetectLanguage(filename)
if err != nil { if err != nil {
return err return err
@@ -123,16 +131,10 @@ func (p *Processor) processFile(filename string) error {
return p.showPreview(filename, language) return p.showPreview(filename, language)
} }
return p.processFileInMemory(filename, language) return p.processFileInMemory(filename, language, cfg)
} }
func (p *Processor) processFileInMemory(filename string, language types.Language) error { func (p *Processor) processFileInMemory(filename string, language types.Language, cfg *config.Config) error {
cfg, _, err := config.Load()
if err != nil && p.cli.Verbose {
fmt.Printf("Warning: failed to load config, using defaults: %v\n", err)
cfg = config.Default()
}
file, err := os.Open(filename) file, err := os.Open(filename)
if err != nil { if err != nil {