-
Notifications
You must be signed in to change notification settings - Fork 30
/
main.go
83 lines (74 loc) · 1.78 KB
/
main.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
package main
import (
"context"
"errors"
"flag"
"fmt"
"os"
"os/exec"
"time"
"github.com/zyxar/argo/rpc"
)
var (
rpcc rpc.Client
rpcSecret string
rpcURI string
launchLocal bool
errParameter = errors.New("invalid parameter")
errNotSupportedCmd = errors.New("not supported command")
errInvalidCmd = errors.New("invalid command")
)
func init() {
flag.StringVar(&rpcSecret, "secret", "", "set --rpc-secret for aria2c")
flag.StringVar(&rpcURI, "uri", "http://localhost:6800/jsonrpc", "set rpc address")
flag.BoolVar(&launchLocal, "launch", false, "launch local aria2c daemon")
}
func main() {
flag.Parse()
if launchLocal {
if err := LaunchAria2cDaemon(rpcSecret); err != nil {
fmt.Fprintf(os.Stderr, "launch: %v", err)
os.Exit(1)
}
return
}
if flag.NArg() == 0 {
fmt.Fprintf(os.Stderr, "usage: argo {CMD} {PARAMETERS}...\n")
flag.PrintDefaults()
fmt.Fprintln(os.Stderr)
c := make([]string, 0, len(cmds))
for cmd := range cmds {
c = append(c, cmd)
}
renderCmdList(os.Stderr, c...)
os.Exit(1)
}
var err error
rpcc, err = rpc.New(context.Background(), rpcURI, rpcSecret, time.Second, nil)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(2)
}
defer rpcc.Close()
args := flag.Args()
if cmd, ok := cmds[args[0]]; ok {
err = cmd(args[1:]...)
} else {
err = errInvalidCmd
}
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
}
// LaunchAria2cDaemon launchs aria2 daemon to listen for RPC calls, locally.
func LaunchAria2cDaemon(secret string) (err error) {
args := []string{"--enable-rpc", "--rpc-listen-all"}
if secret != "" {
args = append(args, "--rpc-secret="+secret)
}
cmd := exec.Command("aria2c", args...)
if err = cmd.Start(); err != nil {
return
}
return cmd.Process.Release()
}