Break out all the executable-finding guff and add some tests
This commit is contained in:
parent
aa64e000ee
commit
73833a1a14
@ -283,14 +283,14 @@ func (dl *Download) Begin() {
|
|||||||
|
|
||||||
dl.Log = append(dl.Log, fmt.Sprintf("executing: %s with args: %s", dl.DownloadProfile.Command, strings.Join(cmdSlice, " ")))
|
dl.Log = append(dl.Log, fmt.Sprintf("executing: %s with args: %s", dl.DownloadProfile.Command, strings.Join(cmdSlice, " ")))
|
||||||
|
|
||||||
execAbsolutePath, err := filepath.Abs(dl.DownloadProfile.Command)
|
cmdPath, err := absPathToExecutable(dl.DownloadProfile.Command)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd := exec.Command(execAbsolutePath, cmdSlice...)
|
cmd := exec.Command(cmdPath, cmdSlice...)
|
||||||
cmd.Dir = dl.Config.Server.DownloadPath
|
cmd.Dir = dl.Config.Server.DownloadPath
|
||||||
log.Printf("Executing command: %v (executable: %s) in %s", cmd, execAbsolutePath, dl.Config.Server.DownloadPath)
|
log.Printf("Executing command executable: %s) in %s", cmdPath, dl.Config.Server.DownloadPath)
|
||||||
|
|
||||||
stdout, err := cmd.StdoutPipe()
|
stdout, err := cmd.StdoutPipe()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -371,7 +371,28 @@ func (dl *Download) Begin() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
dl.Lock.Unlock()
|
dl.Lock.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
func absPathToExecutable(cmd string) (string, error) {
|
||||||
|
|
||||||
|
pathCmd, err := exec.LookPath(cmd)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("could not LookPath '%s': %w", cmd, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
execAbsolutePath, err := filepath.Abs(pathCmd)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("could not get absolute path to '%s': %w", cmd, err)
|
||||||
|
}
|
||||||
|
fi, err := os.Stat(execAbsolutePath)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("could not get stat '%s': %w", cmd, err)
|
||||||
|
}
|
||||||
|
if !fi.Mode().IsRegular() {
|
||||||
|
return "", fmt.Errorf("'%s' is not a regular file: %w", cmd, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return execAbsolutePath, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// updateDownload updates the download based on data from the reader. Expects the
|
// updateDownload updates the download based on data from the reader. Expects the
|
||||||
|
@ -1,11 +1,13 @@
|
|||||||
package download
|
package download
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/tardisx/gropple/config"
|
"github.com/tardisx/gropple/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -360,3 +362,32 @@ Deleting original file The Greatest Shot In Television [2WoDQBhJCVQ].f140.m4a (p
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestLookForExecutable(t *testing.T) {
|
||||||
|
cmd := "sleep"
|
||||||
|
path, err := absPathToExecutable(cmd)
|
||||||
|
if assert.NoError(t, err) {
|
||||||
|
assert.Equal(t, path, "/bin/sleep")
|
||||||
|
}
|
||||||
|
cmd = "/bin/sleep"
|
||||||
|
path, err = absPathToExecutable(cmd)
|
||||||
|
if assert.NoError(t, err) {
|
||||||
|
assert.Equal(t, path, "/bin/sleep")
|
||||||
|
}
|
||||||
|
cmd = "../../../../../bin/sleep"
|
||||||
|
path, err = absPathToExecutable(cmd)
|
||||||
|
if assert.NoError(t, err) {
|
||||||
|
assert.Equal(t, path, "/bin/sleep")
|
||||||
|
}
|
||||||
|
cmd = "./sleep"
|
||||||
|
_, err = absPathToExecutable(cmd)
|
||||||
|
assert.Error(t, err)
|
||||||
|
|
||||||
|
os.Chdir("/bin")
|
||||||
|
cmd = "./sleep"
|
||||||
|
path, err = absPathToExecutable(cmd)
|
||||||
|
if assert.NoError(t, err) {
|
||||||
|
assert.Equal(t, path, "/bin/sleep")
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user