Created
January 6, 2015 20:06
-
-
Save ShawnMilo/6b452347d1e7e222aae3 to your computer and use it in GitHub Desktop.
file finder: Basically a handy replacement for: find . -name '*foo*'
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"flag" | |
"fmt" | |
"log" | |
"os" | |
"path/filepath" | |
"strings" | |
) | |
var path string // path to search | |
var args []string | |
// isDir accepts a string (file path) and returns | |
// a boolean which indicates if the path is | |
// a valid directory. | |
func isDir(path string) bool { | |
stat, err := os.Stat(path) | |
if err != nil { | |
log.Fatal(err) | |
} | |
return stat.IsDir() | |
} | |
func init() { | |
// Set up command-line flags. | |
flag.StringVar(&path, "p", ".", "path") | |
flag.Parse() | |
// Validate flags. | |
if !isDir(path) { | |
log.Fatal(path, "is not a valid path.") | |
} | |
args = flag.Args() | |
if len(args) == 0 { | |
log.Fatal("no arguments passed") | |
} | |
} | |
// walker implements filepath.WalkFunc. | |
func walker(path string, info os.FileInfo, err error) error { | |
if err != nil { | |
log.Println(err) | |
} | |
for _, arg := range args { | |
if !strings.Contains(strings.ToLower(info.Name()), strings.ToLower(arg)) { | |
return nil | |
} | |
} | |
fmt.Println(path) | |
return nil | |
} | |
func main() { | |
filepath.Walk(path, walker) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment