Skip to content

Commit

Permalink
Automatically index when run first time
Browse files Browse the repository at this point in the history
  • Loading branch information
diamondburned committed Jul 6, 2024
1 parent fdf571d commit 91ff97d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
19 changes: 18 additions & 1 deletion cmd/nix-search/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os/signal"
"strings"

"github.com/hashicorp/go-hclog"
"github.com/pkg/errors"
"github.com/urfave/cli/v3"
"golang.org/x/term"
Expand Down Expand Up @@ -99,7 +100,24 @@ func main() {

func mainAction(c *cli.Context) error {
ctx := c.Context
log := hclog.FromContext(ctx)

indexPath := c.String("index-path")
if indexPath == "" {
p, err := blugesearcher.DefaultIndexPath()
if err != nil {
return fmt.Errorf("cannot get default Bluge index path: %w", err)
}
indexPath = p
log.Debug(
"using default index path",
"path", indexPath)
}

if _, err := os.Stat(indexPath); os.IsNotExist(err) {
log.Info("first run detected, indexing packages...")
c.Set("index", "true")
}

if c.Bool("index") {
if c.IsSet("flake") {
Expand All @@ -124,7 +142,6 @@ func mainAction(c *cli.Context) error {
if query == "" {
return nil
}

searcher, err := blugesearcher.Open(indexPath)
if err != nil {
return errors.Wrap(err, "failed to create searcher (try running with --update)")
Expand Down
3 changes: 2 additions & 1 deletion search/searchers/blugesearcher/blugesearcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ func newPackageDocument(path search.Path, pkg search.Package) *bluge.Document {
return doc
}

func defaultPath() (string, error) {
// DefaultIndexPath gets the default index path.
func DefaultIndexPath() (string, error) {
cacheDir, err := os.UserCacheDir()
if err != nil {
return "", fmt.Errorf("cannot get user cache dir: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion search/searchers/blugesearcher/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func IndexPackages(ctx context.Context, path string, packages search.TopLevelPac
if path == "" {
var err error

path, err = defaultPath()
path, err = DefaultIndexPath()
if err != nil {
return fmt.Errorf("cannot get default index path: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion search/searchers/blugesearcher/searcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func Open(path string) (*PackagesSearcher, error) {
if path == "" {
var err error

path, err = defaultPath()
path, err = DefaultIndexPath()
if err != nil {
return nil, fmt.Errorf("cannot get user cache dir: %w", err)
}
Expand Down

0 comments on commit 91ff97d

Please sign in to comment.