parent
6325d7fef6
commit
509750b2ab
@ -0,0 +1,2 @@ |
||||
find |
||||
find.exe |
||||
@ -0,0 +1,4 @@ |
||||
|
||||
|
||||
build: |
||||
go build .
|
||||
@ -0,0 +1,3 @@ |
||||
module lcthw.dev/go/go-coreutils/find |
||||
|
||||
go 1.25.3 |
||||
@ -0,0 +1,47 @@ |
||||
package main |
||||
|
||||
import ( |
||||
"fmt" |
||||
"flag" |
||||
"path/filepath" |
||||
"regexp" |
||||
"log" |
||||
"io/fs" |
||||
) |
||||
|
||||
type Opts struct { |
||||
Name string |
||||
Type string |
||||
Print bool |
||||
} |
||||
|
||||
func main() { |
||||
var opts Opts |
||||
|
||||
flag.StringVar(&opts.Name, "name", "", "regex of file") |
||||
flag.StringVar(&opts.Type, "type", "f", "type to find") |
||||
flag.Parse() |
||||
|
||||
re, err := regexp.Compile(opts.Name) |
||||
if err != nil { log.Fatal(err) } |
||||
|
||||
if flag.NArg() == 0 { |
||||
log.Fatal("USAGE: find -name <regex> [-type d/f] <dir> [dir dir]") |
||||
} |
||||
|
||||
for _, start := range flag.Args() { |
||||
filepath.WalkDir(start, func (path string, d fs.DirEntry, err error) error { |
||||
if re.MatchString(path) && opts.Print { |
||||
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 |
||||
}) |
||||
} |
||||
} |
||||
Loading…
Reference in new issue