-
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(output): Add
-C
and -W
flags for output file rotation based …
…on size and count
- Loading branch information
Showing
13 changed files
with
529 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package types | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
type FlagTypeFileSize struct { | ||
val string | ||
n uint64 | ||
} | ||
|
||
func (s *FlagTypeFileSize) Set(val string) error { | ||
n, err := strconv.ParseUint(val, 10, 64) | ||
if err == nil { | ||
s.n = n * 1_000_000 | ||
return nil | ||
} | ||
val = strings.ToLower(val) | ||
switch { | ||
case strings.HasSuffix(val, "k"): | ||
val = strings.TrimSuffix(val, "k") | ||
n, err = strconv.ParseUint(val, 10, 64) | ||
if err != nil { | ||
return err | ||
} | ||
s.n = n * 1024 | ||
break | ||
case strings.HasSuffix(val, "kb"): | ||
val = strings.TrimSuffix(val, "kb") | ||
n, err = strconv.ParseUint(val, 10, 64) | ||
if err != nil { | ||
return err | ||
} | ||
s.n = n * 1024 | ||
break | ||
case strings.HasSuffix(val, "m"): | ||
val = strings.TrimSuffix(val, "m") | ||
n, err = strconv.ParseUint(val, 10, 64) | ||
if err != nil { | ||
return err | ||
} | ||
s.n = n * 1024 * 1024 | ||
break | ||
case strings.HasSuffix(val, "mb"): | ||
val = strings.TrimSuffix(val, "mb") | ||
n, err = strconv.ParseUint(val, 10, 64) | ||
if err != nil { | ||
return err | ||
} | ||
s.n = n * 1024 * 1024 | ||
break | ||
case strings.HasSuffix(val, "g"): | ||
val = strings.TrimSuffix(val, "g") | ||
n, err = strconv.ParseUint(val, 10, 64) | ||
if err != nil { | ||
return err | ||
} | ||
s.n = n * 1024 * 1024 * 1024 | ||
break | ||
case strings.HasSuffix(val, "gb"): | ||
val = strings.TrimSuffix(val, "gb") | ||
n, err = strconv.ParseUint(val, 10, 64) | ||
if err != nil { | ||
return err | ||
} | ||
s.n = n * 1024 * 1024 * 1024 | ||
break | ||
default: | ||
return fmt.Errorf("invalid file size: %s", val) | ||
} | ||
|
||
return nil | ||
} | ||
func (s *FlagTypeFileSize) Type() string { | ||
return "fileSize" | ||
} | ||
|
||
func (s *FlagTypeFileSize) Bytes() int64 { | ||
return int64(s.n) | ||
} | ||
|
||
func (s *FlagTypeFileSize) String() string { return string(s.val) } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package types | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestFlagTypeFileSize_Set(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
val string | ||
want uint64 | ||
wantErr bool | ||
}{ | ||
{"valid bytes", "1000", 1000 * 1000000, false}, | ||
{"valid kilobytes", "1k", 1 * 1024, false}, | ||
{"valid kilobytes", "1K", 1 * 1024, false}, | ||
{"valid kilobytes with kb", "1kb", 1 * 1024, false}, | ||
{"valid kilobytes with kb", "1KB", 1 * 1024, false}, | ||
{"valid megabytes", "1m", 1 * 1024 * 1024, false}, | ||
{"valid megabytes", "1M", 1 * 1024 * 1024, false}, | ||
{"valid megabytes with mb", "1mb", 1 * 1024 * 1024, false}, | ||
{"valid megabytes with mb", "1MB", 1 * 1024 * 1024, false}, | ||
{"valid gigabytes", "1g", 1 * 1024 * 1024 * 1024, false}, | ||
{"valid gigabytes", "1G", 1 * 1024 * 1024 * 1024, false}, | ||
{"valid gigabytes with gb", "1gb", 1 * 1024 * 1024 * 1024, false}, | ||
{"valid gigabytes with gb", "1GB", 1 * 1024 * 1024 * 1024, false}, | ||
{"invalid format", "1tb", 0, true}, | ||
{"invalid number", "abc", 0, true}, | ||
{"empty string", "", 0, true}, | ||
{"negative number", "-1k", 0, true}, | ||
{"zero value", "0", 0, false}, | ||
{"whitespace around value", " 1k ", 0, true}, | ||
{"uppercase suffix", "1K", 1 * 1024, false}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
s := &FlagTypeFileSize{} | ||
err := s.Set(tt.val) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("Set() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if s.n != tt.want { | ||
t.Errorf("Set() = %v, want %v", s.n, tt.want) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.