diff --git a/lib/config.go b/lib/config.go index bc47347..4ffc972 100644 --- a/lib/config.go +++ b/lib/config.go @@ -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 } diff --git a/lib/corpus.go b/lib/corpus.go index 1b95abd..bdaad26 100644 --- a/lib/corpus.go +++ b/lib/corpus.go @@ -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 { @@ -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 { diff --git a/lib/main.go b/lib/main.go index 4a60fba..8d62b0b 100644 --- a/lib/main.go +++ b/lib/main.go @@ -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") @@ -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 { diff --git a/man/docsim.1 b/man/docsim.1 index fbbbb65..0ee1939 100644 --- a/man/docsim.1 +++ b/man/docsim.1 @@ -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