-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary.go
114 lines (95 loc) · 1.94 KB
/
binary.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package qbittorrent
import (
"errors"
"github.com/5rahim/go-qbit/util"
"os/exec"
"runtime"
"strings"
"time"
)
func (c *Client) getBinaryName() string {
switch runtime.GOOS {
case "windows":
return "qbittorrent.exe"
default:
return "qbittorrent"
}
}
func (c *Client) getBinaryPath() string {
if len(c.BinaryPath) > 0 {
return c.BinaryPath
}
switch runtime.GOOS {
case "windows":
return "C:/Program Files/qBittorrent/qbittorrent.exe"
case "linux":
return "/usr/bin/qbittorrent"
case "darwin":
return "/Applications/Client.app/Contents/MacOS/qBittorrent"
default:
return ""
}
}
func (c *Client) Start() error {
// If the path is empty, do not check if qBittorrent is running
if c.BinaryPath == "" {
return nil
}
name := c.getBinaryName()
if ProgramIsRunning(name) {
return nil
}
path := c.getBinaryPath()
if path == "" {
return errors.New("failed to get path to qBittorrent binary")
}
cmd := qbittorrent_util.NewCmd(path)
err := cmd.Start()
if err != nil {
return errors.New("failed to start qBittorrent")
}
time.Sleep(1 * time.Second)
return nil
}
func (c *Client) CheckStart() bool {
if c == nil {
return false
}
// If the path is empty, assume it's running
if c.BinaryPath == "" {
return true
}
_, err := c.Application.GetAppVersion()
if err == nil {
return true
}
err = c.Start()
timeout := time.After(30 * time.Second)
ticker := time.Tick(1 * time.Second)
for {
select {
case <-ticker:
_, err = c.Application.GetAppVersion()
if err == nil {
return true
}
case <-timeout:
return false
}
}
}
func ProgramIsRunning(name string) bool {
var cmd *exec.Cmd
switch runtime.GOOS {
case "windows":
cmd = qbittorrent_util.NewCmd("tasklist")
case "linux":
cmd = qbittorrent_util.NewCmd("pgrep", name)
case "darwin":
cmd = qbittorrent_util.NewCmd("pgrep", name)
default:
return true
}
output, _ := cmd.Output()
return strings.Contains(string(output), name)
}