mirror of
https://github.com/onyx-and-iris/vbantxt.git
synced 2026-03-12 13:09:16 +00:00
Compare commits
9 Commits
ecd16545ab
...
87ea26ab03
| Author | SHA1 | Date | |
|---|---|---|---|
| 87ea26ab03 | |||
| 331d4e2f9a | |||
| d21dfb9481 | |||
| 510d43ca33 | |||
| acac22f70e | |||
| b568767f86 | |||
| 08909b6b66 | |||
| c6d03e87d7 | |||
| 0d0dbedbcd |
13
.github/workflows/golang-ci.yml
vendored
13
.github/workflows/golang-ci.yml
vendored
@ -2,11 +2,11 @@ name: CI
|
|||||||
|
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
branches: [ "main" ]
|
branches: ['main']
|
||||||
paths:
|
paths:
|
||||||
- '**.go'
|
- '**.go'
|
||||||
pull_request:
|
pull_request:
|
||||||
branches: [ "main" ]
|
branches: ['main']
|
||||||
paths:
|
paths:
|
||||||
- '**.go'
|
- '**.go'
|
||||||
jobs:
|
jobs:
|
||||||
@ -23,7 +23,8 @@ jobs:
|
|||||||
uses: actions/setup-go@v5
|
uses: actions/setup-go@v5
|
||||||
with:
|
with:
|
||||||
go-version: '1.24'
|
go-version: '1.24'
|
||||||
- name: Install golangci-lint
|
- name: golangci-lint
|
||||||
run: go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
|
uses: golangci/golangci-lint-action@v9
|
||||||
- name: Run golangci-lint
|
with:
|
||||||
run: golangci-lint run ./...
|
version: v2.6.0
|
||||||
|
args: --config .golangci.yml
|
||||||
|
|||||||
138
.golangci.yml
138
.golangci.yml
@ -1,54 +1,110 @@
|
|||||||
|
version: '2'
|
||||||
|
|
||||||
run:
|
run:
|
||||||
# timeout for analysis, e.g. 30s, 3m, default is 1m
|
|
||||||
timeout: 3m
|
timeout: 3m
|
||||||
# exclude test files
|
|
||||||
tests: true
|
tests: true
|
||||||
|
go: '1.24'
|
||||||
|
|
||||||
linters:
|
linters:
|
||||||
# Set to true runs only fast linters.
|
|
||||||
# Good option for 'lint on save', pre-commit hook or CI.
|
|
||||||
fast: true
|
|
||||||
|
|
||||||
disable-all: true
|
|
||||||
|
|
||||||
enable:
|
enable:
|
||||||
- gosimple
|
# Default enabled linters
|
||||||
- govet
|
- errcheck # Check for unchecked errors
|
||||||
- ineffassign
|
- govet # Go's built-in vetting tool
|
||||||
- staticcheck
|
- ineffassign # Detect ineffectual assignments
|
||||||
- unused
|
- staticcheck # Advanced static analysis
|
||||||
- gofmt
|
- unused # Check for unused code
|
||||||
- gofumpt
|
# Additional useful linters
|
||||||
- misspell
|
- misspell # Detect common misspellings
|
||||||
- unparam
|
- unparam # Check for unused function parameters
|
||||||
- gosec
|
- gosec # Security checks
|
||||||
- asciicheck
|
- asciicheck # Check for non-ASCII characters
|
||||||
- errname
|
- errname # Check error variable names
|
||||||
- gci
|
- godot # Check for missing periods in comments
|
||||||
- godot
|
- revive # Highly configurable linter for style and correctness
|
||||||
- goimports
|
- gocritic # Detect code issues and suggest improvements
|
||||||
- revive
|
- gocyclo # Check for cyclomatic complexity
|
||||||
|
- dupl # Check for code duplication
|
||||||
|
- predeclared # Check for shadowing of predeclared identifiers
|
||||||
|
- copyloopvar # Check for loop variable capture in goroutines
|
||||||
|
- errorlint # Check for common mistakes in error handling
|
||||||
|
- goconst # Check for repeated strings that could be constants
|
||||||
|
- gosmopolitan # Check for non-portable code
|
||||||
|
|
||||||
linters-settings:
|
settings:
|
||||||
gofmt:
|
misspell:
|
||||||
rewrite-rules:
|
locale: UK
|
||||||
- pattern: 'interface{}'
|
|
||||||
replacement: 'any'
|
|
||||||
- pattern: 'a[b:len(a)]'
|
|
||||||
replacement: 'a[b:]'
|
|
||||||
|
|
||||||
misspell:
|
errcheck:
|
||||||
locale: UK
|
check-type-assertions: true
|
||||||
|
check-blank: true
|
||||||
|
|
||||||
errcheck:
|
revive:
|
||||||
check-type-assertions: true
|
rules:
|
||||||
|
# Code quality and style
|
||||||
|
- name: exported
|
||||||
|
arguments:
|
||||||
|
- 'checkPrivateReceivers'
|
||||||
|
- 'sayRepetitiveInsteadOfStutters'
|
||||||
|
- name: var-naming
|
||||||
|
- name: package-comments
|
||||||
|
- name: range-val-in-closure
|
||||||
|
- name: time-naming
|
||||||
|
- name: context-as-argument
|
||||||
|
- name: context-keys-type
|
||||||
|
- name: dot-imports
|
||||||
|
- name: empty-block
|
||||||
|
- name: error-return
|
||||||
|
- name: error-strings
|
||||||
|
- name: error-naming
|
||||||
|
- name: if-return
|
||||||
|
- name: increment-decrement
|
||||||
|
- name: indent-error-flow
|
||||||
|
- name: receiver-naming
|
||||||
|
- name: redefines-builtin-id
|
||||||
|
- name: superfluous-else
|
||||||
|
- name: unexported-return
|
||||||
|
- name: unreachable-code
|
||||||
|
- name: unused-parameter
|
||||||
|
- name: var-declaration
|
||||||
|
- name: blank-imports
|
||||||
|
|
||||||
|
# Disabled rules (can be enabled if needed)
|
||||||
|
# - name: line-length-limit
|
||||||
|
# arguments: [120]
|
||||||
|
# - name: function-length
|
||||||
|
# arguments: [50, 0]
|
||||||
|
# - name: cyclomatic
|
||||||
|
# arguments: [10]
|
||||||
|
|
||||||
|
gosec:
|
||||||
|
excludes:
|
||||||
|
- G104 # Duplicated errcheck checks
|
||||||
|
- G115 # integer overflow conversion int -> uint32
|
||||||
|
|
||||||
|
# Formatters configuration
|
||||||
|
formatters:
|
||||||
|
# Enable specific formatters
|
||||||
|
enable:
|
||||||
|
- gofumpt # Stricter gofmt alternative
|
||||||
|
- goimports # Organizes imports
|
||||||
|
- gci # Controls import order/grouping
|
||||||
|
- golines # Enforces line length
|
||||||
|
|
||||||
|
# Formatter-specific settings
|
||||||
|
settings:
|
||||||
|
goimports:
|
||||||
|
local-prefixes: [github.com/onyx-and-iris/vbantxt]
|
||||||
|
|
||||||
|
gci:
|
||||||
|
# Define import sections order
|
||||||
|
sections:
|
||||||
|
- standard # Standard library
|
||||||
|
- default # Everything else
|
||||||
|
- prefix(github.com/onyx-and-iris/vbantxt) # Current module
|
||||||
|
|
||||||
|
gofumpt:
|
||||||
|
extra-rules: true # Enable additional formatting rules
|
||||||
|
|
||||||
issues:
|
issues:
|
||||||
max-same-issues: 0
|
max-same-issues: 0
|
||||||
max-issues-per-linter: 0
|
max-issues-per-linter: 0
|
||||||
exclude-use-default: false
|
|
||||||
exclude:
|
|
||||||
# gosec: Duplicated errcheck checks
|
|
||||||
- G104
|
|
||||||
# gosec: integer overflow conversion int -> uint32
|
|
||||||
- G115
|
|
||||||
|
|||||||
11
CHANGELOG.md
11
CHANGELOG.md
@ -11,6 +11,17 @@ Before any major/minor/patch bump all unit tests will be run to verify they pass
|
|||||||
|
|
||||||
- [x]
|
- [x]
|
||||||
|
|
||||||
|
# [0.6.0] - 2025-02-14
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
- streamname length validation added to {packet} factory method.
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
- {packet}.bspIndex now defaults to *indexOf(BpsOpts, 256000)*.
|
||||||
|
- this doesn't affect the CLI which already defaulted to 256000.
|
||||||
|
- WithBPSOpt and WithChannel functional options now log at warn level if passed out of bounds value. They will fallback to valid defaults.
|
||||||
|
|
||||||
# [0.5.0] - 2025-06-25
|
# [0.5.0] - 2025-06-25
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
|||||||
11
Taskfile.yml
11
Taskfile.yml
@ -9,6 +9,7 @@ vars:
|
|||||||
|
|
||||||
WINDOWS: '{{.BIN_DIR}}/{{.PROGRAM}}_windows_amd64.exe'
|
WINDOWS: '{{.BIN_DIR}}/{{.PROGRAM}}_windows_amd64.exe'
|
||||||
LINUX: '{{.BIN_DIR}}/{{.PROGRAM}}_linux_amd64'
|
LINUX: '{{.BIN_DIR}}/{{.PROGRAM}}_linux_amd64'
|
||||||
|
MACOS: '{{.BIN_DIR}}/{{.PROGRAM}}_darwin_amd64'
|
||||||
|
|
||||||
tasks:
|
tasks:
|
||||||
default:
|
default:
|
||||||
@ -22,7 +23,7 @@ tasks:
|
|||||||
cmds:
|
cmds:
|
||||||
- task: build-windows
|
- task: build-windows
|
||||||
- task: build-linux
|
- task: build-linux
|
||||||
|
- task: build-macos
|
||||||
vet:
|
vet:
|
||||||
desc: Vet the code
|
desc: Vet the code
|
||||||
deps: [fmt]
|
deps: [fmt]
|
||||||
@ -38,11 +39,19 @@ tasks:
|
|||||||
desc: Build the vbantxt project for Windows
|
desc: Build the vbantxt project for Windows
|
||||||
cmds:
|
cmds:
|
||||||
- GOOS=windows GOARCH=amd64 go build -o {{.WINDOWS}} -ldflags="-X main.version={{.VERSION}}" ./cmd/{{.PROGRAM}}/
|
- GOOS=windows GOARCH=amd64 go build -o {{.WINDOWS}} -ldflags="-X main.version={{.VERSION}}" ./cmd/{{.PROGRAM}}/
|
||||||
|
internal: true
|
||||||
|
|
||||||
build-linux:
|
build-linux:
|
||||||
desc: Build the vbantxt project for Linux
|
desc: Build the vbantxt project for Linux
|
||||||
cmds:
|
cmds:
|
||||||
- GOOS=linux GOARCH=amd64 go build -o {{.LINUX}} -ldflags="-X main.version={{.VERSION}}" ./cmd/{{.PROGRAM}}/
|
- GOOS=linux GOARCH=amd64 go build -o {{.LINUX}} -ldflags="-X main.version={{.VERSION}}" ./cmd/{{.PROGRAM}}/
|
||||||
|
internal: true
|
||||||
|
|
||||||
|
build-macos:
|
||||||
|
desc: Build the vbantxt project for macOS
|
||||||
|
cmds:
|
||||||
|
- GOOS=darwin GOARCH=amd64 go build -o {{.MACOS}} -ldflags="-X main.version={{.VERSION}}" ./cmd/{{.PROGRAM}}/
|
||||||
|
internal: true
|
||||||
|
|
||||||
test:
|
test:
|
||||||
desc: Run tests
|
desc: Run tests
|
||||||
|
|||||||
@ -11,10 +11,11 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/charmbracelet/log"
|
"github.com/charmbracelet/log"
|
||||||
"github.com/onyx-and-iris/vbantxt"
|
|
||||||
"github.com/peterbourgon/ff/v4"
|
"github.com/peterbourgon/ff/v4"
|
||||||
"github.com/peterbourgon/ff/v4/ffhelp"
|
"github.com/peterbourgon/ff/v4/ffhelp"
|
||||||
"github.com/peterbourgon/ff/v4/fftoml"
|
"github.com/peterbourgon/ff/v4/fftoml"
|
||||||
|
|
||||||
|
"github.com/onyx-and-iris/vbantxt"
|
||||||
)
|
)
|
||||||
|
|
||||||
var version string // Version will be set at build time
|
var version string // Version will be set at build time
|
||||||
@ -32,8 +33,22 @@ type Flags struct {
|
|||||||
Version bool // Version flag
|
Version bool // Version flag
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (f *Flags) String() string {
|
||||||
|
return fmt.Sprintf(
|
||||||
|
"Host: %s, Port: %d, Streamname: %s, Bps: %d, Channel: %d, Ratelimit: %dms, ConfigPath: %s, Loglevel: %s",
|
||||||
|
f.Host,
|
||||||
|
f.Port,
|
||||||
|
f.Streamname,
|
||||||
|
f.Bps,
|
||||||
|
f.Channel,
|
||||||
|
f.Ratelimit,
|
||||||
|
f.ConfigPath,
|
||||||
|
f.Loglevel,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
func exitOnError(err error) {
|
func exitOnError(err error) {
|
||||||
_, _ = fmt.Fprintf(os.Stderr, "Error: %s\n", err)
|
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -41,7 +56,7 @@ func main() {
|
|||||||
var flags Flags
|
var flags Flags
|
||||||
|
|
||||||
// VBAN specific flags
|
// VBAN specific flags
|
||||||
fs := ff.NewFlagSet("vbantxt")
|
fs := ff.NewFlagSet("vbantxt - A command-line tool for sending text requests over VBAN")
|
||||||
fs.StringVar(&flags.Host, 'H', "host", "localhost", "VBAN host")
|
fs.StringVar(&flags.Host, 'H', "host", "localhost", "VBAN host")
|
||||||
fs.IntVar(&flags.Port, 'p', "port", 6980, "VBAN port")
|
fs.IntVar(&flags.Port, 'p', "port", 6980, "VBAN port")
|
||||||
fs.StringVar(&flags.Streamname, 's', "streamname", "Command1", "VBAN stream name")
|
fs.StringVar(&flags.Streamname, 's', "streamname", "Command1", "VBAN stream name")
|
||||||
@ -56,8 +71,20 @@ func main() {
|
|||||||
defaultConfigPath := filepath.Join(configDir, "vbantxt", "config.toml")
|
defaultConfigPath := filepath.Join(configDir, "vbantxt", "config.toml")
|
||||||
|
|
||||||
// Configuration file and logging flags
|
// Configuration file and logging flags
|
||||||
fs.StringVar(&flags.ConfigPath, 'C', "config", defaultConfigPath, "Path to the configuration file")
|
fs.StringVar(
|
||||||
fs.StringVar(&flags.Loglevel, 'l', "loglevel", "warn", "Log level (debug, info, warn, error, fatal, panic)")
|
&flags.ConfigPath,
|
||||||
|
'C',
|
||||||
|
"config",
|
||||||
|
defaultConfigPath,
|
||||||
|
"Path to the configuration file",
|
||||||
|
)
|
||||||
|
fs.StringVar(
|
||||||
|
&flags.Loglevel,
|
||||||
|
'l',
|
||||||
|
"loglevel",
|
||||||
|
"warn",
|
||||||
|
"Log level (debug, info, warn, error, fatal, panic)",
|
||||||
|
)
|
||||||
fs.BoolVar(&flags.Version, 'v', "version", "Show version information")
|
fs.BoolVar(&flags.Version, 'v', "version", "Show version information")
|
||||||
|
|
||||||
err = ff.Parse(fs, os.Args[1:],
|
err = ff.Parse(fs, os.Args[1:],
|
||||||
@ -75,14 +102,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if flags.Version {
|
if flags.Version {
|
||||||
if version == "" {
|
fmt.Printf("vbantxt version: %s\n", versionFromBuild())
|
||||||
info, ok := debug.ReadBuildInfo()
|
|
||||||
if !ok {
|
|
||||||
exitOnError(errors.New("failed to read build info"))
|
|
||||||
}
|
|
||||||
version = strings.Split(info.Main.Version, "-")[0]
|
|
||||||
}
|
|
||||||
fmt.Printf("vbantxt version: %s\n", version)
|
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -92,7 +112,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
log.SetLevel(level)
|
log.SetLevel(level)
|
||||||
|
|
||||||
log.Debugf("Loaded configuration: %+v", flags)
|
log.Debugf("Loaded configuration: %s", flags.String())
|
||||||
|
|
||||||
client, closer, err := createClient(&flags)
|
client, closer, err := createClient(&flags)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -100,12 +120,24 @@ func main() {
|
|||||||
}
|
}
|
||||||
defer closer()
|
defer closer()
|
||||||
|
|
||||||
for _, arg := range fs.GetArgs() {
|
commands := fs.GetArgs()
|
||||||
err := client.Send(arg)
|
if len(commands) == 0 {
|
||||||
if err != nil {
|
exitOnError(errors.New("no VBAN commands provided"))
|
||||||
log.Error(err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sendCommands(client, commands)
|
||||||
|
}
|
||||||
|
|
||||||
|
// versionFromBuild retrieves the version information from the build metadata.
|
||||||
|
func versionFromBuild() string {
|
||||||
|
if version == "" {
|
||||||
|
info, ok := debug.ReadBuildInfo()
|
||||||
|
if !ok {
|
||||||
|
exitOnError(errors.New("failed to read build info"))
|
||||||
|
}
|
||||||
|
version = strings.Split(info.Main.Version, "-")[0]
|
||||||
|
}
|
||||||
|
return version
|
||||||
}
|
}
|
||||||
|
|
||||||
// createClient creates a new vban client with the provided options.
|
// createClient creates a new vban client with the provided options.
|
||||||
@ -129,3 +161,14 @@ func createClient(flags *Flags) (*vbantxt.VbanTxt, func(), error) {
|
|||||||
|
|
||||||
return client, closer, err
|
return client, closer, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// sendCommands sends a list of commands to the VBAN client.
|
||||||
|
func sendCommands(client *vbantxt.VbanTxt, commands []string) {
|
||||||
|
for _, cmd := range commands {
|
||||||
|
err := client.Send(cmd)
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("Failed to send command '%s': %v", cmd, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
2
go.mod
2
go.mod
@ -25,7 +25,7 @@ require (
|
|||||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||||
github.com/mattn/go-runewidth v0.0.19 // indirect
|
github.com/mattn/go-runewidth v0.0.19 // indirect
|
||||||
github.com/muesli/termenv v0.16.0 // indirect
|
github.com/muesli/termenv v0.16.0 // indirect
|
||||||
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
|
github.com/pelletier/go-toml/v2 v2.0.9 // indirect
|
||||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||||
github.com/rivo/uniseg v0.4.7 // indirect
|
github.com/rivo/uniseg v0.4.7 // indirect
|
||||||
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
|
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
|
||||||
|
|||||||
12
go.sum
12
go.sum
@ -18,6 +18,7 @@ github.com/clipperhouse/stringish v0.1.1 h1:+NSqMOr3GR6k1FdRhhnXrLfztGzuG+VuFDfa
|
|||||||
github.com/clipperhouse/stringish v0.1.1/go.mod h1:v/WhFtE1q0ovMta2+m+UbpZ+2/HEXNWYXQgCt4hdOzA=
|
github.com/clipperhouse/stringish v0.1.1/go.mod h1:v/WhFtE1q0ovMta2+m+UbpZ+2/HEXNWYXQgCt4hdOzA=
|
||||||
github.com/clipperhouse/uax29/v2 v2.6.0 h1:z0cDbUV+aPASdFb2/ndFnS9ts/WNXgTNNGFoKXuhpos=
|
github.com/clipperhouse/uax29/v2 v2.6.0 h1:z0cDbUV+aPASdFb2/ndFnS9ts/WNXgTNNGFoKXuhpos=
|
||||||
github.com/clipperhouse/uax29/v2 v2.6.0/go.mod h1:Wn1g7MK6OoeDT0vL+Q0SQLDz/KpfsVRgg6W7ihQeh4g=
|
github.com/clipperhouse/uax29/v2 v2.6.0/go.mod h1:Wn1g7MK6OoeDT0vL+Q0SQLDz/KpfsVRgg6W7ihQeh4g=
|
||||||
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/go-logfmt/logfmt v0.6.1 h1:4hvbpePJKnIzH1B+8OR/JPbTx37NktoI9LE2QZBBkvE=
|
github.com/go-logfmt/logfmt v0.6.1 h1:4hvbpePJKnIzH1B+8OR/JPbTx37NktoI9LE2QZBBkvE=
|
||||||
@ -37,14 +38,20 @@ github.com/mattn/go-runewidth v0.0.19 h1:v++JhqYnZuu5jSKrk9RbgF5v4CGUjqRfBm05byF
|
|||||||
github.com/mattn/go-runewidth v0.0.19/go.mod h1:XBkDxAl56ILZc9knddidhrOlY5R/pDhgLpndooCuJAs=
|
github.com/mattn/go-runewidth v0.0.19/go.mod h1:XBkDxAl56ILZc9knddidhrOlY5R/pDhgLpndooCuJAs=
|
||||||
github.com/muesli/termenv v0.16.0 h1:S5AlUN9dENB57rsbnkPyfdGuWIlkmzJjbFf0Tf5FWUc=
|
github.com/muesli/termenv v0.16.0 h1:S5AlUN9dENB57rsbnkPyfdGuWIlkmzJjbFf0Tf5FWUc=
|
||||||
github.com/muesli/termenv v0.16.0/go.mod h1:ZRfOIKPFDYQoDFF4Olj7/QJbW60Ol/kL1pU3VfY/Cnk=
|
github.com/muesli/termenv v0.16.0/go.mod h1:ZRfOIKPFDYQoDFF4Olj7/QJbW60Ol/kL1pU3VfY/Cnk=
|
||||||
github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4=
|
github.com/pelletier/go-toml/v2 v2.0.9 h1:uH2qQXheeefCCkuBBSLi7jCiSmj3VRh2+Goq2N7Xxu0=
|
||||||
github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=
|
github.com/pelletier/go-toml/v2 v2.0.9/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
|
||||||
github.com/peterbourgon/ff/v4 v4.0.0-beta.1 h1:hV8qRu3V7YfiSMsBSfPfdcznAvPQd3jI5zDddSrDoUc=
|
github.com/peterbourgon/ff/v4 v4.0.0-beta.1 h1:hV8qRu3V7YfiSMsBSfPfdcznAvPQd3jI5zDddSrDoUc=
|
||||||
github.com/peterbourgon/ff/v4 v4.0.0-beta.1/go.mod h1:onQJUKipvCyFmZ1rIYwFAh1BhPOvftb1uhvSI7krNLc=
|
github.com/peterbourgon/ff/v4 v4.0.0-beta.1/go.mod h1:onQJUKipvCyFmZ1rIYwFAh1BhPOvftb1uhvSI7krNLc=
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
|
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
|
||||||
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
|
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
|
||||||
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
|
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||||
|
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||||
|
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
|
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||||
|
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||||
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
||||||
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||||
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
|
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
|
||||||
@ -59,5 +66,6 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33
|
|||||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
||||||
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
|||||||
10
makefile
10
makefile
@ -5,24 +5,27 @@ BIN_DIR := bin
|
|||||||
|
|
||||||
WINDOWS=$(BIN_DIR)/$(PROGRAM)_windows_amd64.exe
|
WINDOWS=$(BIN_DIR)/$(PROGRAM)_windows_amd64.exe
|
||||||
LINUX=$(BIN_DIR)/$(PROGRAM)_linux_amd64
|
LINUX=$(BIN_DIR)/$(PROGRAM)_linux_amd64
|
||||||
|
MACOS=$(BIN_DIR)/$(PROGRAM)_darwin_amd64
|
||||||
VERSION=$(shell git describe --tags $(shell git rev-list --tags --max-count=1))
|
VERSION=$(shell git describe --tags $(shell git rev-list --tags --max-count=1))
|
||||||
|
|
||||||
.DEFAULT_GOAL := build
|
.DEFAULT_GOAL := build
|
||||||
|
|
||||||
.PHONY: fmt vet build windows linux test clean
|
.PHONY: fmt vet build windows linux macos test clean
|
||||||
fmt:
|
fmt:
|
||||||
$(GO) fmt ./...
|
$(GO) fmt ./...
|
||||||
|
|
||||||
vet: fmt
|
vet: fmt
|
||||||
$(GO) vet ./...
|
$(GO) vet ./...
|
||||||
|
|
||||||
build: vet windows linux | $(BIN_DIR)
|
build: vet windows linux macos | $(BIN_DIR)
|
||||||
@echo version: $(VERSION)
|
@echo version: $(VERSION)
|
||||||
|
|
||||||
windows: $(WINDOWS)
|
windows: $(WINDOWS)
|
||||||
|
|
||||||
linux: $(LINUX)
|
linux: $(LINUX)
|
||||||
|
|
||||||
|
macos: $(MACOS)
|
||||||
|
|
||||||
|
|
||||||
$(WINDOWS):
|
$(WINDOWS):
|
||||||
env GOOS=windows GOARCH=amd64 go build -v -o $(WINDOWS) -ldflags="-s -w -X main.version=$(VERSION)" ./cmd/$(PROGRAM)/
|
env GOOS=windows GOARCH=amd64 go build -v -o $(WINDOWS) -ldflags="-s -w -X main.version=$(VERSION)" ./cmd/$(PROGRAM)/
|
||||||
@ -30,6 +33,9 @@ $(WINDOWS):
|
|||||||
$(LINUX):
|
$(LINUX):
|
||||||
env GOOS=linux GOARCH=amd64 go build -v -o $(LINUX) -ldflags="-s -w -X main.version=$(VERSION)" ./cmd/$(PROGRAM)/
|
env GOOS=linux GOARCH=amd64 go build -v -o $(LINUX) -ldflags="-s -w -X main.version=$(VERSION)" ./cmd/$(PROGRAM)/
|
||||||
|
|
||||||
|
$(MACOS):
|
||||||
|
env GOOS=darwin GOARCH=amd64 go build -v -o $(MACOS) -ldflags="-s -w -X main.version=$(VERSION)" ./cmd/$(PROGRAM)/
|
||||||
|
|
||||||
test:
|
test:
|
||||||
$(GO) test ./...
|
$(GO) test ./...
|
||||||
|
|
||||||
|
|||||||
25
option.go
25
option.go
@ -19,18 +19,37 @@ func WithRateLimit(ratelimit time.Duration) Option {
|
|||||||
// WithBPSOpt is a functional option to set the bps index for {VbanTxt}.packet.
|
// WithBPSOpt is a functional option to set the bps index for {VbanTxt}.packet.
|
||||||
func WithBPSOpt(bps int) Option {
|
func WithBPSOpt(bps int) Option {
|
||||||
return func(vt *VbanTxt) {
|
return func(vt *VbanTxt) {
|
||||||
|
defaultBps := BpsOpts[vt.packet.bpsIndex]
|
||||||
|
|
||||||
bpsIndex := indexOf(BpsOpts, bps)
|
bpsIndex := indexOf(BpsOpts, bps)
|
||||||
if bpsIndex == -1 {
|
if bpsIndex == -1 {
|
||||||
log.Warnf("invalid bps value %d, expected one of %v, defaulting to 0", bps, BpsOpts)
|
log.Warnf(
|
||||||
|
"invalid bps value %d, expected one of %v, defaulting to %d",
|
||||||
|
bps,
|
||||||
|
BpsOpts,
|
||||||
|
defaultBps,
|
||||||
|
)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
vt.packet.bpsIndex = bpsIndex
|
if bpsIndex > 255 {
|
||||||
|
log.Warnf("bps index %d too large for uint8, defaulting to %d", bpsIndex, defaultBps)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
vt.packet.bpsIndex = uint8(bpsIndex)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithChannel is a functional option to set the channel for {VbanTxt}.packet.
|
// WithChannel is a functional option to set the channel for {VbanTxt}.packet.
|
||||||
func WithChannel(channel int) Option {
|
func WithChannel(channel int) Option {
|
||||||
return func(vt *VbanTxt) {
|
return func(vt *VbanTxt) {
|
||||||
vt.packet.channel = channel
|
if channel < 0 || channel > 255 {
|
||||||
|
log.Warnf(
|
||||||
|
"channel value %d out of range [0,255], defaulting to %d",
|
||||||
|
channel,
|
||||||
|
vt.packet.channel,
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
vt.packet.channel = uint8(channel)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
54
packet.go
54
packet.go
@ -3,6 +3,7 @@ package vbantxt
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"github.com/charmbracelet/log"
|
"github.com/charmbracelet/log"
|
||||||
)
|
)
|
||||||
@ -21,25 +22,39 @@ var BpsOpts = []int{
|
|||||||
}
|
}
|
||||||
|
|
||||||
type packet struct {
|
type packet struct {
|
||||||
streamname []byte
|
streamname [streamNameSz]byte
|
||||||
bpsIndex int
|
bpsIndex uint8
|
||||||
channel int
|
channel uint8
|
||||||
framecounter []byte
|
framecounter uint32
|
||||||
hbuf *bytes.Buffer
|
hbuf *bytes.Buffer
|
||||||
}
|
}
|
||||||
|
|
||||||
// newPacket returns a packet struct with default values, framecounter at 0.
|
// newPacket creates a new packet with the given stream name and default values for other fields.
|
||||||
func newPacket(streamname string) packet {
|
// It validates the stream name length and ensures the default baud rate is present in BpsOpts.
|
||||||
streamnameBuf := make([]byte, streamNameSz)
|
func newPacket(streamname string) (packet, error) {
|
||||||
copy(streamnameBuf, streamname)
|
if len(streamname) > streamNameSz {
|
||||||
|
return packet{}, fmt.Errorf(
|
||||||
|
"streamname too long: %d chars, max %d",
|
||||||
|
len(streamname),
|
||||||
|
streamNameSz,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
var streamnameBuf [streamNameSz]byte
|
||||||
|
copy(streamnameBuf[:], streamname)
|
||||||
|
|
||||||
|
bpsIndex := indexOf(BpsOpts, 256000)
|
||||||
|
if bpsIndex == -1 {
|
||||||
|
return packet{}, fmt.Errorf("default baud rate 256000 not found in BpsOpts")
|
||||||
|
}
|
||||||
|
|
||||||
return packet{
|
return packet{
|
||||||
streamname: streamnameBuf,
|
streamname: streamnameBuf,
|
||||||
bpsIndex: 0,
|
bpsIndex: uint8(bpsIndex),
|
||||||
channel: 0,
|
channel: 0,
|
||||||
framecounter: make([]byte, 4),
|
framecounter: 0,
|
||||||
hbuf: bytes.NewBuffer(make([]byte, headerSz)),
|
hbuf: bytes.NewBuffer(make([]byte, 0, headerSz)),
|
||||||
}
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// sr defines the samplerate for the request.
|
// sr defines the samplerate for the request.
|
||||||
@ -60,15 +75,20 @@ func (p *packet) header() []byte {
|
|||||||
p.hbuf.WriteByte(byte(0))
|
p.hbuf.WriteByte(byte(0))
|
||||||
p.hbuf.WriteByte(p.nbc())
|
p.hbuf.WriteByte(p.nbc())
|
||||||
p.hbuf.WriteByte(byte(0x10))
|
p.hbuf.WriteByte(byte(0x10))
|
||||||
p.hbuf.Write(p.streamname)
|
p.hbuf.Write(p.streamname[:])
|
||||||
p.hbuf.Write(p.framecounter)
|
|
||||||
|
var frameBytes [4]byte
|
||||||
|
binary.LittleEndian.PutUint32(frameBytes[:], p.framecounter)
|
||||||
|
p.hbuf.Write(frameBytes[:])
|
||||||
|
|
||||||
return p.hbuf.Bytes()
|
return p.hbuf.Bytes()
|
||||||
}
|
}
|
||||||
|
|
||||||
// bumpFrameCounter increments the frame counter by 1.
|
// bumpFrameCounter increments the frame counter by 1.
|
||||||
|
// The uint32 will safely wrap to 0 after reaching max value (4,294,967,295),
|
||||||
|
// which is expected behaviour for network protocol sequence numbers.
|
||||||
func (p *packet) bumpFrameCounter() {
|
func (p *packet) bumpFrameCounter() {
|
||||||
x := binary.LittleEndian.Uint32(p.framecounter)
|
p.framecounter++
|
||||||
binary.LittleEndian.PutUint32(p.framecounter, x+1)
|
|
||||||
|
|
||||||
log.Debugf("framecounter: %d", x)
|
log.Debugf("framecounter: %d", p.framecounter)
|
||||||
}
|
}
|
||||||
|
|||||||
23
vbantxt.go
23
vbantxt.go
@ -8,7 +8,7 @@ import (
|
|||||||
|
|
||||||
// VbanTxt is used to send VBAN-TXT requests to a distant Voicemeeter/Matrix.
|
// VbanTxt is used to send VBAN-TXT requests to a distant Voicemeeter/Matrix.
|
||||||
type VbanTxt struct {
|
type VbanTxt struct {
|
||||||
udpConn io.WriteCloser
|
conn io.WriteCloser
|
||||||
packet packet
|
packet packet
|
||||||
ratelimit time.Duration
|
ratelimit time.Duration
|
||||||
}
|
}
|
||||||
@ -16,14 +16,19 @@ type VbanTxt struct {
|
|||||||
// New constructs a fully formed VbanTxt instance. This is the package's entry point.
|
// New constructs a fully formed VbanTxt instance. This is the package's entry point.
|
||||||
// It sets default values for it's fields and then runs the option functions.
|
// It sets default values for it's fields and then runs the option functions.
|
||||||
func New(host string, port int, streamname string, options ...Option) (*VbanTxt, error) {
|
func New(host string, port int, streamname string, options ...Option) (*VbanTxt, error) {
|
||||||
udpConn, err := newUDPConn(host, port)
|
conn, err := newUDPConn(host, port)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error creating UDP client for (%s:%d): %w", host, port, err)
|
return nil, fmt.Errorf("error creating UDP client for (%s:%d): %w", host, port, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
packet, err := newPacket(streamname)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error creating packet: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
vt := &VbanTxt{
|
vt := &VbanTxt{
|
||||||
udpConn: udpConn,
|
conn: conn,
|
||||||
packet: newPacket(streamname),
|
packet: packet,
|
||||||
ratelimit: time.Duration(20) * time.Millisecond,
|
ratelimit: time.Duration(20) * time.Millisecond,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -34,10 +39,10 @@ func New(host string, port int, streamname string, options ...Option) (*VbanTxt,
|
|||||||
return vt, nil
|
return vt, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send is resonsible for firing each VBAN-TXT request.
|
// Send is responsible for firing each VBAN-TXT request.
|
||||||
// It waits for {vt.ratelimit} time before returning.
|
// It waits for {vt.ratelimit} time before returning.
|
||||||
func (vt VbanTxt) Send(cmd string) error {
|
func (vt *VbanTxt) Send(cmd string) error {
|
||||||
_, err := vt.udpConn.Write(append(vt.packet.header(), []byte(cmd)...))
|
_, err := vt.conn.Write(append(vt.packet.header(), cmd...))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error sending command (%s): %w", cmd, err)
|
return fmt.Errorf("error sending command (%s): %w", cmd, err)
|
||||||
}
|
}
|
||||||
@ -50,8 +55,8 @@ func (vt VbanTxt) Send(cmd string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Close is responsible for closing the UDP Client connection.
|
// Close is responsible for closing the UDP Client connection.
|
||||||
func (vt VbanTxt) Close() error {
|
func (vt *VbanTxt) Close() error {
|
||||||
err := vt.udpConn.Close()
|
err := vt.conn.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error attempting to close UDP Client: %w", err)
|
return fmt.Errorf("error attempting to close UDP Client: %w", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,8 +7,9 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/onyx-and-iris/vbantxt"
|
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
|
"github.com/onyx-and-iris/vbantxt"
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:embed testdata/vm.txt
|
//go:embed testdata/vm.txt
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user