Skip to content

Commit

Permalink
allow listing of dir above size limit, debug switch added
Browse files Browse the repository at this point in the history
Signed-off-by: AbhishekKr <abhikumar163@gmail.com>
  • Loading branch information
abhishekkr committed Aug 25, 2020
1 parent 25240de commit 4718d9b
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 27 deletions.
20 changes: 16 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,30 @@ bff -path ~/Downloads -minsize 750
bff -path ~/Downloads -minsize 1024
```

* list directories also which have total size above limit, even if individual file escape the check

```
bff -dir -path ~/Downloads -minsize 1500
```

* if listing is erroneous, get to see more info using `-debug` switch as

```
bff -dir -path ~/Downloads -minsize 1500 -debug
```

---

### Install/Download

* [latest version](https://github.com/abhishekkr/bff/releases/latest)

* [v0.0.1 Release Page](https://github.com/abhishekkr/bff/releases/tag/v0.0.1)
* [v0.0.2 Release Page](https://github.com/abhishekkr/bff/releases/tag/v0.0.2), [v0.0.1 Release Page](https://github.com/abhishekkr/bff/releases/tag/v0.0.1)

> * [linux](https://github.com/abhishekkr/bff/releases/download/v0.0.1/bff-linux-amd64)
> * [linux](https://github.com/abhishekkr/bff/releases/download/v0.0.2/bff-linux-amd64)
>
> * [macos](https://github.com/abhishekkr/bff/releases/download/v0.0.1/bff-darwin-amd64)
> * [macos](https://github.com/abhishekkr/bff/releases/download/v0.0.2/bff-darwin-amd64)
>
> * [windows](https://github.com/abhishekkr/bff/releases/download/v0.0.1/bff-windows-amd64)
> * [windows](https://github.com/abhishekkr/bff/releases/download/v0.0.2/bff-windows-amd64)
---
62 changes: 39 additions & 23 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
Expand All @@ -18,7 +17,9 @@ To find all files under a path recursively which are above a minimum size limit.
var (
pathsAboveRedSizeMBLimit = flag.Int("minsize", 500, "minimum size to display in MBs")
targetDir = flag.String("path", "/tmp", "path to scan")
listDir = flag.Bool("dir", false, "list big size directories also")
version = flag.Bool("version", false, "show me version")
debug = flag.Bool("debug", false, "show extra information")
)

func readDir(dirname string) ([]os.FileInfo, error) {
Expand Down Expand Up @@ -52,39 +53,54 @@ func sizeInHuman(size int64) (float64, string, string) {
return gbSize, fmt.Sprintf("about %.2f GBs", gbSize), "gb"
}

func calculateDirSize(dirpath string) (dirsize int64, err error) {
err = os.Chdir(dirpath)
if err != nil {
return
}
files, err := ioutil.ReadDir(".")
if err != nil {
return
func changeSizeToMB(size float64, sizeType string) float64 {
if sizeType == "b" {
return (size / (1024 * 1024))
} else if sizeType == "kb" {
return (size / (1024))
} else if sizeType == "mb" {
return size
} else if sizeType == "gb" {
return size * 1024
}
return size
}

for _, file := range files {
if file.Mode().IsRegular() {
dirsize += file.Size()
func calculateDirSize(dirPath string) (dirSize int64, fullDirSize float64, err error) {
err = filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error {
if info.Mode().IsRegular() {
dirSize += info.Size()
}
}
return nil
})

size, _, sizeType := sizeInHuman(dirSize)
fullDirSize = changeSizeToMB(size, sizeType)
return
}

func analyzeSubDir(dirPath string, dirName string, sizeMBLimit float64) {
_, dirSize, err := calculateDirSize(dirPath)
if err != nil && *debug == true {
log.Printf("not able to get dir size for: %s", dirPath)
return
} else if float64(dirSize) >= sizeMBLimit {
if *listDir {
fmt.Printf("\n📁Path: %s\n\tName: %s\n\tSize: %.2f MB\n", dirPath, dirName, dirSize)
}
analyzeDir(dirPath)
}
}

func analyzeDir(pathToScan string) {
sizeMBLimit := float64(*pathsAboveRedSizeMBLimit)
if entries, err := readDir(pathToScan); err == nil {
for _, entry := range entries {
filepath := filepath.Join(pathToScan, entry.Name())
size, humanSize, sizeType := sizeInHuman(entry.Size())
isDir := entry.IsDir()
if isDir {
dirsize, err := calculateDirSize(filepath)
if err != nil {
log.Fatalf("not able to get dir size for: %s", filepath)
continue
} else if float64(dirsize) >= sizeMBLimit {
analyzeDir(filepath)
}

if entry.IsDir() {
analyzeSubDir(filepath, entry.Name(), sizeMBLimit)
continue
}
if sizeType == "b" || sizeType == "kb" {
Expand All @@ -97,7 +113,7 @@ func analyzeDir(pathToScan string) {
continue
}
}
fmt.Printf("\n📁Path: %s\n\tName: %s\n\tSize: %s\n", filepath, entry.Name(), humanSize)
fmt.Printf("\n📄Path: %s\n\tName: %s\n\tSize: %s\n", filepath, entry.Name(), humanSize)
}
} else {
log.Fatalf("error reading dir %s", pathToScan)
Expand Down

0 comments on commit 4718d9b

Please sign in to comment.