Order output by mtime
parent
65c1f16e2c
commit
e70e608869
|
@ -0,0 +1,35 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/ollien/gobbler/categorize"
|
||||
"github.com/ollien/gobbler/categorize/match"
|
||||
"github.com/ollien/gobbler/categorize/tv"
|
||||
)
|
||||
|
||||
type fileLocation struct {
|
||||
filename string
|
||||
location tv.Location
|
||||
}
|
||||
|
||||
// performCategorization categorizes the given files through the given categorizer, converting them to a
|
||||
// slice of FileLocations, ordered as they were provided in sourceFolderCOntents.
|
||||
func performCategorization(categorizer categorize.Categorizer, sourceFolderContents []string) []fileLocation {
|
||||
locations := []fileLocation{}
|
||||
for _, entry := range sourceFolderContents {
|
||||
location, err := categorizer.CategorizeFile(entry)
|
||||
if errors.Is(err, match.ErrNoMatches) {
|
||||
continue
|
||||
} else if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Failed to categorize '%s':\n\t%s\n", entry, err)
|
||||
continue
|
||||
}
|
||||
|
||||
locations = append(locations, fileLocation{entry, location})
|
||||
}
|
||||
|
||||
return locations
|
||||
}
|
59
cmd/main.go
59
cmd/main.go
|
@ -1,21 +1,17 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/ollien/gobbler/categorize"
|
||||
"github.com/ollien/gobbler/categorize/match"
|
||||
"github.com/ollien/gobbler/categorize/tv"
|
||||
|
||||
"github.com/fatih/color"
|
||||
)
|
||||
|
||||
type fileLocationMap = map[string]tv.Location
|
||||
|
||||
func main() {
|
||||
if len(os.Args) != 3 {
|
||||
fmt.Fprintln(os.Stderr, "Usage: ./gobbler sourceFolder libraryFolder")
|
||||
|
@ -24,7 +20,16 @@ func main() {
|
|||
|
||||
sourceFolder, libraryFolder := os.Args[1], os.Args[2]
|
||||
|
||||
sourceFolderContents, err := getDirEntries(sourceFolder)
|
||||
sourceFolderContents, err := getDirEntries(sourceFolder, func(entries []os.FileInfo) {
|
||||
// Sort from newest to oldest
|
||||
sort.Slice(entries, func(i, j int) bool {
|
||||
iModTime := entries[i].ModTime()
|
||||
jModTime := entries[j].ModTime()
|
||||
|
||||
return !iModTime.Before(jModTime)
|
||||
})
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
@ -41,38 +46,36 @@ func main() {
|
|||
fmt.Fprintf(os.Stderr, "%d items not categorized\n", numSkipped)
|
||||
}
|
||||
|
||||
func getDirEntries(path string) ([]string, error) {
|
||||
func getDirEntries(path string, performSort func([]os.FileInfo)) ([]string, error) {
|
||||
fd, err := os.Open(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("failed to open dir: %w", err)
|
||||
}
|
||||
|
||||
defer fd.Close()
|
||||
|
||||
return fd.Readdirnames(-1)
|
||||
}
|
||||
|
||||
// performCategorization categorizes the given files through the given categorizer, converting them to a map of
|
||||
// filename to Locations
|
||||
func performCategorization(categorizer categorize.Categorizer, sourceFolderContents []string) fileLocationMap {
|
||||
locations := map[string]tv.Location{}
|
||||
for _, entry := range sourceFolderContents {
|
||||
location, err := categorizer.CategorizeFile(entry)
|
||||
if errors.Is(err, match.ErrNoMatches) {
|
||||
continue
|
||||
} else if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Failed to categorize '%s':\n\t%s\n", entry, err)
|
||||
continue
|
||||
}
|
||||
|
||||
locations[entry] = location
|
||||
entries, err := fd.Readdir(-1)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read dir contents: %w", err)
|
||||
}
|
||||
|
||||
return locations
|
||||
performSort(entries)
|
||||
|
||||
return getFileInfoNames(entries), nil
|
||||
}
|
||||
|
||||
func prettyPrintLocations(locations fileLocationMap) {
|
||||
for filename, location := range locations {
|
||||
func getFileInfoNames(fileInfos []os.FileInfo) []string {
|
||||
names := make([]string, len(fileInfos))
|
||||
for i, fileInfo := range fileInfos {
|
||||
names[i] = fileInfo.Name()
|
||||
}
|
||||
|
||||
return names
|
||||
}
|
||||
|
||||
func prettyPrintLocations(fileLocations []fileLocation) {
|
||||
for _, fl := range fileLocations {
|
||||
filename, location := fl.filename, fl.location
|
||||
color.New(color.FgGreen, color.Bold).Println(filename)
|
||||
seasonString := "Season"
|
||||
if len(location.Seasons()) > 1 {
|
||||
|
|
Loading…
Reference in New Issue