mirror of
https://github.com/onyx-and-iris/vbantxt.git
synced 2026-02-15 00:47:51 +00:00
add versionFromBuild() and sendCommands() to separate the logic in main() a little.
Flags now implements fmt.Stringer interface. import lint fix
This commit is contained in:
parent
b568767f86
commit
acac22f70e
@ -11,10 +11,11 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/charmbracelet/log"
|
||||
"github.com/onyx-and-iris/vbantxt"
|
||||
"github.com/peterbourgon/ff/v4"
|
||||
"github.com/peterbourgon/ff/v4/ffhelp"
|
||||
"github.com/peterbourgon/ff/v4/fftoml"
|
||||
|
||||
"github.com/onyx-and-iris/vbantxt"
|
||||
)
|
||||
|
||||
var version string // Version will be set at build time
|
||||
@ -32,8 +33,22 @@ type Flags struct {
|
||||
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) {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "Error: %s\n", err)
|
||||
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
@ -41,7 +56,7 @@ func main() {
|
||||
var flags 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.IntVar(&flags.Port, 'p', "port", 6980, "VBAN port")
|
||||
fs.StringVar(&flags.Streamname, 's', "streamname", "Command1", "VBAN stream name")
|
||||
@ -56,8 +71,20 @@ func main() {
|
||||
defaultConfigPath := filepath.Join(configDir, "vbantxt", "config.toml")
|
||||
|
||||
// Configuration file and logging flags
|
||||
fs.StringVar(&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.StringVar(
|
||||
&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")
|
||||
|
||||
err = ff.Parse(fs, os.Args[1:],
|
||||
@ -75,14 +102,7 @@ func main() {
|
||||
}
|
||||
|
||||
if flags.Version {
|
||||
if version == "" {
|
||||
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)
|
||||
fmt.Printf("vbantxt version: %s\n", versionFromBuild())
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
@ -92,7 +112,7 @@ func main() {
|
||||
}
|
||||
log.SetLevel(level)
|
||||
|
||||
log.Debugf("Loaded configuration: %+v", flags)
|
||||
log.Debugf("Loaded configuration: %s", flags.String())
|
||||
|
||||
client, closer, err := createClient(&flags)
|
||||
if err != nil {
|
||||
@ -100,12 +120,24 @@ func main() {
|
||||
}
|
||||
defer closer()
|
||||
|
||||
for _, arg := range fs.GetArgs() {
|
||||
err := client.Send(arg)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
commands := fs.GetArgs()
|
||||
if len(commands) == 0 {
|
||||
exitOnError(errors.New("no VBAN commands provided"))
|
||||
}
|
||||
|
||||
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.
|
||||
@ -129,3 +161,14 @@ func createClient(flags *Flags) (*vbantxt.VbanTxt, func(), error) {
|
||||
|
||||
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-runewidth v0.0.19 // 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/rivo/uniseg v0.4.7 // 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/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/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/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
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/muesli/termenv v0.16.0 h1:S5AlUN9dENB57rsbnkPyfdGuWIlkmzJjbFf0Tf5FWUc=
|
||||
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.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=
|
||||
github.com/pelletier/go-toml/v2 v2.0.9 h1:uH2qQXheeefCCkuBBSLi7jCiSmj3VRh2+Goq2N7Xxu0=
|
||||
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/go.mod h1:onQJUKipvCyFmZ1rIYwFAh1BhPOvftb1uhvSI7krNLc=
|
||||
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/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
|
||||
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/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
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/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
||||
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/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user