-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathff.go
71 lines (60 loc) · 1.46 KB
/
ff.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/spf13/cobra"
)
func main() {
var (
directory string
ignoreDirs []string
)
var rootCmd = &cobra.Command{
Use: "ff [pattern]",
Short: "ff finds files matching a pattern",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
pattern := strings.ToLower(args[0])
ignoreMap := make(map[string]struct{})
for _, dir := range ignoreDirs {
absDir, err := filepath.Abs(dir)
if err != nil {
fmt.Printf("Error resolving absolute path for %s: %v\n", dir, err)
os.Exit(1)
}
ignoreMap[absDir] = struct{}{}
}
err := filepath.Walk(directory, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
absPath, err := filepath.Abs(path)
if err != nil {
return err
}
if _, ignored := ignoreMap[filepath.Dir(absPath)]; ignored {
if info.IsDir() {
return filepath.SkipDir
}
return nil
}
if !info.IsDir() && strings.Contains(strings.ToLower(info.Name()), pattern) {
fmt.Println(path)
}
return nil
})
if err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
},
}
rootCmd.Flags().StringVarP(&directory, "directory", "d", ".", "Directory to search in")
rootCmd.Flags().StringSliceVarP(&ignoreDirs, "ignoredirectory", "i", nil, "Directories to ignore")
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}