Skip to content

Commit

Permalink
Add compress command
Browse files Browse the repository at this point in the history
  • Loading branch information
triole committed Aug 14, 2024
1 parent 7d92ffc commit 6f38a35
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 17 deletions.
6 changes: 5 additions & 1 deletion src/argparse.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var (
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, e.g. '\\..*$'" short:"m" default:".*"`
Matcher string `help:"regex matcher for file detection, e.g. '\\..*$' or '\\.yaml$" short:"m" default:".*"`
AgeRange string `help:"age range of files to consider, 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"`
Expand All @@ -41,6 +41,10 @@ var CLI struct {
List bool `help:"list all processed files" short:"l"`
} `cmd:"" help:"check if file(s) exists, return non-zero exitcode if not"`

Compress struct {
Format string `help:"compression format, if files are not removed" short:"g" default:"gz" enum:"snappy,gz,xz"`
} `cmd:"" help:"rotate matching files, compress and truncate after successful compression"`

Rotate 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
18 changes: 9 additions & 9 deletions src/conf/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ func Init(cli interface{}, lg logseal.Logseal) (conf Conf) {
conf.MsgPrefix = "(dry run) "
}
conf.Action = getcli(cli, "SubCommand").(string)
conf.Ls.Plain = getcli(cli, "Ls.Plain").(bool)
conf.Ls.Plain = getcli(cli, "List.Plain").(bool)
conf.Exists.MinNumber, conf.Exists.MaxNumber = parseNumberRangeArg(
getcli(cli, "Ex.NumberRange").(string), lg,
getcli(cli, "Exists.NumberRange").(string), lg,
)
conf.Exists.List = getcli(cli, "Ex.List").(bool)
conf.Remove.Yes = getcli(cli, "Rm.Yes").(bool)
conf.Rotate.CompressionFormat = getcli(cli, "Rt.Format").(string)
conf.Rotate.SkipTruncate = getcli(cli, "Rt.SkipTruncate").(bool)
conf.Copy.Target = getcli(cli, "Cp.Target").(string)
conf.Move.Target = getcli(cli, "Mv.Target").(string)
conf.Truncate.Yes = getcli(cli, "Tn.Yes").(bool)
conf.Exists.List = getcli(cli, "Exists.List").(bool)
conf.Remove.Yes = getcli(cli, "Remove.Yes").(bool)
conf.Rotate.CompressionFormat = getcli(cli, "Rotate.Format").(string)
conf.Rotate.SkipTruncate = getcli(cli, "Rotate.SkipTruncate").(bool)
conf.Copy.Target = getcli(cli, "Copy.Target").(string)
conf.Move.Target = getcli(cli, "Move.Target").(string)
conf.Truncate.Yes = getcli(cli, "Truncate.Yes").(bool)
return
}

Expand Down
5 changes: 5 additions & 0 deletions src/conf/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Conf struct {
DryRun bool
Ls tLs
Exists tExists
Compress tCompress
Rotate tRotate
Copy tCopyMove
Move tCopyMove
Expand All @@ -40,6 +41,10 @@ type tRemove struct {
Yes bool
}

type tCompress struct {
CompressionFormat string
}

type tRotate struct {
CompressionFormat string
SkipTruncate bool
Expand Down
16 changes: 16 additions & 0 deletions src/fileaxe/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,22 @@ func (fa FileAxe) exists(fileList FileInfos) {
}
}

func (fa FileAxe) compress(fileList FileInfos) {
for _, fil := range fileList {
tar := fa.makeCompressionTargetFileName(fil.Path)
fa.Lg.Trace("make target file name",
logseal.F{
"source": fil.Path, "target": tar,
},
)
err := fa.compressFile(fil, tar)
fa.Lg.IfErrError(
"can not truncate file",
logseal.F{"file": fil, "error": err},
)
}
}

func (fa FileAxe) rotate(fileList FileInfos) {
for _, fil := range fileList {
tar := fa.makeCompressionTargetFileName(fil.Path)
Expand Down
16 changes: 9 additions & 7 deletions src/fileaxe/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,21 @@ func (fa FileAxe) Run() {
fa.Conf.MinAge, fa.Conf.MaxAge, fa.Conf.Now,
)
switch fa.Conf.Action {
case "ls":
case "list":
fa.list(fileList)
case "ex":
case "exists":
fa.exists(fileList)
case "rt":
case "compress":
fa.compress(fileList)
case "rotate":
fa.rotate(fileList)
case "cp":
case "copy":
fa.copy(fileList)
case "mv":
case "move":
fa.move(fileList)
case "tn":
case "truncate":
fa.truncate(fileList)
case "rm":
case "remove":
fa.remove(fileList)
}
}

0 comments on commit 6f38a35

Please sign in to comment.