Refactor vars to shorthands
This commit is contained in:
70
main.go
70
main.go
@@ -105,6 +105,7 @@ func scanRoot() {
|
||||
func scanArchive() {
|
||||
log.Println("Scanning archive...")
|
||||
filepath.Walk(constants.ROOT_ARCHIVE, func(path string, info os.FileInfo, err error) error {
|
||||
log.Printf("Scanning archive file %s...", path)
|
||||
if err != nil {
|
||||
log.Printf("Error scanning %s: %s", path, err)
|
||||
return nil
|
||||
@@ -122,21 +123,22 @@ func scanArchive() {
|
||||
}
|
||||
|
||||
func processFile(path string, info os.FileInfo) {
|
||||
var now = time.Now().UnixMilli()
|
||||
now := time.Now().UnixMilli()
|
||||
log.Printf("Processing file %s...", path)
|
||||
|
||||
var timeType = "accessed"
|
||||
timeType := "accessed"
|
||||
if constants.USE_MODTIME {
|
||||
timeType = "modified"
|
||||
}
|
||||
|
||||
var fileTime int64 = 0
|
||||
var fileTime int64
|
||||
if constants.USE_MODTIME {
|
||||
fileTime = times.Get(info).ModTime().UnixMilli()
|
||||
} else {
|
||||
fileTime = times.Get(info).AccessTime().UnixMilli()
|
||||
}
|
||||
|
||||
var timeDelta = now - fileTime
|
||||
timeDelta := now - fileTime
|
||||
log.Printf("File %s last %s at %d, %dms ago", path, timeType, fileTime, timeDelta)
|
||||
if timeDelta > constants.ARCHIVE_THRESHOLD {
|
||||
log.Printf("File %s was %s more than %dms ago, archiving...", path, timeType, constants.ARCHIVE_THRESHOLD)
|
||||
@@ -145,22 +147,25 @@ func processFile(path string, info os.FileInfo) {
|
||||
}
|
||||
|
||||
func processArchiveFile(path string, info os.FileInfo) {
|
||||
var now = time.Now().UnixMilli()
|
||||
now := time.Now().UnixMilli()
|
||||
|
||||
var timeType = "accessed"
|
||||
timeType := "accessed"
|
||||
if constants.USE_MODTIME {
|
||||
timeType = "modified"
|
||||
}
|
||||
|
||||
var fileTime int64 = 0
|
||||
var fileTime int64
|
||||
if constants.USE_MODTIME {
|
||||
fileTime = times.Get(info).ModTime().UnixMilli()
|
||||
} else {
|
||||
fileTime = times.Get(info).AccessTime().UnixMilli()
|
||||
}
|
||||
|
||||
var timeDelta = now - int64(fileTime)
|
||||
log.Printf("File %s last %s at %d, %dms ago", path, timeType, fileTime, timeDelta)
|
||||
timeDelta := now - int64(fileTime)
|
||||
|
||||
fileTimeFormatted := time.UnixMilli(fileTime).Format("15:04:05.000000")
|
||||
timeDeltaFormatted := time.Duration(timeDelta) * time.Millisecond
|
||||
log.Printf("File %s last %s at %s, %s ago", path, timeType, fileTimeFormatted, timeDeltaFormatted)
|
||||
|
||||
if timeDelta > constants.DELETE_THRESHOLD {
|
||||
log.Printf("File %s was %s more than %dms ago, deleting...", path, timeType, constants.DELETE_THRESHOLD)
|
||||
@@ -169,27 +174,29 @@ func processArchiveFile(path string, info os.FileInfo) {
|
||||
}
|
||||
|
||||
func archiveFile(path string) {
|
||||
defer os.Exit(1)
|
||||
var newPath = constants.ROOT_ARCHIVE + strings.Replace(path, constants.ROOT, "", 1)
|
||||
newPath := constants.ROOT_ARCHIVE + strings.Replace(path, constants.ROOT, "", 1)
|
||||
log.Printf("Archiving file %s to %s...", path, newPath)
|
||||
|
||||
os.MkdirAll(filepath.Dir(newPath), os.ModePerm)
|
||||
var err = os.Rename(path, newPath)
|
||||
err := os.MkdirAll(filepath.Dir(newPath), os.ModePerm)
|
||||
if err != nil {
|
||||
log.Printf("Error archiving file %s: %s", path, err)
|
||||
log.Printf("Error creating directory %s: %s", filepath.Dir(newPath), err)
|
||||
return
|
||||
}
|
||||
// err := os.Rename(path, newPath)
|
||||
// if err != nil {
|
||||
// log.Printf("Error archiving file %s: %s", path, err)
|
||||
// return
|
||||
// }
|
||||
numFilesArchived++
|
||||
}
|
||||
|
||||
func deleteFile(path string) {
|
||||
defer os.Exit(1)
|
||||
log.Printf("Deleting file %s...", path)
|
||||
err := os.Remove(path)
|
||||
if err != nil {
|
||||
log.Printf("Error deleting file %s: %s", path, err)
|
||||
return
|
||||
}
|
||||
// err := os.Remove(path)
|
||||
// if err != nil {
|
||||
// log.Printf("Error deleting file %s: %s", path, err)
|
||||
// return
|
||||
// }
|
||||
numFilesDeleted++
|
||||
}
|
||||
|
||||
@@ -248,17 +255,7 @@ type Constants struct {
|
||||
USE_MODTIME bool
|
||||
}
|
||||
|
||||
func doRun() {
|
||||
log.Printf("Running at %s", time.Now().Format("15:04:05"))
|
||||
scanRoot()
|
||||
scanArchive()
|
||||
cleanRoot()
|
||||
log.Printf("Archived %d files, deleted %d files", numFilesArchived, numFilesDeleted)
|
||||
numFilesArchived = 0
|
||||
numFilesDeleted = 0
|
||||
}
|
||||
|
||||
var constants = Constants{}
|
||||
var constants = &Constants{}
|
||||
|
||||
func main() {
|
||||
log.SetFlags(log.Lmicroseconds)
|
||||
@@ -305,7 +302,18 @@ func main() {
|
||||
|
||||
doRun()
|
||||
for {
|
||||
os.Exit(0)
|
||||
time.Sleep(SCAN_INTERVAL)
|
||||
doRun()
|
||||
}
|
||||
}
|
||||
|
||||
func doRun() {
|
||||
log.Printf("Running at %s", time.Now().Format("15:04:05"))
|
||||
scanRoot()
|
||||
scanArchive()
|
||||
cleanRoot()
|
||||
log.Printf("Archived %d files, deleted %d files", numFilesArchived, numFilesDeleted)
|
||||
numFilesArchived = 0
|
||||
numFilesDeleted = 0
|
||||
}
|
Reference in New Issue
Block a user