mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 08:49:13 +00:00
feat(actions): simplify macro parsing (#560)
* refactor(action): parse macros * feat(action): add ctx to arr clients and test
This commit is contained in:
parent
f6e68fae2b
commit
839eb9f3f3
31 changed files with 323 additions and 334 deletions
|
@ -1,6 +1,9 @@
|
|||
package domain
|
||||
|
||||
import "context"
|
||||
import (
|
||||
"context"
|
||||
"github.com/autobrr/autobrr/pkg/errors"
|
||||
)
|
||||
|
||||
type ActionRepo interface {
|
||||
Store(ctx context.Context, action Action) (*Action, error)
|
||||
|
@ -46,6 +49,27 @@ type Action struct {
|
|||
Client DownloadClient `json:"client,omitempty"`
|
||||
}
|
||||
|
||||
// ParseMacros parse all macros on action
|
||||
func (a *Action) ParseMacros(release Release) error {
|
||||
var err error
|
||||
|
||||
m := NewMacro(release)
|
||||
|
||||
a.ExecArgs, err = m.Parse(a.ExecArgs)
|
||||
a.WatchFolder, err = m.Parse(a.WatchFolder)
|
||||
a.Category, err = m.Parse(a.Category)
|
||||
a.Tags, err = m.Parse(a.Tags)
|
||||
a.Label, err = m.Parse(a.Label)
|
||||
a.SavePath, err = m.Parse(a.SavePath)
|
||||
a.WebhookData, err = m.Parse(a.WebhookData)
|
||||
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "could not parse macros for action: %v", a.Name)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type ActionType string
|
||||
|
||||
const (
|
||||
|
|
|
@ -5,6 +5,8 @@ import (
|
|||
"fmt"
|
||||
"net/url"
|
||||
|
||||
"github.com/autobrr/autobrr/pkg/errors"
|
||||
|
||||
"github.com/autobrr/go-qbittorrent"
|
||||
)
|
||||
|
||||
|
@ -69,6 +71,18 @@ const (
|
|||
DownloadClientTypeReadarr DownloadClientType = "READARR"
|
||||
)
|
||||
|
||||
// Validate basic validation of client
|
||||
func (c DownloadClient) Validate() error {
|
||||
// basic validation of client
|
||||
if c.Host == "" {
|
||||
return errors.New("validation error: missing host")
|
||||
} else if c.Type == "" {
|
||||
return errors.New("validation error: missing type")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c DownloadClient) BuildLegacyHost() string {
|
||||
if c.Type == DownloadClientTypeQbittorrent {
|
||||
return c.qbitBuildLegacyHost()
|
||||
|
|
|
@ -10,54 +10,54 @@ import (
|
|||
)
|
||||
|
||||
type Macro struct {
|
||||
TorrentName string
|
||||
TorrentPathName string
|
||||
TorrentHash string
|
||||
TorrentUrl string
|
||||
TorrentDataRawBytes []byte
|
||||
Indexer string
|
||||
Title string
|
||||
Resolution string
|
||||
Source string
|
||||
HDR string
|
||||
FilterName string
|
||||
Size uint64
|
||||
Season int
|
||||
Episode int
|
||||
Year int
|
||||
CurrentYear int
|
||||
CurrentMonth int
|
||||
CurrentDay int
|
||||
CurrentHour int
|
||||
CurrentMinute int
|
||||
CurrentSecond int
|
||||
TorrentName string
|
||||
TorrentPathName string
|
||||
TorrentHash string
|
||||
TorrentUrl string
|
||||
TorrentDataRawBytes []byte
|
||||
Indexer string
|
||||
Title string
|
||||
Resolution string
|
||||
Source string
|
||||
HDR string
|
||||
FilterName string
|
||||
Size uint64
|
||||
Season int
|
||||
Episode int
|
||||
Year int
|
||||
CurrentYear int
|
||||
CurrentMonth int
|
||||
CurrentDay int
|
||||
CurrentHour int
|
||||
CurrentMinute int
|
||||
CurrentSecond int
|
||||
}
|
||||
|
||||
func NewMacro(release Release) Macro {
|
||||
currentTime := time.Now()
|
||||
|
||||
ma := Macro{
|
||||
TorrentName: release.TorrentName,
|
||||
TorrentUrl: release.TorrentURL,
|
||||
TorrentPathName: release.TorrentTmpFile,
|
||||
TorrentName: release.TorrentName,
|
||||
TorrentUrl: release.TorrentURL,
|
||||
TorrentPathName: release.TorrentTmpFile,
|
||||
TorrentDataRawBytes: release.TorrentDataRawBytes,
|
||||
TorrentHash: release.TorrentHash,
|
||||
Indexer: release.Indexer,
|
||||
Title: release.Title,
|
||||
Resolution: release.Resolution,
|
||||
Source: release.Source,
|
||||
HDR: strings.Join(release.HDR, ", "),
|
||||
FilterName: release.FilterName,
|
||||
Size: release.Size,
|
||||
Season: release.Season,
|
||||
Episode: release.Episode,
|
||||
Year: release.Year,
|
||||
CurrentYear: currentTime.Year(),
|
||||
CurrentMonth: int(currentTime.Month()),
|
||||
CurrentDay: currentTime.Day(),
|
||||
CurrentHour: currentTime.Hour(),
|
||||
CurrentMinute: currentTime.Minute(),
|
||||
CurrentSecond: currentTime.Second(),
|
||||
TorrentHash: release.TorrentHash,
|
||||
Indexer: release.Indexer,
|
||||
Title: release.Title,
|
||||
Resolution: release.Resolution,
|
||||
Source: release.Source,
|
||||
HDR: strings.Join(release.HDR, ", "),
|
||||
FilterName: release.FilterName,
|
||||
Size: release.Size,
|
||||
Season: release.Season,
|
||||
Episode: release.Episode,
|
||||
Year: release.Year,
|
||||
CurrentYear: currentTime.Year(),
|
||||
CurrentMonth: int(currentTime.Month()),
|
||||
CurrentDay: currentTime.Day(),
|
||||
CurrentHour: currentTime.Hour(),
|
||||
CurrentMinute: currentTime.Minute(),
|
||||
CurrentSecond: currentTime.Second(),
|
||||
}
|
||||
|
||||
return ma
|
||||
|
@ -65,6 +65,9 @@ func NewMacro(release Release) Macro {
|
|||
|
||||
// Parse takes a string and replaces valid vars
|
||||
func (m Macro) Parse(text string) (string, error) {
|
||||
if text == "" {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
// setup template
|
||||
tmpl, err := template.New("macro").Parse(text)
|
||||
|
@ -80,3 +83,24 @@ func (m Macro) Parse(text string) (string, error) {
|
|||
|
||||
return tpl.String(), nil
|
||||
}
|
||||
|
||||
// MustParse takes a string and replaces valid vars
|
||||
func (m Macro) MustParse(text string) string {
|
||||
if text == "" {
|
||||
return ""
|
||||
}
|
||||
|
||||
// setup template
|
||||
tmpl, err := template.New("macro").Parse(text)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
var tpl bytes.Buffer
|
||||
err = tmpl.Execute(&tpl, m)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
return tpl.String()
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue