-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
135 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,48 @@ | ||
package conf | ||
|
||
import ( | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
"github.com/triole/logseal" | ||
str2duration "github.com/xhit/go-str2duration/v2" | ||
) | ||
|
||
func parseDurationRangeArg(s string, lg logseal.Logseal) (minAge, maxAge time.Duration) { | ||
func parseDurationRangeArg(s string, lg logseal.Logseal) (min, max time.Duration) { | ||
var err error | ||
arr := strings.Split(s, ",") | ||
minAge, err = str2duration.ParseDuration(arr[0]) | ||
min, err = str2duration.ParseDuration(arr[0]) | ||
lg.IfErrFatal( | ||
"can not parse age range arg", | ||
logseal.F{"string": arr[0], "error": err}, | ||
) | ||
maxAge = time.Duration(0) | ||
max = time.Duration(0) | ||
if len(arr) > 1 { | ||
maxAge, err = str2duration.ParseDuration(arr[1]) | ||
max, err = str2duration.ParseDuration(arr[1]) | ||
lg.IfErrFatal( | ||
"can not parse age range arg", | ||
logseal.F{"string": arr[1], "error": err}, | ||
) | ||
} | ||
return | ||
} | ||
|
||
func parseNumberRangeArg(s string, lg logseal.Logseal) (min, max int) { | ||
var err error | ||
arr := strings.Split(s, ",") | ||
min, err = strconv.Atoi(arr[0]) | ||
lg.IfErrFatal( | ||
"can not parse number range arg", | ||
logseal.F{"string": arr[0], "error": err}, | ||
) | ||
max = min | ||
if len(arr) > 1 { | ||
max, err = strconv.Atoi(arr[1]) | ||
lg.IfErrFatal( | ||
"can not parse number range arg", | ||
logseal.F{"string": arr[1], "error": err}, | ||
) | ||
} | ||
return | ||
} |
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,17 @@ | ||
package fileaxe | ||
|
||
func (fa FileAxe) isInRange(val, min, max int) bool { | ||
if min == 0 && max == 0 { | ||
if val == 0 { | ||
return true | ||
} | ||
} else { | ||
if val >= min && max == 0 { | ||
return true | ||
} | ||
if val >= min && val <= max { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
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,31 @@ | ||
package fileaxe | ||
|
||
import ( | ||
"fileaxe/src/conf" | ||
"testing" | ||
|
||
"github.com/triole/logseal" | ||
) | ||
|
||
func TestIsInRange(t *testing.T) { | ||
validateIsInRange(3, 1, 5, true, t) | ||
validateIsInRange(0, 0, 0, true, t) | ||
validateIsInRange(1, 0, 0, false, t) | ||
validateIsInRange(1, 1, 0, true, t) | ||
validateIsInRange(20, 1, 0, true, t) | ||
validateIsInRange(2, 1, 0, true, t) | ||
validateIsInRange(2, 1, 1, false, t) | ||
} | ||
|
||
func validateIsInRange(val, min, max int, exp bool, t *testing.T) { | ||
conf := conf.InitTestConf("ex", "../testdata/tmp", "\\..*$") | ||
lg := logseal.Init() | ||
fa := Init(conf, lg) | ||
res := fa.isInRange(val, min, max) | ||
if res != exp { | ||
t.Errorf( | ||
"test isInRange failed: val, min, max: %d, %d, %d -- exp: %v, res: %v", | ||
val, min, max, exp, res, | ||
) | ||
} | ||
} |