hodu/main.go

87 lines
1.9 KiB
Go
Raw Normal View History

package main
2024-11-15 15:03:42 +00:00
import "flag"
import "fmt"
import "io"
import "os"
import "strings"
func main() {
var err error
2024-11-15 15:03:42 +00:00
var flgs *flag.FlagSet
if len(os.Args) < 2 {
goto wrong_usage
}
if strings.EqualFold(os.Args[1], "server") {
2024-11-15 15:03:42 +00:00
var la []string
2024-11-15 15:03:42 +00:00
la = make([]string, 0)
flgs = flag.NewFlagSet("", flag.ContinueOnError)
flgs.Func("listen-on", "specify a listening address", func(v string) error {
la = append(la, v)
return nil
})
flgs.SetOutput(io.Discard) // prevent usage output
2024-11-15 15:03:42 +00:00
err = flgs.Parse(os.Args[2:])
if err != nil {
fmt.Printf ("ERROR: %s\n", err.Error())
2024-11-15 15:03:42 +00:00
goto wrong_usage
}
if len(la) < 0 || flgs.NArg() > 0 {
goto wrong_usage
}
2024-11-15 15:03:42 +00:00
err = server_main(la)
if err != nil {
2024-11-15 15:03:42 +00:00
fmt.Fprintf(os.Stderr, "ERROR: server error - %s\n", err.Error())
goto oops
}
} else if strings.EqualFold(os.Args[1], "client") {
2024-11-15 15:03:42 +00:00
var la []string
var sa []string
la = make([]string, 0)
sa = make([]string, 0)
flgs = flag.NewFlagSet("", flag.ContinueOnError)
flgs.Func("listen-on", "specify a control channel address", func(v string) error {
la = append(la, v)
return nil
})
flgs.Func("server", "specify a server address", func(v string) error {
sa = append(sa, v)
return nil
})
flgs.SetOutput(io.Discard)
2024-11-15 15:03:42 +00:00
err = flgs.Parse(os.Args[2:])
if err != nil {
fmt.Printf ("ERROR: %s\n", err.Error())
goto wrong_usage
}
2024-11-15 15:03:42 +00:00
if len(la) != 1 || len(sa) != 1 || flgs.NArg() < 1 {
goto wrong_usage
}
err = client_main(la[0], sa[0], flgs.Args())
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: client error - %s\n", err.Error())
2024-11-15 15:03:42 +00:00
goto oops
}
} else {
goto wrong_usage
}
os.Exit(0)
wrong_usage:
2024-11-15 15:03:42 +00:00
fmt.Fprintf(os.Stderr, "USAGE: %s server --listen-on=addr:port\n", os.Args[0])
fmt.Fprintf(os.Stderr, " %s client --listen-on=addr:port --server=addr:port peer-addr:peer-port\n", os.Args[0])
os.Exit(1)
oops:
os.Exit(1)
}