Skip to content

Commit

Permalink
Add sort options
Browse files Browse the repository at this point in the history
  • Loading branch information
triole committed Jul 21, 2024
1 parent e2f71ad commit de57276
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 12 deletions.
2 changes: 2 additions & 0 deletions src/argparse.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ var CLI struct {
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"`
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 Down
3 changes: 3 additions & 0 deletions src/conf/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ func Init(cli interface{}, lg logseal.Logseal) (conf Conf) {
"can not parse max age arg",
logseal.F{"string": maxAgeArg, "error": err},
)
conf.SortBy = getcli(cli, "SortBy").(string)
conf.Order = getcli(cli, "Order").(string)

conf.DryRun = getcli(cli, "DryRun").(bool)
if conf.DryRun {
conf.MsgPrefix = "(dry run) "
Expand Down
2 changes: 2 additions & 0 deletions src/conf/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ type Conf struct {
Folder string
Matcher string
MaxAge time.Duration
SortBy string
Order string
DryRun bool
Ls tLs
Remove tRemove
Expand Down
36 changes: 24 additions & 12 deletions src/fileaxe/fileops.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ import (
)

type FileInfo struct {
Path string
IsDir bool
Size int64
SizeHR string
LastMod time.Time
Age time.Duration
Err error
Path string
IsDir bool
Size int64
SizeHR string
LastMod time.Time
Age time.Duration
SortIndex string
Err error
}

type FileInfos []FileInfo
Expand All @@ -29,21 +30,23 @@ func (arr FileInfos) Len() int {
}

func (arr FileInfos) Less(i, j int) bool {
si := makeSortIndex(arr[i])
sj := makeSortIndex(arr[j])
return si < sj
return arr[i].SortIndex < arr[j].SortIndex
}

func (arr FileInfos) Swap(i, j int) {
arr[i], arr[j] = arr[j], arr[i]
}

func makeSortIndex(fi FileInfo) (r string) {
func (fa FileAxe) makeSortIndexPath(fi FileInfo) (r string) {
r = fmt.Sprintf("%06d", len(strings.Split(fi.Path, string(os.PathSeparator))))
r += fi.Path
return
}

func (fa FileAxe) makeSortIndexAge(fi FileInfo) (r string) {
return fmt.Sprintf("%050d", fi.Age.Microseconds())
}

func (fa FileAxe) Find(basedir string, rxFilter string, maxAge time.Duration, refTime time.Time) (fileList FileInfos) {
fa.Lg.Debug("detect files", logseal.F{
"folder": basedir,
Expand All @@ -66,6 +69,10 @@ 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)
}
if maxAge == 0 {
fileList = append(fileList, fi)
} else {
Expand All @@ -91,7 +98,12 @@ func (fa FileAxe) Find(basedir string, rxFilter string, maxAge time.Duration, re
"error": err,
})
fa.Lg.Debug("found amount of files", logseal.F{"no": len(fileList)})
sort.Sort(FileInfos(fileList))

if fa.Conf.Order == "asc" {
sort.Sort(FileInfos(fileList))
} else {
sort.Sort(sort.Reverse(FileInfos(fileList)))
}
return
}

Expand Down

0 comments on commit de57276

Please sign in to comment.