Skip to content

Commit 4718d9b

Browse files
committed
allow listing of dir above size limit, debug switch added
Signed-off-by: AbhishekKr <[email protected]>
1 parent 25240de commit 4718d9b

File tree

2 files changed

+55
-27
lines changed

2 files changed

+55
-27
lines changed

README.md

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,30 @@ bff -path ~/Downloads -minsize 750
2525
bff -path ~/Downloads -minsize 1024
2626
```
2727

28+
* list directories also which have total size above limit, even if individual file escape the check
29+
30+
```
31+
bff -dir -path ~/Downloads -minsize 1500
32+
```
33+
34+
* if listing is erroneous, get to see more info using `-debug` switch as
35+
36+
```
37+
bff -dir -path ~/Downloads -minsize 1500 -debug
38+
```
39+
2840
---
2941

3042
### Install/Download
3143

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

34-
* [v0.0.1 Release Page](https://github.com/abhishekkr/bff/releases/tag/v0.0.1)
46+
* [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)
3547

36-
> * [linux](https://github.com/abhishekkr/bff/releases/download/v0.0.1/bff-linux-amd64)
48+
> * [linux](https://github.com/abhishekkr/bff/releases/download/v0.0.2/bff-linux-amd64)
3749
>
38-
> * [macos](https://github.com/abhishekkr/bff/releases/download/v0.0.1/bff-darwin-amd64)
50+
> * [macos](https://github.com/abhishekkr/bff/releases/download/v0.0.2/bff-darwin-amd64)
3951
>
40-
> * [windows](https://github.com/abhishekkr/bff/releases/download/v0.0.1/bff-windows-amd64)
52+
> * [windows](https://github.com/abhishekkr/bff/releases/download/v0.0.2/bff-windows-amd64)
4153
4254
---

main.go

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package main
33
import (
44
"flag"
55
"fmt"
6-
"io/ioutil"
76
"log"
87
"os"
98
"path/filepath"
@@ -18,7 +17,9 @@ To find all files under a path recursively which are above a minimum size limit.
1817
var (
1918
pathsAboveRedSizeMBLimit = flag.Int("minsize", 500, "minimum size to display in MBs")
2019
targetDir = flag.String("path", "/tmp", "path to scan")
20+
listDir = flag.Bool("dir", false, "list big size directories also")
2121
version = flag.Bool("version", false, "show me version")
22+
debug = flag.Bool("debug", false, "show extra information")
2223
)
2324

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

55-
func calculateDirSize(dirpath string) (dirsize int64, err error) {
56-
err = os.Chdir(dirpath)
57-
if err != nil {
58-
return
59-
}
60-
files, err := ioutil.ReadDir(".")
61-
if err != nil {
62-
return
56+
func changeSizeToMB(size float64, sizeType string) float64 {
57+
if sizeType == "b" {
58+
return (size / (1024 * 1024))
59+
} else if sizeType == "kb" {
60+
return (size / (1024))
61+
} else if sizeType == "mb" {
62+
return size
63+
} else if sizeType == "gb" {
64+
return size * 1024
6365
}
66+
return size
67+
}
6468

65-
for _, file := range files {
66-
if file.Mode().IsRegular() {
67-
dirsize += file.Size()
69+
func calculateDirSize(dirPath string) (dirSize int64, fullDirSize float64, err error) {
70+
err = filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error {
71+
if info.Mode().IsRegular() {
72+
dirSize += info.Size()
6873
}
69-
}
74+
return nil
75+
})
76+
77+
size, _, sizeType := sizeInHuman(dirSize)
78+
fullDirSize = changeSizeToMB(size, sizeType)
7079
return
7180
}
7281

82+
func analyzeSubDir(dirPath string, dirName string, sizeMBLimit float64) {
83+
_, dirSize, err := calculateDirSize(dirPath)
84+
if err != nil && *debug == true {
85+
log.Printf("not able to get dir size for: %s", dirPath)
86+
return
87+
} else if float64(dirSize) >= sizeMBLimit {
88+
if *listDir {
89+
fmt.Printf("\n📁Path: %s\n\tName: %s\n\tSize: %.2f MB\n", dirPath, dirName, dirSize)
90+
}
91+
analyzeDir(dirPath)
92+
}
93+
}
94+
7395
func analyzeDir(pathToScan string) {
7496
sizeMBLimit := float64(*pathsAboveRedSizeMBLimit)
7597
if entries, err := readDir(pathToScan); err == nil {
7698
for _, entry := range entries {
7799
filepath := filepath.Join(pathToScan, entry.Name())
78100
size, humanSize, sizeType := sizeInHuman(entry.Size())
79-
isDir := entry.IsDir()
80-
if isDir {
81-
dirsize, err := calculateDirSize(filepath)
82-
if err != nil {
83-
log.Fatalf("not able to get dir size for: %s", filepath)
84-
continue
85-
} else if float64(dirsize) >= sizeMBLimit {
86-
analyzeDir(filepath)
87-
}
101+
102+
if entry.IsDir() {
103+
analyzeSubDir(filepath, entry.Name(), sizeMBLimit)
88104
continue
89105
}
90106
if sizeType == "b" || sizeType == "kb" {
@@ -97,7 +113,7 @@ func analyzeDir(pathToScan string) {
97113
continue
98114
}
99115
}
100-
fmt.Printf("\n📁Path: %s\n\tName: %s\n\tSize: %s\n", filepath, entry.Name(), humanSize)
116+
fmt.Printf("\n📄Path: %s\n\tName: %s\n\tSize: %s\n", filepath, entry.Name(), humanSize)
101117
}
102118
} else {
103119
log.Fatalf("error reading dir %s", pathToScan)

0 commit comments

Comments
 (0)