-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand.go
74 lines (63 loc) · 1.56 KB
/
command.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
package audacity
import (
"encoding/json"
"fmt"
"log"
"strings"
)
// Command sends any command and returns the response.
// The number of arguments must be even, in the form of key and value pairs.
// Use this function if your command is not otherwise implemented.
// See the scripting reference here: https://manual.audacityteam.org/man/scripting_reference.html
func Command(cmd string, args ...string) (resp Response) {
if k := len(args); k > 0 {
if !strings.HasSuffix(cmd, ":") {
cmd += ":"
}
if k%2 != 0 {
log.Fatal("[BUG] can't process command with odd number of arguments")
}
for i := 0; i < k; i = +2 {
cmd += fmt.Sprintf(` %s="%s"`, args[i], args[i+1])
}
}
// Send a single command.
sendCmd(cmd)
// decode response
json.NewDecoder(fromPipe).Decode(&resp)
fmt.Printf("Command: '%s'\nResponse: %s\n\n", cmd, resp)
return resp
}
func sendCmd(cmd string) {
if toPipe == nil {
if err := initPipes(); err != nil {
log.Printf("can't run audacity command %s: %s", cmd, err)
return
}
}
toPipe.Write([]byte(cmd + eol))
}
func command_debug(cmd string) {
// Send a single command.
sendCmd(cmd)
// show message
buf := make([]byte, 15)
fromPipe.Read(buf)
fmt.Printf("Debugging Command: '%s'\nResponse: %s\n\n", cmd, buf)
// // Return the command resp.
// str := ""
// buf = make([]byte, 1)
// done := -1
// for done < 1 {
// // BatchCommand finished: OK
// fromPipe.Read(buf)
// switch buf[0] {
// case '\n':
// done++
// default:
// done = -1
// }
// str += string(buf)
// fmt.Println("MORE:", str)
// }
}