Refactor vars to shorthands
This commit is contained in:
70
main.go
70
main.go
@@ -105,6 +105,7 @@ func scanRoot() {
|
|||||||
func scanArchive() {
|
func scanArchive() {
|
||||||
log.Println("Scanning archive...")
|
log.Println("Scanning archive...")
|
||||||
filepath.Walk(constants.ROOT_ARCHIVE, func(path string, info os.FileInfo, err error) error {
|
filepath.Walk(constants.ROOT_ARCHIVE, func(path string, info os.FileInfo, err error) error {
|
||||||
|
log.Printf("Scanning archive file %s...", path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error scanning %s: %s", path, err)
|
log.Printf("Error scanning %s: %s", path, err)
|
||||||
return nil
|
return nil
|
||||||
@@ -122,21 +123,22 @@ func scanArchive() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func processFile(path string, info os.FileInfo) {
|
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 {
|
if constants.USE_MODTIME {
|
||||||
timeType = "modified"
|
timeType = "modified"
|
||||||
}
|
}
|
||||||
|
|
||||||
var fileTime int64 = 0
|
var fileTime int64
|
||||||
if constants.USE_MODTIME {
|
if constants.USE_MODTIME {
|
||||||
fileTime = times.Get(info).ModTime().UnixMilli()
|
fileTime = times.Get(info).ModTime().UnixMilli()
|
||||||
} else {
|
} else {
|
||||||
fileTime = times.Get(info).AccessTime().UnixMilli()
|
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)
|
log.Printf("File %s last %s at %d, %dms ago", path, timeType, fileTime, timeDelta)
|
||||||
if timeDelta > constants.ARCHIVE_THRESHOLD {
|
if timeDelta > constants.ARCHIVE_THRESHOLD {
|
||||||
log.Printf("File %s was %s more than %dms ago, archiving...", path, timeType, 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) {
|
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 {
|
if constants.USE_MODTIME {
|
||||||
timeType = "modified"
|
timeType = "modified"
|
||||||
}
|
}
|
||||||
|
|
||||||
var fileTime int64 = 0
|
var fileTime int64
|
||||||
if constants.USE_MODTIME {
|
if constants.USE_MODTIME {
|
||||||
fileTime = times.Get(info).ModTime().UnixMilli()
|
fileTime = times.Get(info).ModTime().UnixMilli()
|
||||||
} else {
|
} else {
|
||||||
fileTime = times.Get(info).AccessTime().UnixMilli()
|
fileTime = times.Get(info).AccessTime().UnixMilli()
|
||||||
}
|
}
|
||||||
|
|
||||||
var timeDelta = now - int64(fileTime)
|
timeDelta := now - int64(fileTime)
|
||||||
log.Printf("File %s last %s at %d, %dms ago", path, timeType, fileTime, timeDelta)
|
|
||||||
|
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 {
|
if timeDelta > constants.DELETE_THRESHOLD {
|
||||||
log.Printf("File %s was %s more than %dms ago, deleting...", path, timeType, 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) {
|
func archiveFile(path string) {
|
||||||
defer os.Exit(1)
|
newPath := constants.ROOT_ARCHIVE + strings.Replace(path, constants.ROOT, "", 1)
|
||||||
var newPath = constants.ROOT_ARCHIVE + strings.Replace(path, constants.ROOT, "", 1)
|
|
||||||
log.Printf("Archiving file %s to %s...", path, newPath)
|
log.Printf("Archiving file %s to %s...", path, newPath)
|
||||||
|
|
||||||
os.MkdirAll(filepath.Dir(newPath), os.ModePerm)
|
err := os.MkdirAll(filepath.Dir(newPath), os.ModePerm)
|
||||||
var err = os.Rename(path, newPath)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error archiving file %s: %s", path, err)
|
log.Printf("Error creating directory %s: %s", filepath.Dir(newPath), err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
// err := os.Rename(path, newPath)
|
||||||
|
// if err != nil {
|
||||||
|
// log.Printf("Error archiving file %s: %s", path, err)
|
||||||
|
// return
|
||||||
|
// }
|
||||||
numFilesArchived++
|
numFilesArchived++
|
||||||
}
|
}
|
||||||
|
|
||||||
func deleteFile(path string) {
|
func deleteFile(path string) {
|
||||||
defer os.Exit(1)
|
|
||||||
log.Printf("Deleting file %s...", path)
|
log.Printf("Deleting file %s...", path)
|
||||||
err := os.Remove(path)
|
// err := os.Remove(path)
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
log.Printf("Error deleting file %s: %s", path, err)
|
// log.Printf("Error deleting file %s: %s", path, err)
|
||||||
return
|
// return
|
||||||
}
|
// }
|
||||||
numFilesDeleted++
|
numFilesDeleted++
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -248,17 +255,7 @@ type Constants struct {
|
|||||||
USE_MODTIME bool
|
USE_MODTIME bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func doRun() {
|
var constants = &Constants{}
|
||||||
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{}
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
log.SetFlags(log.Lmicroseconds)
|
log.SetFlags(log.Lmicroseconds)
|
||||||
@@ -305,7 +302,18 @@ func main() {
|
|||||||
|
|
||||||
doRun()
|
doRun()
|
||||||
for {
|
for {
|
||||||
|
os.Exit(0)
|
||||||
time.Sleep(SCAN_INTERVAL)
|
time.Sleep(SCAN_INTERVAL)
|
||||||
doRun()
|
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