Housekeeper/cmd/main.go

60 lines
946 B
Go
Raw Normal View History

2018-10-16 17:16:58 +00:00
package main
2018-10-16 20:06:46 +00:00
import (
"flag"
"fmt"
"log"
"os"
"path/filepath"
"time"
)
2018-10-17 17:21:16 +00:00
func isOlder(t time.Time, days int) bool {
2018-10-17 19:40:19 +00:00
return time.Now().Sub(t) > 1*time.Hour
2018-10-17 17:21:16 +00:00
}
2018-10-16 20:06:46 +00:00
2018-10-17 19:40:19 +00:00
func getFiles(location string, days int) ([]string, error) {
2018-10-16 20:06:46 +00:00
var files []string
err := filepath.Walk(location, func(path string, info os.FileInfo, err error) error {
files = append(files, path)
return nil
})
if err != nil {
log.Fatal(err)
}
2018-10-17 19:40:19 +00:00
test := make([]string, 0)
2018-10-16 20:06:46 +00:00
for _, file := range files {
2018-10-17 19:40:19 +00:00
at, err := os.Stat(file)
if err != nil {
log.Fatal(err)
}
if isOlder(at.ModTime(), days) {
test = append(test, file)
}
2018-10-16 20:06:46 +00:00
}
2018-10-16 17:16:58 +00:00
2018-10-17 19:40:19 +00:00
return test, err
2018-10-17 17:21:16 +00:00
}
func main() {
var location string
var days int
flag.StringVar(&location, "location", "", "location to scan")
flag.IntVar(&days, "days", 0, "days to poll")
flag.Parse()
2018-10-17 19:40:19 +00:00
files, err := getFiles(location, days)
if err != nil {
log.Fatal(err)
}
for _, file := range files {
fmt.Println(file)
}
2018-10-17 17:21:16 +00:00
2018-10-16 17:16:58 +00:00
}