autobrr/internal/action/exec.go
ze0s ca196f0bf1
feat(releases): support magnet links (#730)
* feat(releases): support magnet links

* feat(feeds): support magnet links

* feat(actions): log messages

* fix: component warning

* fix: check hasprefix instead of hassuffix for magnet

* feat(release): resolve magnet uri from link

* fix(actions): deluge use magnet uri

* fix(macros): add `MagnetURI` var

* fix(actions): run magnet resolving before macros

* feat(feeds): set download type on creation
2023-02-28 22:16:10 +01:00

69 lines
1.9 KiB
Go

package action
import (
"context"
"os"
"os/exec"
"strings"
"time"
"github.com/autobrr/autobrr/internal/domain"
"github.com/autobrr/autobrr/pkg/errors"
"github.com/mattn/go-shellwords"
)
func (s *service) execCmd(ctx context.Context, action *domain.Action, release domain.Release) error {
s.log.Debug().Msgf("action exec: %s release: %s", action.Name, release.TorrentName)
if release.TorrentTmpFile == "" && strings.Contains(action.ExecArgs, "TorrentPathName") {
if err := release.DownloadTorrentFileCtx(ctx); err != nil {
return errors.Wrap(err, "error downloading torrent file for release: %s", release.TorrentName)
}
}
// read the file into bytes we can then use in the macro
if len(release.TorrentDataRawBytes) == 0 && release.TorrentTmpFile != "" {
t, err := os.ReadFile(release.TorrentTmpFile)
if err != nil {
return errors.Wrap(err, "could not read torrent file: %s", release.TorrentTmpFile)
}
release.TorrentDataRawBytes = t
}
// check if program exists
cmd, err := exec.LookPath(action.ExecCmd)
if err != nil {
return errors.Wrap(err, "exec failed, could not find program: %s", action.ExecCmd)
}
p := shellwords.NewParser()
p.ParseBacktick = true
args, err := p.Parse(action.ExecArgs)
if err != nil {
return errors.Wrap(err, "could not parse exec args: %s", action.ExecArgs)
}
// we need to split on space into a string slice, so we can spread the args into exec
start := time.Now()
// setup command and args
command := exec.CommandContext(ctx, cmd, args...)
// execute command
output, err := command.CombinedOutput()
if err != nil {
// everything other than exit 0 is considered an error
return errors.Wrap(err, "error executing command: %s args: %s", cmd, args)
}
s.log.Trace().Msgf("executed command: '%s'", string(output))
duration := time.Since(start)
s.log.Info().Msgf("executed command: '%s', args: '%s' %s,%s, total time %v", cmd, args, release.TorrentName, release.Indexer, duration)
return nil
}