Skip to content

Commit

Permalink
split into packages
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Milde committed Dec 30, 2020
1 parent 97f5c24 commit bd1ffe1
Show file tree
Hide file tree
Showing 16 changed files with 276 additions and 267 deletions.
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
VERSION := $(shell git describe --tags)
PACKAGES := $(shell go list ./...)

run:
go run .
Expand All @@ -12,10 +13,10 @@ build:
cd build; GOOS=darwin GOARCH=amd64 go build -ldflags="-s -w -X 'main.AppVersion=$(VERSION)'" -o gdu-darwin-amd64 ..; tar czf gdu-darwin-amd64.tgz gdu-darwin-amd64

test:
go test -v
go test -v $(PACKAGES)

coverage:
go test -v -race -coverprofile=coverage.txt -covermode=atomic
go test -v -race -coverprofile=coverage.txt -covermode=atomic $(PACKAGES)

clean:
-rm coverage.txt
Expand Down
9 changes: 9 additions & 0 deletions analyze/dev.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package analyze

// Device struct
type Device struct {
Name string
MountPoint string
Size int64
Free int64
}
10 changes: 5 additions & 5 deletions dev_linux.go → analyze/dev_linux.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// +build linux,amd64

package main
package analyze

import (
"bufio"
Expand Down Expand Up @@ -34,10 +34,10 @@ func GetDevicesInfo() []*Device {
syscall.Statfs(parts[1], info)

device := &Device{
name: parts[0],
mountPoint: parts[1],
size: info.Bsize * int64(info.Blocks),
free: info.Bsize * int64(info.Bavail),
Name: parts[0],
MountPoint: parts[1],
Size: info.Bsize * int64(info.Blocks),
Free: info.Bsize * int64(info.Bavail),
}
devices = append(devices, device)
}
Expand Down
2 changes: 1 addition & 1 deletion dev_other.go → analyze/dev_other.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// +build windows darwin linux,arm

package main
package analyze

// GetDevicesInfo returns usage info about mounted devices (by calling Statfs syscall)
func GetDevicesInfo() []*Device {
Expand Down
48 changes: 24 additions & 24 deletions dir.go → analyze/dir.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package analyze

import (
"io/ioutil"
Expand All @@ -10,11 +10,11 @@ import (

// CurrentProgress struct
type CurrentProgress struct {
mutex *sync.Mutex
currentItemName string
itemCount int
totalSize int64
done bool
Mutex *sync.Mutex
CurrentItemName string
ItemCount int
TotalSize int64
Done bool
}

// ShouldBeIgnored whether path should be ignored
Expand Down Expand Up @@ -44,11 +44,11 @@ func processDir(path string, progress *CurrentProgress, concurrencyLimitChannel
}

dir := File{
name: filepath.Base(path),
path: path,
isDir: true,
itemCount: 1,
files: make([]*File, 0, len(files)),
Name: filepath.Base(path),
Path: path,
IsDir: true,
ItemCount: 1,
Files: make([]*File, 0, len(files)),
}

var mutex sync.Mutex
Expand All @@ -66,34 +66,34 @@ func processDir(path string, progress *CurrentProgress, concurrencyLimitChannel
go func() {
concurrencyLimitChannel <- true
file = processDir(entryPath, progress, concurrencyLimitChannel, wait, ignore)
file.parent = &dir
file.Parent = &dir
mutex.Lock()
dir.files = append(dir.files, file)
dir.Files = append(dir.Files, file)
mutex.Unlock()
<-concurrencyLimitChannel
wait.Done()
}()
} else {
file = &File{
name: f.Name(),
path: entryPath,
size: f.Size(),
itemCount: 1,
parent: &dir,
Name: f.Name(),
Path: entryPath,
Size: f.Size(),
ItemCount: 1,
Parent: &dir,
}
totalSize += f.Size()

mutex.Lock()
dir.files = append(dir.files, file)
dir.Files = append(dir.Files, file)
mutex.Unlock()
}
}

progress.mutex.Lock()
progress.currentItemName = path
progress.itemCount += len(files)
progress.totalSize += totalSize
progress.mutex.Unlock()
progress.Mutex.Lock()
progress.CurrentItemName = path
progress.ItemCount += len(files)
progress.TotalSize += totalSize
progress.Mutex.Unlock()

return &dir
}
42 changes: 42 additions & 0 deletions analyze/dir_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package analyze

import (
"sync"
"testing"

"github.com/stretchr/testify/assert"
)

func TestProcessDir(t *testing.T) {
fin := CreateTestDir()
defer fin()

dir := ProcessDir("test_dir", &CurrentProgress{Mutex: &sync.Mutex{}}, func(_ string) bool { return false })

// test dir info
assert.Equal(t, "test_dir", dir.Name)
assert.Equal(t, int64(7), dir.Size)
assert.Equal(t, 5, dir.ItemCount)
assert.True(t, dir.IsDir)

// test dir tree
assert.Equal(t, "nested", dir.Files[0].Name)
assert.Equal(t, "subnested", dir.Files[0].Files[1].Name)

// test file
assert.Equal(t, "file2", dir.Files[0].Files[0].Name)
assert.Equal(t, int64(2), dir.Files[0].Files[0].Size)

assert.Equal(t, "file", dir.Files[0].Files[1].Files[0].Name)
assert.Equal(t, int64(5), dir.Files[0].Files[1].Files[0].Size)

// test parent link
assert.Equal(t, "test_dir", dir.Files[0].Files[1].Files[0].Parent.Parent.Parent.Name)
}

func TestIgnoreDir(t *testing.T) {
dir := ProcessDir("/proc", &CurrentProgress{Mutex: &sync.Mutex{}}, func(_ string) bool { return true })

assert.Equal(t, "proc", dir.Name)
assert.Equal(t, 1, dir.ItemCount)
}
40 changes: 20 additions & 20 deletions file.go → analyze/file.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package main
package analyze

import (
"os"
)

// File struct
type File struct {
name string
path string
size int64
itemCount int
isDir bool
files Files
parent *File
Name string
Path string
Size int64
ItemCount int
IsDir bool
Files Files
Parent *File
}

// Files - slice of pointers to File
Expand All @@ -39,37 +39,37 @@ func (s Files) Remove(file *File) Files {

// RemoveFile removes file from dir
func (f *File) RemoveFile(file *File) {
error := os.RemoveAll(file.path)
error := os.RemoveAll(file.Path)
if error != nil {
panic(error)
}

f.files = f.files.Remove(file)
f.Files = f.Files.Remove(file)

cur := f
for {
cur.itemCount -= file.itemCount
cur.size -= file.size
cur.ItemCount -= file.ItemCount
cur.Size -= file.Size

if cur.parent == nil {
if cur.Parent == nil {
break
}
cur = cur.parent
cur = cur.Parent
}
}

// UpdateStats recursively updates size and item count
func (f *File) UpdateStats() {
if !f.isDir {
if !f.IsDir {
return
}
var totalSize int64
var itemCount int
for _, entry := range f.files {
for _, entry := range f.Files {
entry.UpdateStats()
totalSize += entry.size
itemCount += entry.itemCount
totalSize += entry.Size
itemCount += entry.ItemCount
}
f.itemCount = itemCount + 1
f.size = totalSize
f.ItemCount = itemCount + 1
f.Size = totalSize
}
91 changes: 91 additions & 0 deletions analyze/file_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package analyze

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestFind(t *testing.T) {
dir := File{
Name: "xxx",
Size: 5,
ItemCount: 2,
}

file := &File{
Name: "yyy",
Size: 2,
ItemCount: 1,
Parent: &dir,
}
file2 := &File{
Name: "zzz",
Size: 3,
ItemCount: 1,
Parent: &dir,
}
dir.Files = []*File{file, file2}

assert.Equal(t, 0, dir.Files.Find(file))
assert.Equal(t, 1, dir.Files.Find(file2))
}

func TestRemove(t *testing.T) {
dir := File{
Name: "xxx",
Size: 5,
ItemCount: 2,
}

file := &File{
Name: "yyy",
Size: 2,
ItemCount: 1,
Parent: &dir,
}
file2 := &File{
Name: "zzz",
Size: 3,
ItemCount: 1,
Parent: &dir,
}
dir.Files = []*File{file, file2}

dir.Files = dir.Files.Remove(file)

assert.Equal(t, 1, len(dir.Files))
assert.Equal(t, file2, dir.Files[0])
}

func TestRemoveFile(t *testing.T) {
dir := &File{
Name: "xxx",
Size: 5,
ItemCount: 3,
}

subdir := &File{
Name: "yyy",
Size: 4,
ItemCount: 2,
Parent: dir,
}
file := &File{
Name: "zzz",
Size: 3,
ItemCount: 1,
Parent: subdir,
}
dir.Files = []*File{subdir}
subdir.Files = []*File{file}

subdir.RemoveFile(file)

assert.Equal(t, 0, len(subdir.Files))
assert.Equal(t, 1, subdir.ItemCount)
assert.Equal(t, int64(1), subdir.Size)
assert.Equal(t, 1, len(dir.Files))
assert.Equal(t, 2, dir.ItemCount)
assert.Equal(t, int64(2), dir.Size)
}
16 changes: 16 additions & 0 deletions analyze/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package analyze

import (
"io/ioutil"
"os"
)

// CreateTestDir creates test dir structure
func CreateTestDir() func() {
os.MkdirAll("test_dir/nested/subnested", os.ModePerm)
ioutil.WriteFile("test_dir/nested/subnested/file", []byte("hello"), 0644)
ioutil.WriteFile("test_dir/nested/file2", []byte("go"), 0644)
return func() {
os.RemoveAll("test_dir")
}
}
Loading

0 comments on commit bd1ffe1

Please sign in to comment.