diff options
| author | Jeff Halter <868228+jhalter@users.noreply.github.com> | 2021-07-24 17:54:17 -0700 |
|---|---|---|
| committer | Jeff Halter <868228+jhalter@users.noreply.github.com> | 2021-07-24 17:54:17 -0700 |
| commit | 6988a0571d5d37ea0f38ee3e4066533158f608bc (patch) | |
| tree | 172010de54ea7e732d7daa836db659021dc8a933 /client/main.go | |
Initial squashed commit
Diffstat (limited to 'client/main.go')
| -rw-r--r-- | client/main.go | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/client/main.go b/client/main.go new file mode 100644 index 0000000..a761404 --- /dev/null +++ b/client/main.go @@ -0,0 +1,103 @@ +package main + +import ( + "context" + "flag" + "fmt" + hotline "github.com/jhalter/mobius" + "github.com/rivo/tview" + "go.uber.org/zap" + "go.uber.org/zap/zapcore" + "os" + "os/signal" + "syscall" + "time" +) + +//var defaultTrackerList = []string{ +// "hltracker.com:5498", +//} + +const connectTimeout = 3 * time.Second + +func main() { + _, cancelRoot := context.WithCancel(context.Background()) + + sigChan := make(chan os.Signal, 1) + signal.Notify(sigChan, syscall.SIGTERM, syscall.SIGINT, os.Interrupt) + + version := flag.Bool("version", false, "print version and exit") + logLevel := flag.String("log-level", "info", "Log level") + userName := flag.String("name", "unnamed", "User name") + //srvAddr := flag.String("server", "localhost:5500", "Hostname/Port of server") + //login := flag.String("login", "guest", "Login Name") + //pass := flag.String("password", "", "Login Password") + flag.Parse() + + if *version { + fmt.Printf("v%s\n", hotline.VERSION) + os.Exit(0) + } + + zapLvl, ok := zapLogLevel[*logLevel] + if !ok { + fmt.Printf("Invalid log level %s. Must be debug, info, warn, or error.\n", *logLevel) + os.Exit(0) + } + + // init DebugBuffer + db := &hotline.DebugBuffer{ + TextView: tview.NewTextView(), + } + + cores := []zapcore.Core{ + newDebugCore(zapLvl, db), + //newStderrCore(zapLvl), + } + l := zap.New(zapcore.NewTee(cores...)) + defer func() { _ = l.Sync() }() + logger := l.Sugar() + logger.Infow("Started Mobius client", "Version", hotline.VERSION) + + go func() { + sig := <-sigChan + logger.Infow("Stopping client", "signal", sig.String()) + cancelRoot() + }() + + client := hotline.NewClient(*userName, logger) + client.DebugBuf = db + client.UI.Start() + +} + +func newDebugCore(level zapcore.Level, db *hotline.DebugBuffer) zapcore.Core { + encoderCfg := zap.NewProductionEncoderConfig() + encoderCfg.TimeKey = "timestamp" + encoderCfg.EncodeTime = zapcore.ISO8601TimeEncoder + + return zapcore.NewCore( + zapcore.NewConsoleEncoder(encoderCfg), + zapcore.Lock(db), + level, + ) +} + +func newStderrCore(level zapcore.Level) zapcore.Core { + encoderCfg := zap.NewProductionEncoderConfig() + encoderCfg.TimeKey = "timestamp" + encoderCfg.EncodeTime = zapcore.ISO8601TimeEncoder + + return zapcore.NewCore( + zapcore.NewConsoleEncoder(encoderCfg), + zapcore.Lock(os.Stderr), + level, + ) +} + +var zapLogLevel = map[string]zapcore.Level{ + "debug": zap.DebugLevel, + "info": zap.InfoLevel, + "warn": zap.WarnLevel, + "error": zap.ErrorLevel, +} |