diff --git a/cmd/vbantxt/main.go b/cmd/vbantxt/main.go index 4f17e36..b73be00 100644 --- a/cmd/vbantxt/main.go +++ b/cmd/vbantxt/main.go @@ -82,12 +82,12 @@ func main() { } } -func createClient(host string, port int, streamname string, bps int, channel, ratelimit int) (*vbantxt.VbanTxt, error) { +func createClient(host string, port int, streamname string, bps, channel, ratelimit int) (*vbantxt.VbanTxt, error) { client, err := vbantxt.New( host, port, streamname, - vbantxt.WithBPSOpt(indexOf(vbantxt.BpsOpts, bps)), + vbantxt.WithBPSOpt(bps), vbantxt.WithChannel(channel), vbantxt.WithRateLimit(time.Duration(ratelimit)*time.Millisecond)) if err != nil { diff --git a/cmd/vbantxt/util.go b/cmd/vbantxt/util.go index bb0ac6d..399cedb 100644 --- a/cmd/vbantxt/util.go +++ b/cmd/vbantxt/util.go @@ -15,12 +15,3 @@ func flagsPassed(flags []string) bool { }) return found } - -func indexOf[T comparable](collection []T, e T) int { - for i, x := range collection { - if x == e { - return i - } - } - return -1 -} diff --git a/option.go b/option.go index af999ba..cdbe039 100644 --- a/option.go +++ b/option.go @@ -17,10 +17,11 @@ func WithRateLimit(ratelimit time.Duration) Option { } // WithBPSOpt is a functional option to set the bps index for {VbanTx}.{Packet}.bpsIndex -func WithBPSOpt(bpsIndex int) Option { +func WithBPSOpt(bps int) Option { return func(vt *VbanTxt) { - if bpsIndex < 0 || bpsIndex >= len(BpsOpts) { - log.Warnf("invalid bpsIndex %d, defaulting to 0", bpsIndex) + bpsIndex := indexOf(BpsOpts, bps) + if bpsIndex == -1 { + log.Warnf("invalid bps value %d, expected one of %v, defaulting to 0", bps, BpsOpts) return } vt.packet.bpsIndex = bpsIndex diff --git a/util.go b/util.go new file mode 100644 index 0000000..eecbd94 --- /dev/null +++ b/util.go @@ -0,0 +1,10 @@ +package vbantxt + +func indexOf[T comparable](collection []T, e T) int { + for i, x := range collection { + if x == e { + return i + } + } + return -1 +}