mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 00:39:13 +00:00
fix:(actions): exec shell expansion (#295)
* chore: add package * feat(actions): properly parse exec args
This commit is contained in:
parent
4d753b76ed
commit
ffada19506
5 changed files with 161 additions and 10 deletions
|
@ -2,9 +2,10 @@ package action
|
|||
|
||||
import (
|
||||
"os/exec"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/mattn/go-shellwords"
|
||||
|
||||
"github.com/autobrr/autobrr/internal/domain"
|
||||
)
|
||||
|
||||
|
@ -18,18 +19,13 @@ func (s *service) execCmd(release domain.Release, action domain.Action) {
|
|||
return
|
||||
}
|
||||
|
||||
// handle args and replace vars
|
||||
m := NewMacro(release)
|
||||
|
||||
// parse and replace values in argument string before continuing
|
||||
parsedArgs, err := m.Parse(action.ExecArgs)
|
||||
args, err := s.parseExecArgs(release, action.ExecArgs)
|
||||
if err != nil {
|
||||
s.log.Error().Stack().Err(err).Msgf("exec failed, could not parse arguments: %v", action.ExecCmd)
|
||||
s.log.Error().Stack().Err(err).Msgf("parsing args failed: command: %v args: %v torrent: %v", cmd, action.ExecArgs, release.TorrentTmpFile)
|
||||
return
|
||||
}
|
||||
|
||||
// we need to split on space into a string slice, so we can spread the args into exec
|
||||
args := strings.Split(parsedArgs, " ")
|
||||
|
||||
start := time.Now()
|
||||
|
||||
|
@ -40,12 +36,33 @@ func (s *service) execCmd(release domain.Release, action domain.Action) {
|
|||
output, err := command.CombinedOutput()
|
||||
if err != nil {
|
||||
// everything other than exit 0 is considered an error
|
||||
s.log.Error().Stack().Err(err).Msgf("command: %v args: %v failed, torrent: %v", cmd, parsedArgs, release.TorrentTmpFile)
|
||||
s.log.Error().Stack().Err(err).Msgf("command: %v args: %v failed, torrent: %v", cmd, args, release.TorrentTmpFile)
|
||||
return
|
||||
}
|
||||
|
||||
s.log.Trace().Msgf("executed command: '%v'", string(output))
|
||||
|
||||
duration := time.Since(start)
|
||||
|
||||
s.log.Info().Msgf("executed command: '%v', args: '%v' %v,%v, total time %v", cmd, parsedArgs, release.TorrentName, release.Indexer, duration)
|
||||
s.log.Info().Msgf("executed command: '%v', args: '%v' %v,%v, total time %v", cmd, args, release.TorrentName, release.Indexer, duration)
|
||||
}
|
||||
|
||||
func (s *service) parseExecArgs(release domain.Release, execArgs string) ([]string, error) {
|
||||
// handle args and replace vars
|
||||
m := NewMacro(release)
|
||||
|
||||
// parse and replace values in argument string before continuing
|
||||
parsedArgs, err := m.Parse(execArgs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
p := shellwords.NewParser()
|
||||
p.ParseBacktick = true
|
||||
args, err := p.Parse(parsedArgs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return args, nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue