Skip to content

Commit cecd21a

Browse files
committed
oscli metro command
1 parent ac232d2 commit cecd21a

4 files changed

Lines changed: 115 additions & 17 deletions

File tree

cmd/oscli/cmd/generate.go

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package cmd
33
import (
44
"fmt"
55
"log"
6-
"net"
76

87
"github.com/glynternet/oscli/internal"
98
"github.com/glynternet/oscli/models"
@@ -21,33 +20,24 @@ const (
2120
)
2221

2322
var cmdOSCGen = &cobra.Command{
24-
Use: "generate",
23+
Use: "generate [ADDRESS] [MESSAGE]...",
2524
Short: "generate a stream of osc messages",
2625
Long: `generate a stream of osc messages
2726
2827
Generate an osc signal with values ranging from 0 to 1 as a sin wave.
2928
The messages will be sent to the given address.`,
29+
Args: cobra.MinimumNArgs(2),
3030
RunE: func(cmd *cobra.Command, args []string) error {
31-
if len(args) == 0 {
32-
return errors.New("arguments required to form OSC message")
33-
}
3431
msgAddr, err := internal.CleanAddress(args[0])
3532
if err != nil {
3633
return errors.Wrap(err, "parsing OSC message address")
3734
}
3835

39-
host, err := internal.GetRemoteHost(
40-
viper.GetBool(keyLocal),
41-
viper.GetString(keyRemoteHost),
42-
)
36+
host, err := initRemoteHost()
4337
if err != nil {
44-
log.Fatal(errors.Wrap(err, "getting remote host"))
38+
return errors.Wrap(err, "initialising host")
4539
}
4640

47-
_, err = net.LookupHost(host)
48-
if err != nil {
49-
log.Fatal(errors.Wrapf(err, "looking up %s host %s", keyRemoteHost, host))
50-
}
5141
client := osc.NewClient(
5242
host,
5343
viper.GetInt(keyRemotePort),
@@ -61,7 +51,11 @@ The messages will be sent to the given address.`,
6151
var staticArgs []interface{}
6252
if len(args) > 0 {
6353
for _, arg := range args[1:] {
64-
staticArgs = append(staticArgs, arg)
54+
a, err := osc2.Parse(arg)
55+
if err != nil {
56+
return errors.Wrapf(err, "parsing arg '%s' as value", arg)
57+
}
58+
staticArgs = append(staticArgs, a)
6559
}
6660
}
6761

@@ -75,6 +69,7 @@ The messages will be sent to the given address.`,
7569
err := client.Send(msg)
7670
if err != nil {
7771
log.Print(errors.Wrap(err, "sending message to client"))
72+
continue
7873
}
7974
log.Printf("Message (%+v) sent to client at %s:%d", msg, client.IP(), client.Port())
8075
}

cmd/oscli/cmd/metro.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"log"
6+
7+
"github.com/glynternet/oscli/internal"
8+
osc2 "github.com/glynternet/oscli/pkg/osc"
9+
"github.com/glynternet/oscli/pkg/wave"
10+
"github.com/hypebeast/go-osc/osc"
11+
"github.com/pkg/errors"
12+
"github.com/spf13/cobra"
13+
"github.com/spf13/viper"
14+
)
15+
16+
var cmdMetro = &cobra.Command{
17+
Use: "metro [ADDRESS] [MESSAGE]...",
18+
Short: "generate a ticker of the same OSC message",
19+
Args: cobra.MinimumNArgs(2),
20+
RunE: func(cmd *cobra.Command, args []string) error {
21+
msgAddr, err := internal.CleanAddress(args[0])
22+
if err != nil {
23+
return errors.Wrap(err, "parsing OSC message address")
24+
}
25+
26+
host, err := initRemoteHost()
27+
if err != nil {
28+
return errors.Wrap(err, "initialising host")
29+
}
30+
31+
client := osc.NewClient(
32+
host,
33+
viper.GetInt(keyRemotePort),
34+
)
35+
36+
msgFreq := viper.GetFloat64(keyMsgFrequency)
37+
if msgFreq <= 0 {
38+
log.Fatal(fmt.Errorf("%s must be positive, received %f", keyMsgFrequency, msgFreq))
39+
}
40+
41+
var staticArgs []interface{}
42+
if len(args) > 0 {
43+
for _, arg := range args[1:] {
44+
a, err := osc2.Parse(arg)
45+
if err != nil {
46+
return errors.Wrapf(err, "parsing arg '%s' as value", arg)
47+
}
48+
staticArgs = append(staticArgs, a)
49+
}
50+
}
51+
52+
genFn := func() *osc.Message {
53+
return osc.NewMessage(msgAddr, staticArgs...)
54+
}
55+
56+
// TODO: the second argument to this could be a ticker or something?
57+
msgCh := osc2.Generate(genFn, wave.Frequency(msgFreq).Period())
58+
for {
59+
select {
60+
case msg := <-msgCh:
61+
err := client.Send(msg)
62+
if err != nil {
63+
log.Print(errors.Wrap(err, "sending message to client"))
64+
continue
65+
}
66+
log.Printf("Message (%+v) sent to client at %s:%d", msg, client.IP(), client.Port())
67+
}
68+
}
69+
},
70+
}
71+
72+
func init() {
73+
rootCmd.AddCommand(cmdMetro)
74+
err := viper.BindPFlags(cmdMetro.Flags())
75+
if err != nil {
76+
log.Fatal(err)
77+
}
78+
}

cmd/oscli/cmd/root.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package cmd
22

33
import (
44
"log"
5+
"net"
56
"strings"
67

8+
"github.com/glynternet/oscli/internal"
79
"github.com/pkg/errors"
810
"github.com/spf13/cobra"
911
"github.com/spf13/viper"
@@ -15,7 +17,7 @@ func Execute() error {
1517
}
1618

1719
const (
18-
appName = "osc"
20+
appName = "oscli"
1921

2022
keyListenHost = "listen-host"
2123
usageListenHost = "host address to listen on"
@@ -43,6 +45,9 @@ func init() {
4345
rootCmd.PersistentFlags().Uint(keyListenPort, 9000, usageListenPort)
4446
rootCmd.PersistentFlags().StringP(keyRemoteHost, "r", "", usageRemoteHost)
4547
rootCmd.PersistentFlags().Uint(keyRemotePort, 9000, usageRemotePort)
48+
49+
rootCmd.PersistentFlags().Float64P(keyMsgFrequency, "m", 25, "frequency to send messages at")
50+
4651
err := viper.BindPFlags(rootCmd.PersistentFlags())
4752
if err != nil {
4853
log.Fatal(errors.Wrap(err, "binding PFlags"))
@@ -54,3 +59,22 @@ func initConfig() {
5459
viper.AutomaticEnv() // read in environment variables that match
5560
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
5661
}
62+
63+
func initRemoteHost() (string, error) {
64+
host, err := internal.GetRemoteHost(
65+
viper.GetBool(keyLocal),
66+
viper.GetString(keyRemoteHost),
67+
)
68+
if err != nil {
69+
return "", errors.Wrap(err, "getting remote host")
70+
}
71+
72+
return host, errors.Wrap(verifyHost(host), "verifying host")
73+
}
74+
75+
// verifyHost checks that the given string can be resolved through the current
76+
// DNS/networking state
77+
func verifyHost(host string) error {
78+
_, err := net.LookupHost(host)
79+
return errors.Wrapf(err, "looking up %s host %s", keyRemoteHost, host)
80+
}

cmd/oscli/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func main() {
1717
}
1818

1919
const (
20-
appName = "osc"
20+
appName = "oscli"
2121

2222
keyListenHost = "listen-host"
2323
usageListenHost = "host address to listen on"
@@ -40,6 +40,7 @@ var rootCmd = &cobra.Command{
4040
}
4141

4242
func init() {
43+
cobra.OnInitialize(initConfig)
4344
rootCmd.PersistentFlags().BoolP(keyLocal, "l", false, usageLocal)
4445
rootCmd.PersistentFlags().String(keyListenHost, "", usageListenHost)
4546
rootCmd.PersistentFlags().Uint(keyListenPort, 9000, usageListenPort)

0 commit comments

Comments
 (0)