@@ -2,14 +2,47 @@ package main
22
33import (
44 "fmt"
5+ "os"
6+
7+ cli "github.com/urfave/cli/v2"
58
69 "github.com/cluttercode/clutter/internal/pkg/parser"
710 "github.com/cluttercode/clutter/internal/pkg/scanner"
811
912 "github.com/cluttercode/clutter/pkg/clutter/clutterindex"
1013)
1114
12- func ReadIndex (filename string ) (src func () (* clutterindex.Entry , error ), done func (), err error ) {
15+ func readIndex (c * cli.Context ) (func () (* clutterindex.Entry , error ), func (), error ) {
16+ paths := indexPaths (c )
17+
18+ z .Debugw ("reading index" , "paths" , paths )
19+
20+ for _ , path := range paths {
21+ z := z .With ("path" , path )
22+
23+ src , done , err := readSpecificIndex (path )
24+ if err != nil {
25+ if os .IsNotExist (err ) {
26+ z .Warn ("file does not exist" )
27+ continue
28+ }
29+
30+ if path == "" {
31+ path = "stdin"
32+ }
33+
34+ return nil , nil , fmt .Errorf ("%s: %w" , path , err )
35+ }
36+
37+ z .Info ("index read" )
38+
39+ return src , done , nil
40+ }
41+
42+ return nil , nil , fmt .Errorf ("no index file exist" )
43+ }
44+
45+ func readSpecificIndex (filename string ) (src func () (* clutterindex.Entry , error ), done func (), err error ) {
1346 if filename == "" {
1447 scan , err := scanner .NewScanner (z .Named ("scanner" ), cfg .Scanner )
1548 if err != nil {
@@ -28,11 +61,10 @@ func ReadIndex(filename string) (src func() (*clutterindex.Entry, error), done f
2861
2962 src = clutterindex .SliceSource (clutterindex .NewIndex (ents ))
3063 done = func () {}
31- } else {
32- src , done , err = clutterindex .FileSource (filename )
33- if err != nil {
34- return nil , nil , fmt .Errorf ("index open: %w" , err )
35- }
64+
65+ return src , done , nil
66+ } else if src , done , err = clutterindex .FileSource (filename ); err != nil {
67+ return nil , nil , err // do not wrap error.
3668 }
3769
3870 return
0 commit comments