Skip to content

Instantly share code, notes, and snippets.

@selfup
Created August 10, 2019 22:50
Show Gist options
  • Save selfup/a4ba9befad13ae7d9602b77acf65aefb to your computer and use it in GitHub Desktop.
Save selfup/a4ba9befad13ae7d9602b77acf65aefb to your computer and use it in GitHub Desktop.
package main
import (
"os"
"path/filepath"
"strconv"
)
func main() {
var dir string
var size int64
if len(os.Args) == 3 {
dir = os.Args[1]
sizeInt, err := strconv.Atoi(os.Args[2])
if err != nil {
panic(err)
}
size = int64(sizeInt)
}
scnnr := FindBySize{
Directory: dir,
Size: size,
}
err := scnnr.Scan()
if err != nil {
panic(err)
}
}
// FindBySize holds cli args
type FindBySize struct {
Directory string
Size int64
Found []string
}
// Scan walks the given directory tree
func (f *FindBySize) Scan() error {
err := filepath.Walk(f.Directory, f.scan)
if err != nil {
return err
}
return nil
}
func (f *FindBySize) scan(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
if info.Size() > f.Size {
f.Found = append(f.Found, path)
}
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment