Skip to content

Commit

Permalink
Refactor rmOldCamera
Browse files Browse the repository at this point in the history
  • Loading branch information
Miguel-Dorta committed Nov 18, 2019
1 parent e2fadf1 commit 70a9a62
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 119 deletions.
102 changes: 102 additions & 0 deletions cmd/generic_rmOldCameraData/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package main

import (
"flag"
"fmt"
"github.com/Miguel-Dorta/logolang"
"github.com/Miguel-Dorta/surveillance-cameras/internal"
"github.com/Miguel-Dorta/surveillance-cameras/pkg/utils"
"os"
"path/filepath"
"strconv"
"time"
)

var (
log *logolang.Logger
path string
oldestPreserve time.Time
)

func init() {
log = logolang.NewLogger()
log.Level = logolang.LevelError

var days int
var verbose, version bool
flag.StringVar(&path, "path", ".", "Path for removing old camera data")
flag.IntVar(&days, "days", 30, "Preserve camera data more recent than ~ days")
flag.BoolVar(&verbose, "verbose", false, "Verbose output")
flag.BoolVar(&verbose, "v", false, "Verbose output")
flag.BoolVar(&version, "version", false, "Print version and exit")
flag.BoolVar(&version, "V", false, "Print version and exit")
flag.Parse()

if version {
fmt.Println(internal.Version)
os.Exit(0)
}

if verbose {
log.Level = logolang.LevelInfo
}

oldestPreserve = time.Now().AddDate(0, 0, days * -1)
}

// TODO rewrite tests
func main() {
if err := utils.IterateDir(path, func(f os.FileInfo) {
if !f.IsDir() {
return
}
iterateYears(filepath.Join(path, f.Name()))
}); err != nil {
log.Errorf("error listing path \"%s\": %s", path, err)
}
}

func iterateYears(path string) {
iterateDate(path, oldestPreserve.Year(), iterateMonths)
}

func iterateMonths(path string) {
iterateDate(path, int(oldestPreserve.Month()), iterateDays)
}

func iterateDays(path string) {
iterateDate(path, oldestPreserve.Day(), func(_ string) {})
}

func iterateDate(path string, compareTo int, doWithCoincidence func(path string)) {
if err := utils.IterateDir(path, func(f os.FileInfo) {
fPath := filepath.Join(path, f.Name())

// Parse comparable
comparable, err := strconv.Atoi(f.Name())
if err != nil {
log.Errorf("error parsing date from path \"%s\": %s", fPath, err)
return
}

// Remove if it's an older comparable
if comparable < compareTo {
log.Infof("removing %s", fPath)
if err := os.RemoveAll(fPath); err != nil {
log.Errorf("error removing path \"%s\": %s", fPath, err)
}
return
}

// Do action if it's the same
if comparable == compareTo {
doWithCoincidence(fPath)
return
}

// Skip if it's a future comparable

}); err != nil {
log.Errorf("error listing path \"%s\": %s", path, err)
}
}
File renamed without changes.
119 changes: 0 additions & 119 deletions cmd/rmOldCamera/rmOldCamera.go

This file was deleted.

0 comments on commit 70a9a62

Please sign in to comment.