Skip to content

Commit

Permalink
Add move func
Browse files Browse the repository at this point in the history
  • Loading branch information
triole committed Jul 26, 2024
1 parent daa9d17 commit baa7c73
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 32 deletions.
12 changes: 9 additions & 3 deletions src/argparse.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ 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:"\\..*$"`
MaxAge string `help:"max age of files to consider, determined by last modified date, use with duration like i.e. 90m, 12h, 4d, 2w" short:"x" default:"0"`
SortBy string `help:"sort output list by, can be: age, path" short:"s" enum:"age,path" default:"path"`
Order string `help:"sort order" short:"o" enum:"asc,desc" default:"asc"`
MinAge string `help:"minimum age of files to consider, determined by last modified date, use with duration like i.e. 90m, 12h, 4d, 2w" default:"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"`
LogLevel string `help:"log level" default:"info" enum:"trace,debug,info,error,fatal"`
LogNoColors bool `help:"disable output colours, print plain text"`
Expand All @@ -41,6 +41,10 @@ var CLI struct {
SkipTruncate bool `help:"skip file truncation, don't empty compressed log files" short:"k"`
} `cmd:"" help:"rotate matching files, compress and truncate after successful compression"`

Move struct {
Target string `help:"target to which the files are moved" short:"t" required:""`
} `cmd:"" help:"move matching files older than max age, requires target folder definition"`

Remove struct {
Yes bool `help:"assume yes on remove affirmation query"`
} `cmd:"" help:"remove matching files older than max age"`
Expand All @@ -67,6 +71,8 @@ func parseArgs() {
CLI.SubCommand = "ls"
case "rotate":
CLI.SubCommand = "rotate"
case "move":
CLI.SubCommand = "move"
case "remove":
CLI.SubCommand = "remove"
default:
Expand Down
7 changes: 4 additions & 3 deletions src/conf/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ func Init(cli interface{}, lg logseal.Logseal) (conf Conf) {
}

conf.Matcher = getcli(cli, "Matcher").(string)
maxAgeArg := getcli(cli, "MaxAge").(string)
maxAgeArg := getcli(cli, "MinAge").(string)

conf.MaxAge, err = str2duration.ParseDuration(maxAgeArg)
conf.MinAge, err = str2duration.ParseDuration(maxAgeArg)
lg.IfErrFatal(
"can not parse max age arg",
logseal.F{"string": maxAgeArg, "error": err},
Expand All @@ -45,6 +45,7 @@ func Init(cli interface{}, lg logseal.Logseal) (conf Conf) {
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.Move.Target = getcli(cli, "Move.Target").(string)
return
}

Expand Down Expand Up @@ -77,7 +78,7 @@ func InitTestConf(subcommand, fol string) (conf Conf) {
conf.Action = subcommand
conf.Folder = "../testdata/tmp"
conf.Matcher = ".*"
conf.MaxAge = 0
conf.MinAge = 0
conf.Rotate.CompressionFormat = "gz"
return
}
9 changes: 7 additions & 2 deletions src/conf/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,25 @@ type Conf struct {
Now time.Time
Folder string
Matcher string
MaxAge time.Duration
MinAge time.Duration
SortBy string
Order string
DryRun bool
Ls tLs
Remove tRemove
Rotate tRotate
Move tMove
Remove tRemove
MsgPrefix string
}

type tLs struct {
Plain bool
}

type tMove struct {
Target string
}

type tRemove struct {
Yes bool
}
Expand Down
12 changes: 9 additions & 3 deletions src/fileaxe/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (fa FileAxe) rotate(fileList FileInfos) {

err := fa.compressFile(fil, tar)
if !fa.Conf.Rotate.SkipTruncate && err == nil {
err := fa.truncate(fil.Path)
err := fa.truncateFile(fil.Path)
fa.Lg.IfErrError(
"can not truncate file",
logseal.F{"file": fil, "error": err},
Expand All @@ -41,14 +41,20 @@ func (fa FileAxe) rotate(fileList FileInfos) {
}
}

func (fa FileAxe) move(fileList FileInfos) {
for _, fil := range fileList {
fa.moveFile(fil, fa.Conf.Move.Target)
}
}

func (fa FileAxe) remove(fileList FileInfos) {
for _, fil := range fileList {
if !fa.Conf.DryRun {
if fa.Conf.Remove.Yes {
fa.rm(fil.Path)
fa.removeFile(fil.Path)
} else {
if askForConfirmation(fil.Path) {
fa.rm(fil.Path)
fa.removeFile(fil.Path)
}
}
} else {
Expand Down
103 changes: 86 additions & 17 deletions src/fileaxe/fileops.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package fileaxe

import (
"fmt"
"io"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -69,9 +70,9 @@ func (fa FileAxe) Find(basedir string, rxFilter string, maxAge time.Duration, re
if rxf.MatchString(path) {
fi := fa.fileInfo(path, refTime)
if fi.Err == nil && !fi.IsDir {
fi.SortIndex = fa.makeSortIndexPath(fi)
if fa.Conf.SortBy == "age" {
fi.SortIndex = fa.makeSortIndexAge(fi)
fi.SortIndex = fa.makeSortIndexAge(fi)
if fa.Conf.SortBy == "path" {
fi.SortIndex = fa.makeSortIndexPath(fi)
}
if maxAge == 0 {
fileList = append(fileList, fi)
Expand Down Expand Up @@ -125,27 +126,81 @@ func (fa FileAxe) fileInfo(path string, refTime time.Time) (fi FileInfo) {
return
}

func (fa FileAxe) truncate(filename string) error {
fa.Lg.Info(fa.Conf.MsgPrefix+"truncate", logseal.F{"file": filename})
func (fa FileAxe) moveFile(fil FileInfo, destPath string) (err error) {
sourcePath := fil.Path
fa.Lg.Info(
fa.Conf.MsgPrefix+"move file",
logseal.F{
"source_age": fil.Age,
"source_path": sourcePath,
"destination_path": destPath},
)
if !fa.Conf.DryRun {
f, err := os.OpenFile(filename, os.O_TRUNC, 0664)
var inputFile *os.File
var outputFile *os.File
inputFile, err = os.Open(sourcePath)
if err != nil {
return fmt.Errorf("could not open file %q for truncation: %v", filename, err)
fa.Lg.Error(
"can not open source file",
logseal.F{
"source_age": fil.Age,
"source_path": sourcePath,
"destination_path": destPath},
)
return
}
if err = f.Close(); err != nil {
return fmt.Errorf("could not close file handler for %q after truncation: %v", filename, err)
defer inputFile.Close()

outputFile, err = os.Create(destPath)
if err != nil {
fa.Lg.Error(
"can not open destination file",
logseal.F{
"source_age": fil.Age,
"source_path": sourcePath,
"destination_path": destPath},
)
return
}
}
return nil
}
defer outputFile.Close()

func (fa FileAxe) rm(filepath string) {
if fa.Conf.DryRun {
fa.Lg.Info(
"dry run, would have removed file",
logseal.F{"path": filepath},
"copy file",
logseal.F{
"source_age": fil.Age,
"source_path": sourcePath,
"destination_path": destPath},
)
} else {
_, err = io.Copy(outputFile, inputFile)
if err != nil {
fa.Lg.Error(
"can not copy file",
logseal.F{
"source_age": fil.Age,
"source_path": sourcePath,
"destination_path": destPath},
)
return
}
inputFile.Close() // for Windows, close before remove

err = os.Remove(sourcePath)
if err != nil {
fa.Lg.Error(
"can not remove source file",
logseal.F{
"source_age": fil.Age,
"source_path": sourcePath,
"destination_path": destPath},
)
}
}
return err
}

func (fa FileAxe) removeFile(filepath string) {
fa.Lg.Info(fa.Conf.MsgPrefix+"remove file", logseal.F{"path": filepath})
if !fa.Conf.DryRun {
err := os.Remove(filepath)
if err == nil {
fa.Lg.Info("file removed", logseal.F{"path": filepath})
Expand All @@ -156,3 +211,17 @@ func (fa FileAxe) rm(filepath string) {
)
}
}

func (fa FileAxe) truncateFile(filepath string) error {
fa.Lg.Info(fa.Conf.MsgPrefix+"truncate", logseal.F{"file": filepath})
if !fa.Conf.DryRun {
f, err := os.OpenFile(filepath, os.O_TRUNC, 0664)
if err != nil {
return fmt.Errorf("could not open file %q for truncation: %v", filepath, err)
}
if err = f.Close(); err != nil {
return fmt.Errorf("could not close file handler for %q after truncation: %v", filepath, err)
}
}
return nil
}
8 changes: 4 additions & 4 deletions src/fileaxe/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ func (fa FileAxe) Run() {
if !fa.Conf.Ls.Plain {
fa.Lg.Debug(
"start fileaxe",
logseal.F{
"conf": fmt.Sprintf("%+v", fa.Conf),
},
logseal.F{"conf": fmt.Sprintf("%+v", fa.Conf)},
)
}

fileList := fa.Find(fa.Conf.Folder, fa.Conf.Matcher, fa.Conf.MaxAge, fa.Conf.Now)
fileList := fa.Find(fa.Conf.Folder, fa.Conf.Matcher, fa.Conf.MinAge, fa.Conf.Now)
switch fa.Conf.Action {
case "ls":
fa.list(fileList)
case "rotate":
fa.rotate(fileList)
case "move":
fa.move(fileList)
case "remove":
fa.remove(fileList)
}
Expand Down

0 comments on commit baa7c73

Please sign in to comment.