Skip to content

Commit 61a4005

Browse files
author
Itay Donanhirsh
committed
default to ad hoc search if not index
1 parent de1d467 commit 61a4005

File tree

13 files changed

+102
-23
lines changed

13 files changed

+102
-23
lines changed

.clutter/config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ scanner:
33
- .git
44
- bin
55
- tests
6-
6+
- README.md

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,4 @@ make install
113113
- [ ] More tests
114114
- [ ] Cross repo.
115115

116-
- [ ] Only account for tags in comments.
116+
- [ ] Only account for tags in comments.

cmd/clutter/app.go

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,29 @@ var (
1313
debug bool
1414
indexPath string
1515
configPath string
16+
nocolor bool
1617
}{
1718
logLevel: "info",
1819
indexPath: configPath(indexFilename),
1920
configPath: configPath(configFilename),
2021
}
2122

23+
indexFlag = cli.StringFlag{
24+
Name: "index-path",
25+
Aliases: []string{"i"},
26+
Value: opts.indexPath,
27+
Destination: &opts.indexPath,
28+
}
29+
2230
app = cli.App{
2331
UseShortOptionHandling: true,
2432
Flags: []cli.Flag{
33+
&cli.BoolFlag{
34+
Name: "nocolor",
35+
Aliases: []string{"nc"},
36+
Destination: &opts.nocolor,
37+
Usage: "do not colorize logs",
38+
},
2539
&cli.BoolFlag{
2640
Name: "verbose",
2741
Aliases: []string{"v"},
@@ -40,18 +54,13 @@ var (
4054
Value: "warn",
4155
Destination: &opts.logLevel,
4256
},
43-
&cli.StringFlag{
44-
Name: "index-path",
45-
Aliases: []string{"i"},
46-
Value: opts.indexPath,
47-
Destination: &opts.indexPath,
48-
},
4957
&cli.StringFlag{
5058
Name: "config-path",
5159
Aliases: []string{"c"},
5260
Value: opts.configPath,
5361
Destination: &opts.configPath,
5462
},
63+
&indexFlag,
5564
},
5665
Commands: []*cli.Command{
5766
&indexCommand,
@@ -75,7 +84,7 @@ var (
7584
level = "debug"
7685
}
7786

78-
if err := initLogger(level); err != nil {
87+
if err := initLogger(level, !opts.nocolor); err != nil {
7988
return fmt.Errorf("init logger: %w", err)
8089
}
8190

@@ -85,3 +94,15 @@ var (
8594
},
8695
}
8796
)
97+
98+
func indexPaths(c *cli.Context) []string {
99+
if c.IsSet(indexFlag.Name) {
100+
return []string{opts.indexPath}
101+
}
102+
103+
if cfg.IgnoreIndex || opts.indexPath == "" {
104+
return []string{""}
105+
}
106+
107+
return []string{opts.indexPath, ""}
108+
}

cmd/clutter/cmd_lint.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ var (
2222
return fmt.Errorf("linter: %w", err)
2323
}
2424

25-
src, done, err := ReadIndex(opts.indexPath)
25+
src, done, err := readIndex(c)
2626
if err != nil {
2727
return fmt.Errorf("read index: %w", err)
2828
}

cmd/clutter/cmd_resolve.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ var (
5959
return fmt.Errorf("loc: %w", err)
6060
}
6161

62-
src, done, err := ReadIndex(opts.indexPath)
62+
src, done, err := readIndex(c)
6363
if err != nil {
6464
return fmt.Errorf("read index: %w", err)
6565
}

cmd/clutter/cmd_search.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ var (
7979
return fmt.Errorf("matcher: %w", err)
8080
}
8181

82-
src, done, err := ReadIndex(opts.indexPath)
82+
src, done, err := readIndex(c)
8383
if err != nil {
8484
return fmt.Errorf("read index: %w", err)
8585
}

cmd/clutter/config.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ const (
2121
func configPath(p string) string { return filepath.Join(defaultClutterDir, p) }
2222

2323
type config struct {
24-
Scanner scanner.Config `json:"scanner"`
25-
Linter linter.Config `json:"linter"`
24+
IgnoreIndex bool `json:"ignore-index"`
25+
Scanner scanner.Config `json:"scanner"`
26+
Linter linter.Config `json:"linter"`
2627
}
2728

2829
var (

cmd/clutter/index.go

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,47 @@ package main
22

33
import (
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

cmd/clutter/log.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,16 @@ import (
1010

1111
var z *zap.SugaredLogger = zap.NewNop().Sugar()
1212

13-
func initLogger(level string) error {
13+
func initLogger(level string, color bool) error {
1414
zcfg := zap.NewDevelopmentConfig()
1515

1616
zcfg.DisableStacktrace = true
17+
1718
zcfg.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
19+
if !color {
20+
zcfg.EncoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
21+
}
22+
1823
zcfg.EncoderConfig.EncodeTime = func(time.Time, zapcore.PrimitiveArrayEncoder) {}
1924

2025
if level != "debug" {

pkg/clutter/clutterindex/filter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func FileSource(path string) (next func() (*Entry, error), done func(), err erro
2424
if path == "stdin" || path == "-" {
2525
f = os.Stdin
2626
} else if f, err = os.Open(path); err != nil {
27-
return nil, nil, fmt.Errorf("open: %w", err)
27+
return nil, nil, err // don't wrap here - checking for IsNotExist in caller.
2828
}
2929

3030
done = func() { f.Close() }

0 commit comments

Comments
 (0)