Skip to content

Commit

Permalink
feat(config): allow add files to exception in config file
Browse files Browse the repository at this point in the history
  • Loading branch information
Wittano committed Nov 21, 2023
1 parent 06f7304 commit eb08748
Show file tree
Hide file tree
Showing 13 changed files with 259 additions and 168 deletions.
4 changes: 2 additions & 2 deletions cmd/filebot/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ var (
)

func init() {
rootCmd.PersistentFlags().StringVarP(&setting.Flags.ConfigPath, "setting", "c", setting.GetDefaultConfigPath(), "Specific path for filebot configuration")
rootCmd.PersistentFlags().DurationVarP(&setting.Flags.UpdateInterval, "updateInterval", "u", setting.GetDefaultUpdateInterval(), "Set time after filebot should be refresh watched file state")
rootCmd.PersistentFlags().StringVarP(&setting.Flags.ConfigPath, "setting", "c", setting.DefaultConfigPath(), "Specific path for filebot configuration")
rootCmd.PersistentFlags().DurationVarP(&setting.Flags.UpdateInterval, "updateInterval", "u", setting.DefaultUpdateInterval(), "Set time after filebot should be refresh watched file state")
}
4 changes: 2 additions & 2 deletions cmd/filebot/filebot.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (
)

func runMainCommand(_ *cobra.Command, _ []string) {
conf := setting.Flags.GetConfig()
conf := setting.Flags.Config()

w := watcher.NewWatcher()
w.AddFilesToObservable(conf)
w.AddFilesToObservable(*conf)

s := cron.NewScheduler()
s.StartAsync()
Expand Down
15 changes: 13 additions & 2 deletions cron/trash.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"github.com/wittano/filebot/file"
"github.com/wittano/filebot/setting"
"golang.org/x/exp/slices"
"log"
"os"
"path/filepath"
Expand All @@ -14,7 +15,7 @@ import (
var TrashPath = filepath.Join(os.Getenv("HOME"), ".locale", "share", "Trash", "files")

func moveToTrashTask() {
c := setting.Flags.GetConfig()
c := setting.Flags.Config()

for _, dir := range c.Dirs {
if dir.MoveToTrash {
Expand All @@ -24,7 +25,17 @@ func moveToTrashTask() {
}

func moveFileToTrash(dir setting.Directory) {
for _, p := range dir.Src {
paths, err := dir.RealPaths()
if err != nil {
log.Printf("Failed to get file paths. %s", err)
return
}

for _, p := range paths {
if slices.Contains(dir.Exceptions, p) {
continue
}

if isAfterDateOfRemovingFile(p, dir.After) {
go file.MoveToDestination(TrashPath, p)
}
Expand Down
46 changes: 0 additions & 46 deletions internal/test/config.go

This file was deleted.

71 changes: 17 additions & 54 deletions path/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,16 @@ package path
import (
"os"
"path/filepath"
"regexp"
)

func GetPathsFromPattern(src string) ([]string, error) {
reg, err := getPathRegex(src)
reg, err := GetPathRegex(src)
if err != nil {
return nil, err
}

dirPath := filepath.Dir(src)

if f, _ := os.Stat(dirPath); f != nil && !f.IsDir() {
if reg.Match([]byte(f.Name())) {
return []string{f.Name()}, err
}
}

files, err := os.ReadDir(dirPath)
if err != nil {
return nil, err
Expand All @@ -28,7 +21,7 @@ func GetPathsFromPattern(src string) ([]string, error) {
paths := make([]string, 0, len(files))

for _, f := range files {
if !f.IsDir() && reg.Match([]byte(f.Name())) {
if !f.IsDir() && reg.MatchString(f.Name()) {
paths = append(paths, filepath.Join(dirPath, f.Name()))
}
}
Expand All @@ -51,29 +44,26 @@ func GetPathFromPatternRecursive(path string) ([]string, error) {
return GetPathsFromPattern(dir)
}

paths := make([]string, len(files))
var (
paths = make([]string, len(files))
size = uint(0)
)

size := uint(0)
for _, f := range files {
if f.IsDir() {
recPath, err := GetPathFromPatternRecursive(dir + f.Name())
if err != nil {
return nil, err
}
var path []string

paths = append(paths[0:size], recPath...)
size = uint(len(paths))
if f.IsDir() {
path, err = GetPathFromPatternRecursive(dir + f.Name())
} else {
path, err := GetPathsFromPattern(filepath.Join(dir, f.Name()))
if err != nil {
return nil, err
}

if len(path) > 0 {
paths = append(paths[0:size], path...)
size++
}
path, err = GetPathsFromPattern(filepath.Join(dir, f.Name()))
}

if err != nil {
return nil, err
}

paths = append(paths[0:size], path...)
size = uint(len(paths))
}

if size == 0 {
Expand All @@ -82,30 +72,3 @@ func GetPathFromPatternRecursive(path string) ([]string, error) {

return paths[0:size], nil
}

func getPathRegex(src string) (*regexp.Regexp, error) {
pattern := filepath.Base(src)

reg, err := regexp.Compile("^\\*")
if err != nil {
return nil, err
}

pattern = "^" + string(reg.ReplaceAll([]byte(pattern), []byte("\\w*"))) + "$"

return regexp.Compile(pattern)
}

func isFilePathIsRegex(reg string) bool {
specialChars := "*+?|[]{}()"

for _, specChar := range specialChars {
for _, char := range reg {
if char == specChar {
return true
}
}
}

return false
}
33 changes: 33 additions & 0 deletions path/regex.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package path

import (
"path/filepath"
"regexp"
)

func GetPathRegex(src string) (*regexp.Regexp, error) {
pattern := filepath.Base(src)

reg, err := regexp.Compile("\\*")
if err != nil {
return nil, err
}

pattern = "^" + string(reg.ReplaceAll([]byte(pattern), []byte("[\\w|\\W]*"))) + "$"

return regexp.Compile(pattern)
}

func isFilePathIsRegex(reg string) bool {
specialChars := "*+?|[]{}()"

for _, specChar := range specialChars {
for _, char := range reg {
if char == specChar {
return true
}
}
}

return false
}
35 changes: 35 additions & 0 deletions path/regex_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package path

import (
"testing"
)

type argument struct {
in string
out string
}

func TestGetPathRegex(t *testing.T) {
paths := []argument{
{"/path/to/example*", "example.txt"},
{"/path/to/example*", "example1234123412.txt"},
{"/path/to/example*", "example1$^&@1A.txt"},
{"/path/to/example.txt", "example.txt"},
{"/path/to/example", "example"},
{"/path/to/(example){2}", "exampleexample"},
{"/path/to/*.mp4", "example.mp4"},
}

for _, p := range paths {
t.Run(p.in, func(t *testing.T) {
res, err := GetPathRegex(p.in)
if err != nil {
t.Fatalf("Failed to extract regex from path. %s", err)
}

if !res.MatchString(p.out) {
t.Fatalf("Failed to match example.txt to regex")
}
})
}
}
67 changes: 59 additions & 8 deletions setting/config_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ package setting

import (
"github.com/pelletier/go-toml/v2"
"github.com/wittano/filebot/path"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
"log"
"os"
"path/filepath"
"regexp"
)

type Config struct {
Expand All @@ -16,6 +21,60 @@ type Directory struct {
Recursive bool
MoveToTrash bool
After uint
Exceptions []string
}

func (d Directory) RealPaths() (paths []string, err error) {
for _, exp := range d.Src {
if d.Recursive {
paths, err = path.GetPathFromPatternRecursive(exp)
} else {
paths, err = path.GetPathsFromPattern(exp)
}

if err != nil {
log.Printf("Failed get files from pattern '%s'\n", exp)
return
}

paths = append(paths, paths...)
}

if d.Exceptions != nil {
return d.filterRealPaths(paths), nil
}

return
}

func (d Directory) filterRealPaths(paths []string) (res []string) {
for _, p := range paths {
f, err := os.Stat(p)
if err != nil {
continue
}

if !f.IsDir() && slices.Contains(d.Exceptions, p) {
res = append(res, p)
continue

}

for _, exp := range d.Exceptions {
reg, err := regexp.Compile(exp)
if err != nil {
log.Printf("Failed to compile regex: '%s'", exp)
continue
}

filename := filepath.Base(p)
if exp != filename && !reg.MatchString(filename) {
res = append(res, p)
}
}
}

return
}

var config *Config
Expand All @@ -37,11 +96,3 @@ func load(path string) (*Config, error) {

return config, nil
}

func GetConfig(path string) (*Config, error) {
if config != nil {
return config, nil
}

return load(path)
}
2 changes: 1 addition & 1 deletion setting/config_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func TestGetDefaultConfigPath(t *testing.T) {
dir := GetDefaultConfigPath()
dir := DefaultConfigPath()

if dir != filepath.Join(os.Getenv("USERPROFILE"), ".setting\\filebot\\setting.toml") {
t.Fatalf("Invalid default setting path. Expected %s\\.setting\\filebot\\setting.toml, Acually: %s", os.Getenv("USERPROFILE"), dir)
Expand Down
Loading

0 comments on commit eb08748

Please sign in to comment.