Skip to content

Commit

Permalink
Refactor cameraSort
Browse files Browse the repository at this point in the history
  • Loading branch information
Miguel-Dorta committed Nov 18, 2019
1 parent dd1f43e commit d55f763
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 122 deletions.
58 changes: 0 additions & 58 deletions cmd/cameraSort/cameraSort.go

This file was deleted.

95 changes: 95 additions & 0 deletions cmd/cameraSort/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package main

import (
"errors"
"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"
)

var (
from, to string
log *logolang.Logger
)

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

var verbose, version bool
flag.StringVar(&from, "from", "", "Path to read the files")
flag.StringVar(&to, "to", "", "Path to put the files")
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
}

if from == "" || to == "" {
flag.PrintDefaults()
os.Exit(1)
}
}

// TODO rewrite tests
func main() {
errFound := false
if err := utils.IterateDir(from, func(f os.FileInfo) {
fPath := filepath.Join(from, f.Name())

// Omit if it's not a file
if !f.Mode().IsRegular() {
return
}

camID, year, month, day, err := getInfoFromFilename(f.Name())
if err != nil {
log.Errorf("cannot get info from file \"%s\": %s", fPath, err)
errFound = true
return
}

destination := filepath.Join(to, camID, year, month, day, f.Name())
log.Infof("moving file from \"%s\" to \"%s\"", fPath, destination)
if err = utils.Move(fPath, destination); err != nil {
log.Errorf("error moving file from \"%s\" to \"%s\": %s", fPath, destination, err)
errFound = true
return
}
}); err != nil {
log.Errorf("error listing path \"%s\": %s", from, err)
errFound = true
}

if errFound {
os.Exit(1)
}
}

// Gets CamID, Year, Month and Day from a string like CMIDyyyyMMdd
func getInfoFromFilename(filename string) (camID, year, month, day string, err error) {
if len(filename) < 12 {
return "", "", "", "", errors.New("incorrect format: too short")
}

for _, b := range filename[4:12] {
if b < '0' || b > '9' {
return "", "", "", "", errors.New("incorrect format: cannot parse date")
}
}

return filename[:4], filename[4:8], filename[8:10], filename[10:12], nil
}
File renamed without changes.
20 changes: 0 additions & 20 deletions pkg/cameras/files.go

This file was deleted.

44 changes: 0 additions & 44 deletions pkg/utils/dirs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,50 +7,6 @@ import (
"os"
)

// ForEachInDirectory executes the function provided for each os.FileInfo found in the directory of the path provided.
// If the function returns an error, the execution will not stop and will be collected in an error slice.
func ForEachInDirectory(path string, fn func(fi os.FileInfo) error) []error {
// Check if path exists, is readable, and is a directory
fStat, err := os.Stat(path)
if err != nil {
return []error{err}
}
if !fStat.IsDir() {
return []error{fmt.Errorf("%s is not a directory", path)}
}

// Open directory for reading
f, err := os.Open(path)
if err != nil {
return []error{fmt.Errorf("error opening directory \"%s\": %s", path, err)}
}
defer f.Close()

errList := make([]error, 0, 50)
for {
// List 1000 children each time.
list, err := f.Readdir(1000)
if err != nil {
if err != io.EOF {
return []error{fmt.Errorf("error listing directory \"%s\": %s", path, err)}
}
break
}

// Apply fn(os.FileInfo) for each child
for _, fi := range list {
if err = fn(fi); err != nil {
errList = append(errList, err)
}
}
}

if len(errList) != 0 {
return errList
}
return nil
}

// IterateDir iterates the dir provided applying the function provided to every file found.
func IterateDir(path string, fn func(f os.FileInfo)) error {
// Check if path exists, is readable, and is a directory
Expand Down

0 comments on commit d55f763

Please sign in to comment.