Add -L symlink follow option, default false
This commit is contained in:
parent
5694cc5194
commit
eacffb4fe1
49
main.go
49
main.go
|
@ -47,22 +47,31 @@ to a regular expression to make that specific expression case insensitive.
|
||||||
|
|
||||||
Files and directories can be excluded with the -x option. This supports bash-style
|
Files and directories can be excluded with the -x option. This supports bash-style
|
||||||
globs with '*', '?', '[a-z]', '{this,that}', or '/**/' to match zero or more
|
globs with '*', '?', '[a-z]', '{this,that}', or '/**/' to match zero or more
|
||||||
directories. By default, .git and vim swap files are ignored.`,
|
directories. By default, .git and vim swap files are ignored. Symlinks named on
|
||||||
|
the command line are followed, but by default symlinks are not followed when
|
||||||
|
recursing into directories. -L allows them to be dereferenced.`,
|
||||||
|
|
||||||
RunE: run,
|
RunE: run,
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
searchRegexp []string
|
// flags
|
||||||
regexps []*regexp.Regexp
|
searchRegexp []string
|
||||||
searchFixed []string
|
searchFixed []string
|
||||||
searchPath []string
|
searchPath []string
|
||||||
excludeList []string
|
excludeList []string
|
||||||
binaryFile notPlainTextFlag
|
ignoreCase bool
|
||||||
minifiedFile notPlainTextFlag
|
noColour bool
|
||||||
ignoreCase bool
|
binaryFile notPlainTextFlag
|
||||||
noColour bool
|
minifiedFile notPlainTextFlag
|
||||||
display *Display
|
followSymlinks bool
|
||||||
|
|
||||||
|
// computed from searchRegexp, searchFixed. Each regexp here will be
|
||||||
|
// matched against each line of each input file.
|
||||||
|
regexps []*regexp.Regexp
|
||||||
|
|
||||||
|
// formats output
|
||||||
|
display *Display
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -73,6 +82,7 @@ func init() {
|
||||||
rootCmd.Flags().BoolVarP(&noColour, "no-colour", "C", false, "disable colour output")
|
rootCmd.Flags().BoolVarP(&noColour, "no-colour", "C", false, "disable colour output")
|
||||||
rootCmd.Flags().Var(&binaryFile, "binary", "what to do with binary files")
|
rootCmd.Flags().Var(&binaryFile, "binary", "what to do with binary files")
|
||||||
rootCmd.Flags().Var(&minifiedFile, "minified", "what to do with minified text files")
|
rootCmd.Flags().Var(&minifiedFile, "minified", "what to do with minified text files")
|
||||||
|
rootCmd.Flags().BoolVarP(&followSymlinks, "dereference", "L", false, "follow symlinks when recursing")
|
||||||
}
|
}
|
||||||
|
|
||||||
func run(c *cobra.Command, args []string) error {
|
func run(c *cobra.Command, args []string) error {
|
||||||
|
@ -129,7 +139,7 @@ func run(c *cobra.Command, args []string) error {
|
||||||
|
|
||||||
var errs []error
|
var errs []error
|
||||||
for _, path := range searchPath {
|
for _, path := range searchPath {
|
||||||
if err := search(path); err != nil {
|
if err := search(path, true); err != nil {
|
||||||
errs = append(errs, err)
|
errs = append(errs, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -155,15 +165,24 @@ NextFile:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := search(fullPath); err != nil {
|
if err := search(fullPath, followSymlinks); err != nil {
|
||||||
errs = append(errs, err)
|
errs = append(errs, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return errors.Join(errs...)
|
return errors.Join(errs...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func search(path string) error {
|
func search(path string, deref bool) error {
|
||||||
st, err := os.Stat(path)
|
var (
|
||||||
|
st os.FileInfo
|
||||||
|
err error
|
||||||
|
)
|
||||||
|
|
||||||
|
if deref {
|
||||||
|
st, err = os.Stat(path)
|
||||||
|
} else {
|
||||||
|
st, err = os.Lstat(path)
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue