diff --git a/src/conf/init.go b/src/conf/init.go index 5112bf0..2a2ddd3 100644 --- a/src/conf/init.go +++ b/src/conf/init.go @@ -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) diff --git a/src/conf/struct.go b/src/conf/struct.go index 15a4145..261a373 100644 --- a/src/conf/struct.go +++ b/src/conf/struct.go @@ -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 { diff --git a/src/fileaxe/action.go b/src/fileaxe/action.go index 751fca3..d166a5e 100644 --- a/src/fileaxe/action.go +++ b/src/fileaxe/action.go @@ -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 { @@ -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, }, ) @@ -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") } } diff --git a/src/fileaxe/compression.go b/src/fileaxe/compression.go index 7aa91a9..aaa50eb 100644 --- a/src/fileaxe/compression.go +++ b/src/fileaxe/compression.go @@ -3,7 +3,7 @@ package fileaxe import ( "context" "os" - "path" + "path/filepath" "strings" "time" @@ -11,15 +11,7 @@ import ( "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{ @@ -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} @@ -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, }, ) @@ -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) @@ -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", @@ -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 } diff --git a/src/fileaxe/fileops.go b/src/fileaxe/fileops.go index 3b8cd46..bc17e6f 100644 --- a/src/fileaxe/fileops.go +++ b/src/fileaxe/fileops.go @@ -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 { diff --git a/src/fileaxe/main.go b/src/fileaxe/main.go index 389c1dc..fd613c0 100644 --- a/src/fileaxe/main.go +++ b/src/fileaxe/main.go @@ -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) } } diff --git a/src/fileaxe/util.go b/src/fileaxe/util.go index 69c1227..3558bcf 100644 --- a/src/fileaxe/util.go +++ b/src/fileaxe/util.go @@ -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 }