Skip to content

Commit

Permalink
Add --follow-symlinks flag
Browse files Browse the repository at this point in the history
  • Loading branch information
hrs committed May 23, 2023
1 parent 6db2d75 commit aedb7c2
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 15 deletions.
15 changes: 8 additions & 7 deletions lib/config.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package main

type Config struct {
BestFirst bool
Limit int
NoStemming bool
NoStoplist bool
OmitQuery bool
ShowScores bool
Verbose bool
BestFirst bool
FollowSymlinks bool
Limit int
NoStemming bool
NoStoplist bool
OmitQuery bool
ShowScores bool
Verbose bool
}
9 changes: 8 additions & 1 deletion lib/corpus.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func ParseCorpus(query *Document, paths []string, config *Config) *Corpus {
}

// Don't parse directories or symlinks (or the queried file, if so configured)
if xinfo.Type().IsRegular() && !(config.OmitQuery && sameFile(query.Path, xpath)) {
if isParsableFile(xinfo, config) && !(config.OmitQuery && sameFile(query.Path, xpath)) {
doc, err := NewDocument(xpath, config)

if err != nil {
Expand All @@ -71,6 +71,13 @@ func ParseCorpus(query *Document, paths []string, config *Config) *Corpus {
return NewCorpus(documents)
}

func isParsableFile(info fs.DirEntry, config *Config) bool {
// Return true if this is either a regular file OR if it's a symlink that the
// user's chosen to include
return info.Type().IsRegular() ||
(config.FollowSymlinks && (info.Type()&os.ModeSymlink != 0))
}

func sameFile(a, b string) bool {
aFileInfo, err := os.Stat(a)
if err != nil {
Expand Down
16 changes: 9 additions & 7 deletions lib/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

func main() {
bestFirstFlag := flag.Bool("best-first", false, "print best matches first")
followSymlinksFlag := flag.Bool("follow-symlinks", false, "included symlinked files in results")
limitFlag := flag.Int("limit", 0, "return at most `limit` results")
noStemmingFlag := flag.Bool("no-stemming", false, "don't perform stemming on words")
noStoplistFlag := flag.Bool("no-stoplist", false, "don't omit common words by using a stoplist")
Expand All @@ -20,13 +21,14 @@ func main() {
flag.Parse()

config := Config{
BestFirst: *bestFirstFlag,
Limit: *limitFlag,
NoStemming: *noStemmingFlag,
NoStoplist: *noStoplistFlag,
OmitQuery: *omitQueryFlag,
ShowScores: *showScoresFlag,
Verbose: *verboseFlag,
BestFirst: *bestFirstFlag,
FollowSymlinks: *followSymlinksFlag,
Limit: *limitFlag,
NoStemming: *noStemmingFlag,
NoStoplist: *noStoplistFlag,
OmitQuery: *omitQueryFlag,
ShowScores: *showScoresFlag,
Verbose: *verboseFlag,
}

if !config.Verbose {
Expand Down
3 changes: 3 additions & 0 deletions man/docsim.1
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ docsim --query main.c --no-stoplist --no-stemming **/*.c
When enabled, docsim will sort results from best to worst, instead of the
default (worst to best).
.TP
.BR \-\-follow\-symlinks
Parse symbolic links in the search path. Default behavior is to ignore symlinks.
.TP
.BR \-\-limit " " \fINUM\fR
Show no more that the best NUM results.
.TP
Expand Down

0 comments on commit aedb7c2

Please sign in to comment.