Add ability to specify bytes options as strings with units (KB, MiB, ...) (#4852)

This commit is contained in:
Samuel-BF
2018-10-19 20:17:18 +02:00
committed by Daniel Nelson
parent 17360f079c
commit 589d0587f6
16 changed files with 117 additions and 61 deletions

View File

@@ -19,10 +19,11 @@ Counts files in directories that match certain criteria.
## Only count regular files. Defaults to true.
regular_only = true
## Only count files that are at least this size in bytes. If size is
## Only count files that are at least this size. If size is
## a negative number, only count files that are smaller than the
## absolute value of size. Defaults to 0.
size = 0
## absolute value of size. Acceptable units are B, KiB, MiB, KB, ...
## Without quotes and units, interpreted as size in bytes.
size = "0B"
## Only count files that have not been touched for at least this
## duration. If mtime is negative, only count files that have been

View File

@@ -34,10 +34,11 @@ const sampleConfig = `
## Only count regular files. Defaults to true.
regular_only = true
## Only count files that are at least this size in bytes. If size is
## Only count files that are at least this size. If size is
## a negative number, only count files that are smaller than the
## absolute value of size. Defaults to 0.
size = 0
## absolute value of size. Acceptable units are B, KiB, MiB, KB, ...
## Without quotes and units, interpreted as size in bytes.
size = "0B"
## Only count files that have not been touched for at least this
## duration. If mtime is negative, only count files that have been
@@ -51,7 +52,7 @@ type FileCount struct {
Name string
Recursive bool
RegularOnly bool
Size int64
Size internal.Size
MTime internal.Duration `toml:"mtime"`
fileFilters []fileFilterFunc
}
@@ -99,7 +100,7 @@ func (fc *FileCount) regularOnlyFilter() fileFilterFunc {
}
func (fc *FileCount) sizeFilter() fileFilterFunc {
if fc.Size == 0 {
if fc.Size.Size == 0 {
return nil
}
@@ -107,10 +108,10 @@ func (fc *FileCount) sizeFilter() fileFilterFunc {
if !f.Mode().IsRegular() {
return false, nil
}
if fc.Size < 0 {
return f.Size() < -fc.Size, nil
if fc.Size.Size < 0 {
return f.Size() < -fc.Size.Size, nil
}
return f.Size() >= fc.Size, nil
return f.Size() >= fc.Size.Size, nil
}
}
@@ -257,7 +258,7 @@ func NewFileCount() *FileCount {
Name: "*",
Recursive: true,
RegularOnly: true,
Size: 0,
Size: internal.Size{Size: 0},
MTime: internal.Duration{Duration: 0},
fileFilters: nil,
}

View File

@@ -70,7 +70,7 @@ func TestRegularOnlyFilter(t *testing.T) {
func TestSizeFilter(t *testing.T) {
fc := getNoFilterFileCount("testdata")
fc.Size = -100
fc.Size = internal.Size{Size: -100}
matches := []string{"foo", "bar", "baz", "subdir/quux", "subdir/quuz"}
acc := testutil.Accumulator{}
@@ -78,7 +78,7 @@ func TestSizeFilter(t *testing.T) {
require.True(t, assertFileCount(&acc, "testdata", len(matches)))
fc.Size = 100
fc.Size = internal.Size{Size: 100}
matches = []string{"qux"}
acc = testutil.Accumulator{}
@@ -119,7 +119,7 @@ func getNoFilterFileCount(dir string) FileCount {
Name: "*",
Recursive: true,
RegularOnly: false,
Size: 0,
Size: internal.Size{Size: 0},
MTime: internal.Duration{Duration: 0},
fileFilters: nil,
}