mirror of
https://github.com/onyx-and-iris/vbantxt.git
synced 2024-11-21 17:30:49 +00:00
host now defaults to "localhost"
loglevel flag now uses logrus constants (0 up to 6) config values are only applied if the corresponding flag wasn't passed isFlagPassed() added to util.go
This commit is contained in:
parent
31b188280d
commit
2bdabdae03
71
main.go
71
main.go
@ -3,7 +3,6 @@ package main
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"errors"
|
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
@ -44,20 +43,9 @@ type (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func setLogLevel(loglevel int) {
|
|
||||||
switch loglevel {
|
|
||||||
case 0:
|
|
||||||
log.SetLevel(log.WarnLevel)
|
|
||||||
case 1:
|
|
||||||
log.SetLevel(log.InfoLevel)
|
|
||||||
case 2:
|
|
||||||
log.SetLevel(log.DebugLevel)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
flag.StringVar(&host, "host", "", "vban host")
|
flag.StringVar(&host, "host", "localhost", "vban host")
|
||||||
flag.StringVar(&host, "h", "", "vban host (shorthand)")
|
flag.StringVar(&host, "h", "localhost", "vban host (shorthand)")
|
||||||
flag.IntVar(&port, "port", 6980, "vban server port")
|
flag.IntVar(&port, "port", 6980, "vban server port")
|
||||||
flag.IntVar(&port, "p", 6980, "vban server port (shorthand)")
|
flag.IntVar(&port, "p", 6980, "vban server port (shorthand)")
|
||||||
flag.StringVar(&streamname, "streamname", "Command1", "stream name for text requests")
|
flag.StringVar(&streamname, "streamname", "Command1", "stream name for text requests")
|
||||||
@ -68,11 +56,13 @@ func main() {
|
|||||||
flag.IntVar(&channel, "c", 0, "vban channel (shorthand)")
|
flag.IntVar(&channel, "c", 0, "vban channel (shorthand)")
|
||||||
flag.IntVar(&delay, "delay", 20, "delay between requests")
|
flag.IntVar(&delay, "delay", 20, "delay between requests")
|
||||||
flag.IntVar(&delay, "d", 20, "delay between requests (shorthand)")
|
flag.IntVar(&delay, "d", 20, "delay between requests (shorthand)")
|
||||||
flag.IntVar(&loglevel, "loglevel", 0, "log level")
|
flag.IntVar(&loglevel, "loglevel", int(log.WarnLevel), "log level")
|
||||||
flag.IntVar(&loglevel, "l", 0, "log level (shorthand)")
|
flag.IntVar(&loglevel, "l", int(log.WarnLevel), "log level (shorthand)")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
setLogLevel(loglevel)
|
if loglevel >= int(log.PanicLevel) && loglevel <= int(log.TraceLevel) {
|
||||||
|
log.SetLevel(log.Level(loglevel))
|
||||||
|
}
|
||||||
|
|
||||||
c, err := vbanConnect()
|
c, err := vbanConnect()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -84,52 +74,49 @@ func main() {
|
|||||||
for _, arg := range flag.Args() {
|
for _, arg := range flag.Args() {
|
||||||
err := send(c, header, arg)
|
err := send(c, header, arg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(err)
|
log.Error(err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// vbanConnect establishes a VBAN connection to remote host
|
// vbanConnect establishes a VBAN connection to remote host
|
||||||
func vbanConnect() (*net.UDPConn, error) {
|
func vbanConnect() (*net.UDPConn, error) {
|
||||||
if host == "" {
|
homeDir, err := os.UserHomeDir()
|
||||||
conn, err := connFromToml()
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
f := filepath.Join(homeDir, ".vbantxt_cli", "config.toml")
|
||||||
|
if _, err := os.Stat(f); err == nil {
|
||||||
|
conn, err := connFromToml(f)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
host = conn.Host
|
if !isFlagPassed("h") && !isFlagPassed("host") {
|
||||||
port = conn.Port
|
host = conn.Host
|
||||||
streamname = conn.Streamname
|
}
|
||||||
if host == "" {
|
if !isFlagPassed("p") && !isFlagPassed("port") {
|
||||||
err := errors.New("must provide a host with --host flag or config.toml")
|
port = conn.Port
|
||||||
return nil, err
|
}
|
||||||
|
if !isFlagPassed("s") && !isFlagPassed("streamname") {
|
||||||
|
streamname = conn.Streamname
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
CONNECT := fmt.Sprintf("%s:%d", host, port)
|
log.Debugf("Using values host: %s port: %d streamname: %s", host, port, streamname)
|
||||||
|
|
||||||
s, _ := net.ResolveUDPAddr("udp4", CONNECT)
|
s, _ := net.ResolveUDPAddr("udp4", fmt.Sprintf("%s:%d", host, port))
|
||||||
c, err := net.DialUDP("udp4", nil, s)
|
c, err := net.DialUDP("udp4", nil, s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
log.Info("Connected to ", c.RemoteAddr().String())
|
log.Infof("Connected to %s", c.RemoteAddr())
|
||||||
|
|
||||||
return c, nil
|
return c, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// connFromToml parses connection info from config.toml
|
// connFromToml parses connection info from config.toml
|
||||||
func connFromToml() (*connection, error) {
|
func connFromToml(f string) (*connection, error) {
|
||||||
homeDir, err := os.UserHomeDir()
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
f := filepath.Join(homeDir, ".vbantxt_cli", "config.toml")
|
|
||||||
if _, err := os.Stat(f); err != nil {
|
|
||||||
err := fmt.Errorf("unable to locate %s", f)
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var c config
|
var c config
|
||||||
_, err = toml.DecodeFile(f, &c.Connection)
|
_, err := toml.DecodeFile(f, &c.Connection)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -139,7 +126,7 @@ func connFromToml() (*connection, error) {
|
|||||||
|
|
||||||
// send sends a VBAN text request over UDP to remote host
|
// send sends a VBAN text request over UDP to remote host
|
||||||
func send(c *net.UDPConn, h *requestHeader, msg string) error {
|
func send(c *net.UDPConn, h *requestHeader, msg string) error {
|
||||||
log.Debug("Sending '", msg, "' to: ", c.RemoteAddr().String())
|
log.Debugf("Sending '%s' to: %s", msg, c.RemoteAddr())
|
||||||
data := []byte(msg)
|
data := []byte(msg)
|
||||||
_, err := c.Write(append(h.header(), data...))
|
_, err := c.Write(append(h.header(), data...))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
12
util.go
12
util.go
@ -1,5 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
|
import "flag"
|
||||||
|
|
||||||
// indexOf returns the index of an element in an array
|
// indexOf returns the index of an element in an array
|
||||||
func indexOf[T comparable](collection []T, e T) int {
|
func indexOf[T comparable](collection []T, e T) int {
|
||||||
for i, x := range collection {
|
for i, x := range collection {
|
||||||
@ -9,3 +11,13 @@ func indexOf[T comparable](collection []T, e T) int {
|
|||||||
}
|
}
|
||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isFlagPassed(name string) bool {
|
||||||
|
found := false
|
||||||
|
flag.Visit(func(f *flag.Flag) {
|
||||||
|
if f.Name == name {
|
||||||
|
found = true
|
||||||
|
}
|
||||||
|
})
|
||||||
|
return found
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user