Skip to content

Commit

Permalink
Add exists function
Browse files Browse the repository at this point in the history
  • Loading branch information
triole committed Jul 29, 2024
1 parent 9de4163 commit 255df1a
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 6 deletions.
6 changes: 5 additions & 1 deletion src/argparse.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var CLI struct {
SubCommand string `kong:"-"`
Folder string `help:"folder to process, default is current directory" short:"f" default:"${curdir}"`
Matcher string `help:"regex matcher for file detection" short:"m" default:"\\..*$"`
AgeRange string `help:"age range of files to consider, takes a string consisting of one or two comma separated values, min age and max age, supports durations like 90m, 12h, 4d, 2w; default behaviour is that all files in a folder will be considered, usage: -r 2h, -r 30m,2h" short:"r" default:"0,0"`
AgeRange string `help:"age range of files to consider, takes a string of one or two comma separated values, min age and max age, supports durations like 90m, 12h, 4d, 2w; default behaviour is that all files in a folder will be considered, usage: -r 2h, -r 30m,2h" short:"r" default:"0,0"`
SortBy string `help:"sort output list by, can be: age, path" short:"s" enum:"age,path" default:"age"`
Order string `help:"sort order" short:"o" enum:"asc,desc" default:"desc"`
LogFile string `help:"log file" default:"/dev/stdout"`
Expand All @@ -36,6 +36,10 @@ var CLI struct {
Plain bool `help:"print plain list, file names only" short:"p"`
} `cmd:"" help:"list files matching the criteria"`

Ex struct {
NumberRange string `help:"number of files to be considered as a valid match, can be string of one or two comma separated values, min and max number," short:"b" default:"1,0"`
} `cmd:"" help:"check if file(s) exists, return non-zero exitcode if not"`

Rt struct {
Format string `help:"compression format, if files are not removed" short:"g" default:"gz" enum:"snappy,gz,xz"`
SkipTruncate bool `help:"skip file truncation, don't empty compressed log files" short:"k"`
Expand Down
7 changes: 6 additions & 1 deletion src/conf/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ func Init(cli interface{}, lg logseal.Logseal) (conf Conf) {
}

conf.Matcher = getcli(cli, "Matcher").(string)
conf.MinAge, conf.MaxAge = parseDurationRangeArg(getcli(cli, "AgeRange").(string), lg)
conf.MinAge, conf.MaxAge = parseDurationRangeArg(
getcli(cli, "AgeRange").(string), lg,
)

conf.SortBy = getcli(cli, "SortBy").(string)
conf.Order = getcli(cli, "Order").(string)
Expand All @@ -35,6 +37,9 @@ func Init(cli interface{}, lg logseal.Logseal) (conf Conf) {
}
conf.Action = getcli(cli, "SubCommand").(string)
conf.Ls.Plain = getcli(cli, "Ls.Plain").(bool)
conf.Exists.MinNumber, conf.Exists.MaxNumber = parseNumberRangeArg(
getcli(cli, "Ex.NumberRange").(string), lg,
)
conf.Remove.Yes = getcli(cli, "Rm.Yes").(bool)
conf.Rotate.CompressionFormat = getcli(cli, "Rt.Format").(string)
conf.Rotate.SkipTruncate = getcli(cli, "Rt.SkipTruncate").(bool)
Expand Down
28 changes: 24 additions & 4 deletions src/conf/range.go
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
}
26 changes: 26 additions & 0 deletions src/conf/range_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ func TestParseDurationRangeArg(t *testing.T) {
validateParseDurationRangeArg("0", time.Second*0, time.Second*0, lg, t)
}

func TestParseNumberRangeArg(t *testing.T) {
lg := logseal.Init()
validateParseNumberRangeArg("1", 1, 1, lg, t)
validateParseNumberRangeArg("2,0", 2, 0, lg, t)
validateParseNumberRangeArg("11,0", 11, 0, lg, t)
validateParseNumberRangeArg("3,30", 3, 30, lg, t)
validateParseNumberRangeArg("4,40", 4, 40, lg, t)
validateParseNumberRangeArg("0,50", 0, 50, lg, t)
}

func validateParseDurationRangeArg(s string, expMin, expMax time.Duration, lg logseal.Logseal, t *testing.T) {
min, max := parseDurationRangeArg(s, lg)
if min != expMin {
Expand All @@ -31,3 +41,19 @@ func validateParseDurationRangeArg(s string, expMin, expMax time.Duration, lg lo
)
}
}

func validateParseNumberRangeArg(s string, expMin, expMax int, lg logseal.Logseal, t *testing.T) {
min, max := parseNumberRangeArg(s, lg)
if min != expMin {
t.Errorf(
"failed test parseDurationRangeArg, min != expMin: %d != %d",
min, expMin,
)
}
if max != expMax {
t.Errorf(
"failed test parseDurationRangeArg, max != expMax: %d != %d",
max, expMax,
)
}
}
6 changes: 6 additions & 0 deletions src/conf/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type Conf struct {
Order string
DryRun bool
Ls tLs
Exists tExists
Rotate tRotate
Copy tCopyMove
Move tCopyMove
Expand All @@ -25,6 +26,11 @@ type tLs struct {
Plain bool
}

type tExists struct {
MinNumber int
MaxNumber int
}

type tCopyMove struct {
Target string
}
Expand Down
18 changes: 18 additions & 0 deletions src/fileaxe/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package fileaxe

import (
"fmt"
"os"

"github.com/triole/logseal"
)
Expand All @@ -19,6 +20,23 @@ func (fa FileAxe) list(fileList FileInfos) {
}
}

func (fa FileAxe) exists(fileList FileInfos) {
match_no := len(fileList)
success := fa.isInRange(match_no, fa.Conf.Exists.MinNumber, fa.Conf.Exists.MaxNumber)
fa.Lg.Info(
"exists check results",
logseal.F{
"exp_min": fa.Conf.Exists.MinNumber,
"exp_max": fa.Conf.Exists.MaxNumber,
"no": match_no,
"success": success,
},
)
if !success {
os.Exit(1)
}
}

func (fa FileAxe) rotate(fileList FileInfos) {
for _, fil := range fileList {
tar := fa.makeCompressionTargetFileName(fil.Path)
Expand Down
2 changes: 2 additions & 0 deletions src/fileaxe/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ func (fa FileAxe) Run() {
switch fa.Conf.Action {
case "ls":
fa.list(fileList)
case "ex":
fa.exists(fileList)
case "rt":
fa.rotate(fileList)
case "cp":
Expand Down
17 changes: 17 additions & 0 deletions src/fileaxe/range.go
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
}
31 changes: 31 additions & 0 deletions src/fileaxe/range_test.go
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,
)
}
}

0 comments on commit 255df1a

Please sign in to comment.