From 61a4005af03fabadd336fa648aa1e3caac09fc73 Mon Sep 17 00:00:00 2001 From: Itay Donanhirsh Date: Mon, 8 Feb 2021 16:36:54 -0800 Subject: [PATCH] default to ad hoc search if not index --- .clutter/config.yaml | 2 +- README.md | 2 +- cmd/clutter/app.go | 35 +++++++++++++++++++----- cmd/clutter/cmd_lint.go | 2 +- cmd/clutter/cmd_resolve.go | 2 +- cmd/clutter/cmd_search.go | 2 +- cmd/clutter/config.go | 5 ++-- cmd/clutter/index.go | 44 ++++++++++++++++++++++++++---- cmd/clutter/log.go | 7 ++++- pkg/clutter/clutterindex/filter.go | 2 +- tests/cli/config.1.yaml | 1 + tests/cli/misc.clitest | 19 +++++++++++++ tests/cli/run.sh | 2 +- 13 files changed, 102 insertions(+), 23 deletions(-) create mode 100644 tests/cli/config.1.yaml create mode 100644 tests/cli/misc.clitest diff --git a/.clutter/config.yaml b/.clutter/config.yaml index a331041..9132668 100644 --- a/.clutter/config.yaml +++ b/.clutter/config.yaml @@ -3,4 +3,4 @@ scanner: - .git - bin - tests - + - README.md diff --git a/README.md b/README.md index 6c2d7a8..0a02bcb 100644 --- a/README.md +++ b/README.md @@ -113,4 +113,4 @@ make install - [ ] More tests - [ ] Cross repo. -- [ ] Only account for tags in comments. \ No newline at end of file +- [ ] Only account for tags in comments. diff --git a/cmd/clutter/app.go b/cmd/clutter/app.go index 9678c3f..241e1b4 100644 --- a/cmd/clutter/app.go +++ b/cmd/clutter/app.go @@ -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"}, @@ -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, @@ -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) } @@ -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, ""} +} diff --git a/cmd/clutter/cmd_lint.go b/cmd/clutter/cmd_lint.go index 3566986..a216e45 100644 --- a/cmd/clutter/cmd_lint.go +++ b/cmd/clutter/cmd_lint.go @@ -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) } diff --git a/cmd/clutter/cmd_resolve.go b/cmd/clutter/cmd_resolve.go index b9b407d..bb71357 100644 --- a/cmd/clutter/cmd_resolve.go +++ b/cmd/clutter/cmd_resolve.go @@ -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) } diff --git a/cmd/clutter/cmd_search.go b/cmd/clutter/cmd_search.go index 921e62e..6094da4 100644 --- a/cmd/clutter/cmd_search.go +++ b/cmd/clutter/cmd_search.go @@ -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) } diff --git a/cmd/clutter/config.go b/cmd/clutter/config.go index 1786d6d..506291c 100644 --- a/cmd/clutter/config.go +++ b/cmd/clutter/config.go @@ -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 ( diff --git a/cmd/clutter/index.go b/cmd/clutter/index.go index 3cbc5f8..fffba37 100644 --- a/cmd/clutter/index.go +++ b/cmd/clutter/index.go @@ -2,6 +2,9 @@ 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" @@ -9,7 +12,37 @@ import ( "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 { @@ -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 diff --git a/cmd/clutter/log.go b/cmd/clutter/log.go index 7cae415..84d3b72 100644 --- a/cmd/clutter/log.go +++ b/cmd/clutter/log.go @@ -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" { diff --git a/pkg/clutter/clutterindex/filter.go b/pkg/clutter/clutterindex/filter.go index b0c0eda..d57f8e3 100644 --- a/pkg/clutter/clutterindex/filter.go +++ b/pkg/clutter/clutterindex/filter.go @@ -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() } diff --git a/tests/cli/config.1.yaml b/tests/cli/config.1.yaml new file mode 100644 index 0000000..b12badf --- /dev/null +++ b/tests/cli/config.1.yaml @@ -0,0 +1 @@ +ignore-index: true diff --git a/tests/cli/misc.clitest b/tests/cli/misc.clitest new file mode 100644 index 0000000..bf9ecaa --- /dev/null +++ b/tests/cli/misc.clitest @@ -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 diff --git a/tests/cli/run.sh b/tests/cli/run.sh index 111f41b..1a150d3 100755 --- a/tests/cli/run.sh +++ b/tests/cli/run.sh @@ -18,5 +18,5 @@ fi for t in ${TESTS}; do printf "${BWHITE}${t}${NC}:\n" - ./clitest "$t" + ./clitest --diff-options "-u -w" "$t" done