Skip to content

Commit c427146

Browse files
committed
blobsupport:Move blob parse to internal/osc
1 parent 6a0942a commit c427146

3 files changed

Lines changed: 22 additions & 20 deletions

File tree

cmd/oscli/cmd/root.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"strings"
77

88
"github.com/glynternet/oscli/internal"
9+
"github.com/glynternet/oscli/internal/osc"
910
"github.com/pkg/errors"
1011
"github.com/spf13/cobra"
1112
"github.com/spf13/viper"
@@ -32,6 +33,8 @@ const (
3233
var (
3334
remoteHost string
3435
remotePort uint
36+
37+
asBlob bool
3538
)
3639

3740
var rootCmd = &cobra.Command{
@@ -45,6 +48,7 @@ func init() {
4548
rootCmd.PersistentFlags().StringVarP(&remoteHost, "remote-host", "r", "", "address to send any messages to")
4649
rootCmd.PersistentFlags().UintVar(&remotePort, "remote-port", 9000, "port of the remote host to send any messages to")
4750

51+
rootCmd.Flags().BoolVar(&asBlob, "as-blob", false, "send all arguments as blobs (in monitor and send subcommands)")
4852
rootCmd.PersistentFlags().Float64P(keyMsgFrequency, "m", 25, "frequency to send messages at")
4953

5054
err := viper.BindPFlags(rootCmd.PersistentFlags())
@@ -77,3 +81,10 @@ func verifyHost(host string) error {
7781
_, err := net.LookupHost(host)
7882
return errors.Wrapf(err, "looking up host %s on network", host)
7983
}
84+
85+
func getParser(asBlobs bool) func(string) (interface{}, error) {
86+
if asBlobs {
87+
return osc.BlobParse
88+
}
89+
return osc.Parse
90+
}

cmd/oscli/cmd/send.go

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,13 @@ import (
66
"net"
77

88
"github.com/glynternet/oscli/internal"
9-
osc3 "github.com/glynternet/oscli/internal/osc"
109
"github.com/glynternet/oscli/pkg/osc"
1110
"github.com/pkg/errors"
1211
osc2 "github.com/sander/go-osc/osc"
1312
"github.com/spf13/cobra"
1413
"github.com/spf13/viper"
1514
)
1615

17-
const keyAsBlob = "as-blob"
18-
1916
var cmdSend = &cobra.Command{
2017
Use: "send",
2118
Short: "send a single OSC message",
@@ -47,7 +44,7 @@ var cmdSend = &cobra.Command{
4744
)
4845

4946
msg := osc2.NewMessage(msgAddr)
50-
parse := getParser(viper.GetBool(keyAsBlob))
47+
parse := getParser(asBlob)
5148
for _, val := range args[1:] {
5249
app, err := parse(val)
5350
if err != nil {
@@ -65,23 +62,8 @@ var cmdSend = &cobra.Command{
6562
},
6663
}
6764

68-
type argParser func(arg string) interface{}
69-
70-
// blobParse will convert any string argument to a []byte so that it will be sent
71-
// as a blob argument
72-
func blobParse(arg string) (interface{}, error) {
73-
return []byte(arg), nil
74-
}
75-
76-
func getParser(asBlobs bool) func(string) (interface{}, error) {
77-
if asBlobs {
78-
return blobParse
79-
}
80-
return osc3.Parse
81-
}
82-
8365
func init() {
84-
cmdSend.Flags().Bool(keyAsBlob, false, "send all arguments as blobs")
66+
8567
rootCmd.AddCommand(cmdSend)
8668
err := viper.BindPFlags(cmdSend.Flags())
8769
if err != nil {

internal/osc/parse.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ func Parse(val string) (interface{}, error) {
3030
return val, nil
3131
}
3232

33+
// BlobParse will convert any string argument to a []byte so that it will be sent
34+
// as a blob argument
35+
func BlobParse(val string) (interface{}, error) {
36+
if val == "" {
37+
return nil, EmptyValueError
38+
}
39+
return []byte(val), nil
40+
}
41+
3342
// ParseError is an error type that can be returned when there is an issue with
3443
// parsing a string value in order to turn it into an osc.Message.
3544
type ParseError string

0 commit comments

Comments
 (0)