aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMartin Polden <mpolden@mpolden.no>2021-09-04 11:01:40 +0200
committerMartin Polden <mpolden@mpolden.no>2021-09-04 11:15:42 +0200
commitc8d1eacc292579e6a4daf72c1c1dfcce69e2ff25 (patch)
tree926ce09d98309e72a8bdfca1bcdf5e31e9274b22
parent99546577c6094ddc9e407065ccaab06810537917 (diff)
parser: Move method
-rw-r--r--parser/parser.go19
-rw-r--r--queue/item.go27
2 files changed, 22 insertions, 24 deletions
diff --git a/parser/parser.go b/parser/parser.go
index e86f418..621992d 100644
--- a/parser/parser.go
+++ b/parser/parser.go
@@ -1,9 +1,14 @@
package parser
import (
+ "bytes"
"fmt"
+ "os"
+ "path/filepath"
"regexp"
"strconv"
+ "strings"
+ "text/template"
)
var (
@@ -41,6 +46,20 @@ func (m *Media) Equal(o Media) bool {
return m.Name == o.Name && m.Season == o.Season && m.Episode == o.Episode && m.Year == o.Year
}
+func (m *Media) PathIn(dir *template.Template) (string, error) {
+ var b bytes.Buffer
+ if err := dir.Execute(&b, m); err != nil {
+ return "", err
+ }
+ path := b.String()
+ // When path has a trailing slash, the actual destination path will be a directory inside LocalPath (same
+ // behaviour as rsync)
+ if strings.HasSuffix(path, string(os.PathSeparator)) {
+ path = filepath.Join(path, m.Release)
+ }
+ return path, nil
+}
+
func Default(s string) (Media, error) {
return Media{Release: s}, nil
}
diff --git a/queue/item.go b/queue/item.go
index 53a5f63..27d3821 100644
--- a/queue/item.go
+++ b/queue/item.go
@@ -1,14 +1,9 @@
package queue
import (
- "bytes"
- "os"
"path/filepath"
- "strings"
"time"
- "text/template"
-
"github.com/mpolden/lftpq/parser"
)
@@ -51,21 +46,6 @@ func (i *Item) setMedia(dirname string) error {
return nil
}
-func (i *Item) setLocalPath(t *template.Template) error {
- var b bytes.Buffer
- if err := t.Execute(&b, i.Media); err != nil {
- return err
- }
- path := b.String()
- // When path has a trailing slash, the actual destination path will be a directory inside LocalPath (same
- // behaviour as rsync)
- if strings.HasSuffix(path, string(os.PathSeparator)) {
- path = filepath.Join(path, filepath.Base(i.RemotePath))
- }
- i.LocalPath = path
- return nil
-}
-
func (i *Item) duplicates(readDir readDir) []Item {
var items []Item
parent := filepath.Join(i.LocalPath, "..")
@@ -97,8 +77,7 @@ func newItem(remotePath string, modTime time.Time, itemParser itemParser) (Item,
if err := item.setMedia(filepath.Base(remotePath)); err != nil {
return item, err
}
- if err := item.setLocalPath(itemParser.template); err != nil {
- return item, err
- }
- return item, nil
+ var err error
+ item.LocalPath, err = item.Media.PathIn(itemParser.template)
+ return item, err
}