-
Notifications
You must be signed in to change notification settings - Fork 0
/
jmctl.go
214 lines (176 loc) · 6.99 KB
/
jmctl.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package main
import (
"fmt"
"os"
"log"
"io/ioutil"
"strconv"
// "flag"
"github.com/jawher/mow.cli"
"github.com/Snowlabs/Jamyxgo"
)
const NAME_ALIAS = "name n na nam -n --name"
const ISIN_ALIAS = "is-input isinput isin i in inp inpu input -in --is-input"
const ISMO_ALIAS = "is-mono ismono ismo ismon ism mono m mo mon -mo --is-mono"
const VOLM_ALIAS = "volume vol vo v -v --volume"
const BALN_ALIAS = "balance bal ba b -b --balance"
const CONS_ALIAS = "connections cons cs c -cs --connections"
const COND_ALIAS = "connected conned connd cond cnd cd -cd --connected"
const MOND_ALIAS = "monitored monned monnd moned mond mnd md -md --monitored"
type Float32Arg float32
func (fla *Float32Arg) String() string {
return strconv.FormatFloat(float64(*fla), 'f', -1, 32)
}
func (fla *Float32Arg) Set(s string) error {
val, err := strconv.ParseFloat(s, 32)
if err != nil {
return err
}
*fla = Float32Arg(float32(val))
return err
}
func main() {
var target *jamyxgo.Target
app := cli.App("jmctl", "Jamyxer control")
app.Spec = "[-v]"
verbose := app.BoolOpt("v verbose", false, "Verbose debug mode")
app.Before = func() {
target = jamyxgo.NewTarget("localhost", 56065)
if !*verbose {
log.SetFlags(0)
log.SetOutput(ioutil.Discard)
}
}
app.Command("get-all ga geta getall", "get all channels", func(cmd *cli.Cmd) {
cmd.Spec = "-i|-o"
isin := cmd.BoolOpt("i input", false, "input")
isout := cmd.BoolOpt("o output", false, "output")
cmd.Action = func() {
var ports []jamyxgo.Port
if *isin && !*isout {
ports = target.GetPorts().Inputs
} else {
ports = target.GetPorts().Outputs
}
for _, c := range ports {
fmt.Println(c.Port)
}
}
})
app.Command("get g ge -g --get", "get a channel's properties", func(cmd *cli.Cmd) {
var port jamyxgo.Port
var wait func()
cmd.Spec = "((-io PORT) | -m) [-VB]"
name := cmd.StringArg("PORT", "", "Port name")
isin := cmd.BoolOpt("i input", false, "input")
isout := cmd.BoolOpt("o output", false, "output")
ismon := cmd.BoolOpt("m monitor", false, "monitor")
mon_vol := cmd.BoolOpt("V monitor-vol", false, "wait for volume to change")
mon_bal := cmd.BoolOpt("B monitor-bal", false, "wait for balance to change")
cmd.Before = func() {
// if (*isin == *isout) && (*isin == *ismon) { panic("Impossible state!") }
if !*ismon {
port = target.GetPort(*isin && !*isout, *name)
} else {
port = target.GetMonitorPort()
}
if *mon_vol {
wait = port.ListenVol
} else if *mon_bal {
wait = port.ListenBal
} else {
wait = func() { /* Do not wait */ }
}
}
construct_cmd := func(name, desc string, attr func() interface{}) {
cmd.Command(name, desc, func(cmd *cli.Cmd) { cmd.Action = func() { wait(); fmt.Println(attr()) } })
}
construct_cmd(NAME_ALIAS, "get a channel's name", func() interface{} { return port.Port })
construct_cmd(ISIN_ALIAS, "is a channel input", func() interface{} { return port.IsInput })
construct_cmd(ISMO_ALIAS, "is a channel mono", func() interface{} { return port.IsMono })
construct_cmd(VOLM_ALIAS, "get a channel's volume", func() interface{} { return port.Vol })
construct_cmd(BALN_ALIAS, "get a channel's balance", func() interface{} { return port.Bal })
cmd.Command(CONS_ALIAS, "get a channel's connections", func(cmd *cli.Cmd) { cmd.Action = func() {
wait()
for _, c := range port.Cons {
fmt.Println(c)
}
} })
cmd.Command(COND_ALIAS, "is a channel connected to ...", func(cmd *cli.Cmd) {
name2 := cmd.StringArg("OTHER", "", "Other port name")
cmd.Action = func() {
wait()
for _, e := range port.Cons {
if e == *name2 { fmt.Println(true); return }
}
fmt.Println(false)
}
})
cmd.Command(MOND_ALIAS, "is a channel monitored", func(cmd *cli.Cmd) { cmd.Action = func() {
wait()
m := target.GetMonitorPort()
fmt.Println((port.Port == m.Port) && (port.IsInput == m.IsInput))
} })
})
app.Command("set se s -s --set", "set a channel's properties", func(cmd *cli.Cmd) {
var port jamyxgo.Port
cmd.Spec = "(-io PORT) | -m"
name := cmd.StringArg("PORT", "", "Port name")
isin := cmd.BoolOpt("i input", false, "input")
isout := cmd.BoolOpt("o output", false, "output")
ismon := cmd.BoolOpt("m monitor", false, "monitor")
cmd.Before = func() {
// if (*isin == *isout) && (*isin == *ismon) { panic("Impossible state!") }
if !*ismon {
port = target.GetPort(*isin && !*isout, *name)
} else {
port = target.GetMonitorPort()
}
}
cmd.Command(VOLM_ALIAS, "set a channel's volume", func(cmd *cli.Cmd) {
val := Float32Arg(0)
cmd.VarArg("VAL", &val, "value")
cmd.Spec = "-- VAL"
cmd.Action = func() {
port.SetVol(float32(val))
fmt.Println(port.Vol)
}
})
cmd.Command(BALN_ALIAS, "set a channel's balance", func(cmd *cli.Cmd) {
val := Float32Arg(0)
cmd.VarArg("VAL", &val, "value")
cmd.Spec = "-- VAL"
cmd.Action = func() {
port.SetBal(float32(val))
fmt.Println(port.Bal)
}
})
cmd.Command(COND_ALIAS, "set a channel connection with ...", func(cmd *cli.Cmd) {
cmd.Spec = "OTHER -tcd"
name2 := cmd.StringArg("OTHER", "", "Other port name")
tog := cmd.BoolOpt("t toggle", false, "toggle")
con := cmd.BoolOpt("c connect", false, "connect")
dis := cmd.BoolOpt("d disconnect", false, "disconnect")
cmd.Action = func() {
if *tog {
port.ToggleConnectionWithChannel(*name2)
} else if *con {
port.ConnectToChannel(*name2)
} else if *dis {
port.DisconnectFromChannel(*name2)
}
port.Update() // might be unnecessary
for _, c := range port.Cons {
fmt.Println(c)
}
}
})
cmd.Command(MOND_ALIAS, "set a channel to be monitored", func(cmd *cli.Cmd) {
cmd.Action = func() {
port.SetMonitored()
fmt.Println(target.GetMonitorPort().Port)
}
})
})
app.Run(os.Args)
}