Improve flag handling and server output

This commit is contained in:
Justin Hawkins 2022-01-16 22:36:34 +10:30
parent 8da4b60f28
commit 6437763ed9
2 changed files with 14 additions and 16 deletions

28
main.go
View File

@ -1,24 +1,23 @@
package main package main
import ( import (
"flag"
"fmt" "fmt"
"os" "os"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/mattn/go-isatty" "github.com/mattn/go-isatty"
"github.com/spf13/pflag" flag "github.com/spf13/pflag"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
func main() { func main() {
isServer := flag.Bool("s", false, "Run netgiv in server mode") isServer := flag.Bool("server", false, "Run netgiv in server mode")
// client mode flags // client mode flags
isList := flag.Bool("l", false, "Set if requesting a list") isList := flag.BoolP("list", "l", false, "Set if requesting a list")
isSend := flag.Bool("c", false, "sending stdin to netgiv server (copy)") isSend := flag.BoolP("copy", "c", false, "sending stdin to netgiv server (copy)")
isReceive := flag.Bool("p", false, "receive file from netgiv server to stdout (paste)") isReceive := flag.BoolP("paste", "p", false, "receive file from netgiv server to stdout (paste)")
debug := flag.Bool("debug", false, "turn on debug logging") debug := flag.Bool("debug", false, "turn on debug logging")
flag.String("address", "", "IP address/hostname of the netgiv server") flag.String("address", "", "IP address/hostname of the netgiv server")
@ -44,9 +43,8 @@ func main() {
} }
} }
pflag.CommandLine.AddGoFlagSet(flag.CommandLine) flag.Parse()
pflag.Parse() viper.BindPFlags(flag.CommandLine)
viper.BindPFlags(pflag.CommandLine)
viper.SetEnvPrefix("NETGIV") viper.SetEnvPrefix("NETGIV")
viper.BindEnv("authtoken") viper.BindEnv("authtoken")
@ -57,12 +55,12 @@ func main() {
address := viper.GetString("address") address := viper.GetString("address")
flag.Usage = func() { // flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s:\n", os.Args[0]) // fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s:\n", os.Args[0])
flag.PrintDefaults() // flag.PrintDefaults()
fmt.Printf("\nIf stdin or stdout is a pipe, %s will automatically choose an appropriate\n", os.Args[0]) // fmt.Printf("\nIf stdin or stdout is a pipe, %s will automatically choose an appropriate\n", os.Args[0])
fmt.Printf("copy (-c) or paste (-p) mode\n") // fmt.Printf("copy (-c) or paste (-p) mode\n")
} // }
if *helpConfig { if *helpConfig {
fmt.Print( fmt.Print(

View File

@ -38,7 +38,7 @@ var ngfs []NGF
var globalId uint32 var globalId uint32
func (s *Server) Run() { func (s *Server) Run() {
log.Debugf("starting server on :%d", s.port) log.Infof("starting server on :%d", s.port)
address := fmt.Sprintf(":%d", s.port) address := fmt.Sprintf(":%d", s.port)
networkAddress, _ := net.ResolveTCPAddr("tcp", address) networkAddress, _ := net.ResolveTCPAddr("tcp", address)