Skip to content

Commit

Permalink
Simplify compress
Browse files Browse the repository at this point in the history
  • Loading branch information
triole committed Jul 20, 2024
1 parent 9dc66ac commit 68bd7d1
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 100 deletions.
4 changes: 3 additions & 1 deletion src/conf/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ func Init(cli interface{}, lg logseal.Logseal) (conf Conf) {
logseal.F{"string": maxAgeArg, "error": err},
)
conf.DryRun = getcli(cli, "DryRun").(bool)

if conf.DryRun {
conf.MsgPrefix = "(dry run) "
}
conf.Action = getcli(cli, "SubCommand").(string)
conf.Ls.Plain = getcli(cli, "Ls.Plain").(bool)
conf.Remove.Yes = getcli(cli, "Remove.Yes").(bool)
Expand Down
19 changes: 10 additions & 9 deletions src/conf/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ package conf
import "time"

type Conf struct {
Action string
Now time.Time
Folder string
Matcher string
MaxAge time.Duration
DryRun bool
Ls tLs
Remove tRemove
Rotate tRotate
Action string
Now time.Time
Folder string
Matcher string
MaxAge time.Duration
DryRun bool
Ls tLs
Remove tRemove
Rotate tRotate
MsgPrefix string
}

type tLs struct {
Expand Down
62 changes: 20 additions & 42 deletions src/fileaxe/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import (
"github.com/triole/logseal"
)

func (fa FileAxe) list() {
logFiles := fa.Find(fa.Conf.Folder, fa.Conf.Matcher, fa.Conf.MaxAge, fa.Conf.Now)
for _, el := range logFiles {
func (fa FileAxe) list(fileList FileInfos) {
for _, el := range fileList {
if fa.Conf.Ls.Plain {
fmt.Printf("%s\n", el.Path)
} else {
Expand All @@ -20,13 +19,12 @@ func (fa FileAxe) list() {
}
}

func (fa FileAxe) rotate() {
logFiles := fa.Find(fa.Conf.Folder, fa.Conf.Matcher, 0, fa.Conf.Now)
for _, fil := range logFiles {
tar := fa.makeZipArchiveFilenameAndDetectionScheme(fil.Path)
fa.Lg.Trace("make file name and detection scheme",
func (fa FileAxe) rotate(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, "detection_scheme": tar.DetectionScheme,
"source": fil.Path, "target": tar,
},
)

Expand All @@ -38,46 +36,26 @@ func (fa FileAxe) rotate() {
logseal.F{"file": fil, "error": err},
)
} else {
fa.Lg.Debug("skip truncate")
}

if fa.Conf.MaxAge > 0 {
compressedLogs := fa.Find(
fa.Conf.Folder, tar.DetectionScheme,
fa.Conf.MaxAge, fa.Conf.Now,
)
for _, fil := range compressedLogs {
if !fa.Conf.DryRun {
fa.rm(fil.Path)
}
}
fa.Lg.Debug("skip truncate", logseal.F{"file": fil})
}
}
}

func (fa FileAxe) remove() {
if fa.Conf.MaxAge > 0 {
files := fa.Find(
fa.Conf.Folder, fa.Conf.Matcher,
fa.Conf.MaxAge, fa.Conf.Now,
)
for _, fil := range files {
if !fa.Conf.DryRun {
if fa.Conf.Remove.Yes {
func (fa FileAxe) remove(fileList FileInfos) {
for _, fil := range fileList {
if !fa.Conf.DryRun {
if fa.Conf.Remove.Yes {
fa.rm(fil.Path)
} else {
if askForConfirmation(fil.Path) {
fa.rm(fil.Path)
} else {
if askForConfirmation(fil.Path) {
fa.rm(fil.Path)
}
}
} else {
fa.Lg.Info(
"dry run, might have removed file",
logseal.F{"path": fil.Path},
)
}
} else {
fa.Lg.Info(
"dry run, might have removed file",
logseal.F{"path": fil.Path},
)
}
} else {
fa.Lg.Info("nothing to do, remove mode requires a max age definition, use --max-age or -m")
}
}
47 changes: 17 additions & 30 deletions src/fileaxe/compression.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,15 @@ package fileaxe
import (
"context"
"os"
"path"
"path/filepath"
"strings"
"time"

"github.com/mholt/archiver/v4"
"github.com/triole/logseal"
)

type tTarget struct {
Folder string
FullPath string
BaseName string
ShortName string
DetectionScheme string
}

func (fa FileAxe) compressFile(sourceFile FileInfo, target tTarget) (err error) {
func (fa FileAxe) compressFile(sourceFile FileInfo, target string) (err error) {
start := time.Now()

format := archiver.CompressedArchive{
Expand All @@ -41,10 +33,10 @@ func (fa FileAxe) compressFile(sourceFile FileInfo, target tTarget) (err error)
Archival: archiver.Tar{},
}
}

fa.Lg.Info("compress file", logseal.F{
"file": sourceFile.Path,
"size": sourceFile.SizeHR,
fa.Lg.Info(fa.Conf.MsgPrefix+"compress file", logseal.F{
"file": sourceFile.Path,
"size": sourceFile.SizeHR,
"target": target,
})

sourceFilesArr := []string{sourceFile.Path}
Expand All @@ -54,11 +46,11 @@ func (fa FileAxe) compressFile(sourceFile FileInfo, target tTarget) (err error)
elapsed := end.Sub(start)

if err == nil {
taInfos := fa.fileInfo(target.FullPath, time.Now())
taInfos := fa.fileInfo(target, time.Now())
fa.Lg.Info(
"compression done",
logseal.F{
"file": target.FullPath, "duration": elapsed,
"file": target, "duration": elapsed,
"size": taInfos.SizeHR,
},
)
Expand All @@ -74,11 +66,11 @@ func (fa FileAxe) compressFile(sourceFile FileInfo, target tTarget) (err error)
return
}

func (fa FileAxe) runCompression(sources []string, target tTarget, format archiver.CompressedArchive) (err error) {
func (fa FileAxe) runCompression(sources []string, target string, format archiver.CompressedArchive) (err error) {
var files []archiver.File
fileMap := make(map[string]string)
for _, fil := range sources {
fileMap[fil] = target.BaseName
fileMap[fil] = filepath.Base(target)
}

files, err = archiver.FilesFromDisk(nil, fileMap)
Expand All @@ -90,7 +82,7 @@ func (fa FileAxe) runCompression(sources []string, target tTarget, format archiv
return err
}

out, err := os.Create(target.FullPath)
out, err := os.Create(target)
if err != nil {
fa.Lg.Error(
"can not create file",
Expand All @@ -111,17 +103,12 @@ func (fa FileAxe) runCompression(sources []string, target tTarget, format archiv
return nil
}

func (fa FileAxe) makeZipArchiveFilenameAndDetectionScheme(fn string) (tar tTarget) {
tar.Folder = rxFind(".*\\/", fn)
base := rxFind("[^/]+$", fn)
base = rxFind(".*?\\.", base)
base = strings.TrimSuffix(base, ".")
tar.BaseName = base + "_" + timestamp() + ".log"
tar.ShortName = tar.BaseName + "." + fa.Conf.Rotate.CompressionFormat
tar.DetectionScheme = path.Join(
tar.Folder,
base+"_[0-2][0-9]{3}[0-1][0-9][0-3][0-9]t[0-2][0-9][0-5][0-9][0-5][0-9]\\.log\\."+fa.Conf.Rotate.CompressionFormat+"$",
func (fa FileAxe) makeCompressionTargetFileName(fn string) (tar string) {
cleanBase := rxReplaceAllString(filepath.Base(fn), "[^A-Za-z0-9_\\-]", "_")
noext := strings.TrimSuffix(cleanBase, filepath.Ext(fn))

tar = filepath.Join(
filepath.Dir(fn), noext+"_"+timestamp()+"."+fa.Conf.Rotate.CompressionFormat,
)
tar.FullPath = path.Join(tar.Folder, tar.ShortName)
return
}
2 changes: 1 addition & 1 deletion src/fileaxe/fileops.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (fa FileAxe) fileInfo(path string, refTime time.Time) (fi FileInfo) {
}

func (fa FileAxe) truncate(filename string) error {
fa.Lg.Info("truncate", logseal.F{"file": filename})
fa.Lg.Info(fa.Conf.MsgPrefix+"truncate", logseal.F{"file": filename})
if !fa.Conf.DryRun {
f, err := os.OpenFile(filename, os.O_TRUNC, 0664)
if err != nil {
Expand Down
19 changes: 5 additions & 14 deletions src/fileaxe/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,21 @@ import (

func (fa FileAxe) Run() {
if !fa.Conf.Ls.Plain {
fa.Lg.Info(
fa.Lg.Debug(
"start fileaxe",
logseal.F{
"conf": fmt.Sprintf("%+v", fa.Conf),
},
)

if fa.Conf.DryRun {
fa.Lg.Info(" --- DRY RUN START ---")
}
}

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

if !fa.Conf.Ls.Plain {
if fa.Conf.DryRun {
fa.Lg.Info(" --- DRY RUN END ---")
}
fa.remove(fileList)
}
}
6 changes: 3 additions & 3 deletions src/fileaxe/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ func round(val float64, roundOn float64, places int) (newVal float64) {
return
}

func rxFind(rx string, content string) (r string) {
temp, _ := regexp.Compile(rx)
r = temp.FindString(content)
func rxReplaceAllString(basestring, regex, newstring string) (r string) {
rx := regexp.MustCompile(regex)
r = rx.ReplaceAllString(basestring, newstring)
return
}

Expand Down

0 comments on commit 68bd7d1

Please sign in to comment.