Skip to content

Commit

Permalink
Add removal function
Browse files Browse the repository at this point in the history
  • Loading branch information
triole committed Jul 17, 2024
1 parent 7c63238 commit aa5ab62
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 19 deletions.
5 changes: 5 additions & 0 deletions sh/testrun.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash
scriptdir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
basedir=$(echo "${scriptdir%/*}")

r "${basedir}/testdata/tmp" -r ".*" --remove -m 1m
3 changes: 2 additions & 1 deletion src/argparse.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ var CLI struct {
Matcher string `help:"regex matcher for file detection" short:"r" default:"\.log$"`
MaxAge string `help:"remove compressed log files older than x, default keeps all, use with duration like i.e. 90m, 12h, 4d, 2w" short:"m" default:"0"`
Format string `help:"compressed target archive format" short:"f" default:"gz" enum:"snappy,gz,xz"`
Remove bool `help:"remove matching files instead of compressing them" `
Remove bool `help:"remove matching files instead of compressing them"`
Yes bool `help:"assume yes on remove affirmation query"`
LogFile string `help:"log file" short:"l" 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 Down
5 changes: 4 additions & 1 deletion src/conf/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Conf struct {
TargetFormat string
MaxAge tMaxAge
Remove bool
Yes bool
SkipTruncate bool
DryRun bool
}
Expand All @@ -24,7 +25,7 @@ type tMaxAge struct {
Duration time.Duration
}

func Init(fol, mat, frm, mxa string, rm, skt, dry bool, lg logseal.Logseal) (conf Conf) {
func Init(fol, mat, frm, mxa string, rm, yes, skt, dry bool, lg logseal.Logseal) (conf Conf) {
conf.Now = time.Now()

abs, err := filepath.Abs(fol)
Expand Down Expand Up @@ -52,6 +53,7 @@ func Init(fol, mat, frm, mxa string, rm, skt, dry bool, lg logseal.Logseal) (con
}

conf.Remove = rm
conf.Yes = rm
conf.SkipTruncate = skt
conf.DryRun = dry
return
Expand All @@ -67,6 +69,7 @@ func InitTestConf(fol string, remove bool) (conf Conf) {
remove,
false,
false,
false,
lg,
)
return
Expand Down
19 changes: 10 additions & 9 deletions src/logaxe/action.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package logaxe

import (
"fmt"

"github.com/triole/logseal"
)

Expand Down Expand Up @@ -33,12 +31,6 @@ func (la LogAxe) compress() {
la.Conf.MaxAge.Duration, la.Conf.Now,
)
for _, fil := range compressedLogs {
la.Lg.Info(
"remove file", logseal.F{
"path": fil.Path,
"age": fil.Age,
},
)
if !la.Conf.DryRun {
la.rm(fil.Path)
}
Expand All @@ -54,7 +46,16 @@ func (la LogAxe) remove() {
la.Conf.MaxAge.Duration, la.Conf.Now,
)
for _, fil := range files {
fmt.Printf("%+v\n", fil)
if !la.Conf.DryRun {
if askForConfirmation(fil.Path) {
la.rm(fil.Path)
}
} else {
la.Lg.Info(
"dry run, might have removed file",
logseal.F{"path": fil.Path},
)
}
}
} else {
la.Lg.Info("nothing to do, remove mode requires a max age definition, use --max-age or -m")
Expand Down
40 changes: 40 additions & 0 deletions src/logaxe/confirmation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package logaxe

import (
"fmt"
"log"
)

func askForConfirmation(filename string) bool {
okayResponses := []string{"y", "Y", "yes", "Yes", "YES"}
nokayResponses := []string{"n", "N", "no", "No", "NO"}
fmt.Printf(
"Type %s to confirm deletion of %q ",
okayResponses, filename,
)
var response string
_, err := fmt.Scanln(&response)
if err != nil {
log.Fatal(err)
}
if containsString(okayResponses, response) {
return true
} else if containsString(nokayResponses, response) {
return false
} else {
return askForConfirmation(filename)
}
}

func posString(slice []string, element string) int {
for index, elem := range slice {
if elem == element {
return index
}
}
return -1
}

func containsString(slice []string, element string) bool {
return !(posString(slice, element) == -1)
}
20 changes: 15 additions & 5 deletions src/logaxe/fileops.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,19 @@ func (la LogAxe) truncate(filename string) error {
}

func (la LogAxe) rm(filepath string) {
err := os.Remove(filepath)
la.Lg.IfErrError(
"can not delete file",
logseal.F{"path": filepath, "error": err},
)
if la.Conf.DryRun {
la.Lg.Info(
"dry run, would have removed file",
logseal.F{"path": filepath},
)
} else {
err := os.Remove(filepath)
if err == nil {
la.Lg.Info("file removed", logseal.F{"path": filepath})
}
la.Lg.IfErrError(
"can not delete file",
logseal.F{"path": filepath, "error": err},
)
}
}
5 changes: 4 additions & 1 deletion src/logaxe/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import (
)

func (la LogAxe) Run() {
la.Lg.Info("Start logaxe", logseal.F{"conf": fmt.Sprintf("%+v", la)})
la.Lg.Info(
"Start logaxe",
logseal.F{"conf": fmt.Sprintf("%+v", la.Conf)},
)

if la.Conf.DryRun {
la.Lg.Info(" --- DRY RUN START ---")
Expand Down
1 change: 1 addition & 0 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func main() {
CLI.Format,
CLI.MaxAge,
CLI.Remove,
CLI.Yes,
CLI.SkipTruncate,
CLI.DryRun,
lg,
Expand Down
4 changes: 2 additions & 2 deletions src/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (

func TestMainProcessor(t *testing.T) {
fol := "../testdata/tmp"
generateTestLogFiles(fol, 0, 9)
generateTestLogFiles(fol, 9)

conf := conf.InitTestConf(fol, false)
lg = logseal.Init("info")
Expand All @@ -47,7 +47,7 @@ func verifyFiles(files logaxe.FileInfos, hash string, amount int, t *testing.T)
}
}

func generateTestLogFiles(fol string, i, j int) {
func generateTestLogFiles(fol string, j int) {
os.MkdirAll(fol, 0755)
for i := 1; i <= j; i++ {
createFile(fmt.Sprintf(path.Join(fol, "log%v.log"), i))
Expand Down

0 comments on commit aa5ab62

Please sign in to comment.