This is an education project that attempts to reimplement the GNU coreutils in Go. You can find the full manual here:
https://www.gnu.org/software/coreutils/manual/coreutils.html
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
54 lines
996 B
54 lines
996 B
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"flag"
|
|
"path/filepath"
|
|
"regexp"
|
|
"log"
|
|
"io/fs"
|
|
)
|
|
|
|
type Opts struct {
|
|
Name string
|
|
Type string
|
|
}
|
|
|
|
func ParseOpts() Opts {
|
|
var opts Opts
|
|
|
|
flag.StringVar(&opts.Name, "name", "", "regex of file")
|
|
flag.StringVar(&opts.Type, "type", "f", "type to find")
|
|
flag.Parse()
|
|
|
|
if flag.NArg() == 0 {
|
|
log.Fatal("USAGE: find -name <regex> [-type d/f] <dir> [dir dir]")
|
|
}
|
|
|
|
return opts
|
|
}
|
|
|
|
func main() {
|
|
opts := ParseOpts()
|
|
|
|
re, err := regexp.Compile(opts.Name)
|
|
if err != nil { log.Fatal(err) }
|
|
|
|
for _, start := range flag.Args() {
|
|
filepath.WalkDir(start, func (path string, d fs.DirEntry, err error) error {
|
|
if err != nil { log.Fatal(err) }
|
|
|
|
if re.MatchString(path) {
|
|
if opts.Type == "" {
|
|
fmt.Println(path)
|
|
} else if d.IsDir() && opts.Type == "d" {
|
|
fmt.Println(path)
|
|
} else if !d.IsDir() && opts.Type == "f" {
|
|
fmt.Println(path)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
})
|
|
}
|
|
}
|
|
|