Skip to content

Commit

Permalink
default to ad hoc search if not index
Browse files Browse the repository at this point in the history
  • Loading branch information
Itay Donanhirsh committed Feb 9, 2021
1 parent de1d467 commit 61a4005
Show file tree
Hide file tree
Showing 13 changed files with 102 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .clutter/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ scanner:
- .git
- bin
- tests

- README.md
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,4 @@ make install
- [ ] More tests
- [ ] Cross repo.

- [ ] Only account for tags in comments.
- [ ] Only account for tags in comments.
35 changes: 28 additions & 7 deletions cmd/clutter/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,29 @@ var (
debug bool
indexPath string
configPath string
nocolor bool
}{
logLevel: "info",
indexPath: configPath(indexFilename),
configPath: configPath(configFilename),
}

indexFlag = cli.StringFlag{
Name: "index-path",
Aliases: []string{"i"},
Value: opts.indexPath,
Destination: &opts.indexPath,
}

app = cli.App{
UseShortOptionHandling: true,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "nocolor",
Aliases: []string{"nc"},
Destination: &opts.nocolor,
Usage: "do not colorize logs",
},
&cli.BoolFlag{
Name: "verbose",
Aliases: []string{"v"},
Expand All @@ -40,18 +54,13 @@ var (
Value: "warn",
Destination: &opts.logLevel,
},
&cli.StringFlag{
Name: "index-path",
Aliases: []string{"i"},
Value: opts.indexPath,
Destination: &opts.indexPath,
},
&cli.StringFlag{
Name: "config-path",
Aliases: []string{"c"},
Value: opts.configPath,
Destination: &opts.configPath,
},
&indexFlag,
},
Commands: []*cli.Command{
&indexCommand,
Expand All @@ -75,7 +84,7 @@ var (
level = "debug"
}

if err := initLogger(level); err != nil {
if err := initLogger(level, !opts.nocolor); err != nil {
return fmt.Errorf("init logger: %w", err)
}

Expand All @@ -85,3 +94,15 @@ var (
},
}
)

func indexPaths(c *cli.Context) []string {
if c.IsSet(indexFlag.Name) {
return []string{opts.indexPath}
}

if cfg.IgnoreIndex || opts.indexPath == "" {
return []string{""}
}

return []string{opts.indexPath, ""}
}
2 changes: 1 addition & 1 deletion cmd/clutter/cmd_lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var (
return fmt.Errorf("linter: %w", err)
}

src, done, err := ReadIndex(opts.indexPath)
src, done, err := readIndex(c)
if err != nil {
return fmt.Errorf("read index: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/clutter/cmd_resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ var (
return fmt.Errorf("loc: %w", err)
}

src, done, err := ReadIndex(opts.indexPath)
src, done, err := readIndex(c)
if err != nil {
return fmt.Errorf("read index: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/clutter/cmd_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ var (
return fmt.Errorf("matcher: %w", err)
}

src, done, err := ReadIndex(opts.indexPath)
src, done, err := readIndex(c)
if err != nil {
return fmt.Errorf("read index: %w", err)
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/clutter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ const (
func configPath(p string) string { return filepath.Join(defaultClutterDir, p) }

type config struct {
Scanner scanner.Config `json:"scanner"`
Linter linter.Config `json:"linter"`
IgnoreIndex bool `json:"ignore-index"`
Scanner scanner.Config `json:"scanner"`
Linter linter.Config `json:"linter"`
}

var (
Expand Down
44 changes: 38 additions & 6 deletions cmd/clutter/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,47 @@ package main

import (
"fmt"
"os"

cli "github.com/urfave/cli/v2"

"github.com/cluttercode/clutter/internal/pkg/parser"
"github.com/cluttercode/clutter/internal/pkg/scanner"

"github.com/cluttercode/clutter/pkg/clutter/clutterindex"
)

func ReadIndex(filename string) (src func() (*clutterindex.Entry, error), done func(), err error) {
func readIndex(c *cli.Context) (func() (*clutterindex.Entry, error), func(), error) {
paths := indexPaths(c)

z.Debugw("reading index", "paths", paths)

for _, path := range paths {
z := z.With("path", path)

src, done, err := readSpecificIndex(path)
if err != nil {
if os.IsNotExist(err) {
z.Warn("file does not exist")
continue
}

if path == "" {
path = "stdin"
}

return nil, nil, fmt.Errorf("%s: %w", path, err)
}

z.Info("index read")

return src, done, nil
}

return nil, nil, fmt.Errorf("no index file exist")
}

func readSpecificIndex(filename string) (src func() (*clutterindex.Entry, error), done func(), err error) {
if filename == "" {
scan, err := scanner.NewScanner(z.Named("scanner"), cfg.Scanner)
if err != nil {
Expand All @@ -28,11 +61,10 @@ func ReadIndex(filename string) (src func() (*clutterindex.Entry, error), done f

src = clutterindex.SliceSource(clutterindex.NewIndex(ents))
done = func() {}
} else {
src, done, err = clutterindex.FileSource(filename)
if err != nil {
return nil, nil, fmt.Errorf("index open: %w", err)
}

return src, done, nil
} else if src, done, err = clutterindex.FileSource(filename); err != nil {
return nil, nil, err // do not wrap error.
}

return
Expand Down
7 changes: 6 additions & 1 deletion cmd/clutter/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@ import (

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

func initLogger(level string) error {
func initLogger(level string, color bool) error {
zcfg := zap.NewDevelopmentConfig()

zcfg.DisableStacktrace = true

zcfg.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
if !color {
zcfg.EncoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
}

zcfg.EncoderConfig.EncodeTime = func(time.Time, zapcore.PrimitiveArrayEncoder) {}

if level != "debug" {
Expand Down
2 changes: 1 addition & 1 deletion pkg/clutter/clutterindex/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func FileSource(path string) (next func() (*Entry, error), done func(), err erro
if path == "stdin" || path == "-" {
f = os.Stdin
} else if f, err = os.Open(path); err != nil {
return nil, nil, fmt.Errorf("open: %w", err)
return nil, nil, err // don't wrap here - checking for IsNotExist in caller.
}

done = func() { f.Close() }
Expand Down
1 change: 1 addition & 0 deletions tests/cli/config.1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ignore-index: true
19 changes: 19 additions & 0 deletions tests/cli/misc.clitest
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
$ ${CLUTTER} --nc -i nosuchfile s; echo $?
WARN file does not exist {"path": "nosuchfile"}

error: read index: no index file exist
1
$ ${CLUTTER} -c config.1.yaml --nc -i nosuchfile s; echo $?
WARN file does not exist {"path": "nosuchfile"}

error: read index: no index file exist
1
$ ${CLUTTER} -i "" s; echo $?
0
$ ${CLUTTER} -c config.1.yaml -i "" s; echo $?
0
$ ${CLUTTER} --nc s; echo $?
WARN file does not exist {"path": ".clutter/index"}
0
$ ${CLUTTER} -c config.1.yaml --nc s; echo $?
0
2 changes: 1 addition & 1 deletion tests/cli/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ fi

for t in ${TESTS}; do
printf "${BWHITE}${t}${NC}:\n"
./clitest "$t"
./clitest --diff-options "-u -w" "$t"
done

0 comments on commit 61a4005

Please sign in to comment.