From 6b63c3c020747057b99e6c4756e92e9a1361912a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Majdand=C5=BEi=C4=87?= Date: Sat, 22 Jun 2024 13:59:53 +0200 Subject: [PATCH] Refactor vars to shorthands --- main.go | 70 ++++++++++++++++++++++++++++++++------------------------- 1 file changed, 39 insertions(+), 31 deletions(-) diff --git a/main.go b/main.go index 9d8a290..63245b1 100644 --- a/main.go +++ b/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 +} \ No newline at end of file