diff --git a/src/client/main.go b/src/client/main.go index cd04aed..2c37f6d 100644 --- a/src/client/main.go +++ b/src/client/main.go @@ -26,6 +26,7 @@ import ( "golang.org/x/term" "nvrh/src/context" + "nvrh/src/logger" "nvrh/src/nvim_helpers" "nvrh/src/ssh_helpers" ) @@ -87,6 +88,9 @@ var CliClientOpenCommand = cli.Command{ }, Action: func(c *cli.Context) error { + isDebug := c.Bool("debug") + logger.PrepareLogger(isDebug) + // Prepare the context. sessionId := fmt.Sprintf("%d", time.Now().Unix()) @@ -100,7 +104,7 @@ var CliClientOpenCommand = cli.Command{ return err } - nvrhContext := context.NvrhContext{ + nvrhContext := &context.NvrhContext{ SessionId: sessionId, Server: c.Args().Get(0), RemoteDirectory: c.Args().Get(1), @@ -111,31 +115,16 @@ var CliClientOpenCommand = cli.Command{ ShouldUsePorts: c.Bool("use-ports"), RemoteSocketPath: fmt.Sprintf("/tmp/nvrh-socket-%s", sessionId), - LocalSocketPath: path.Join(os.TempDir(), fmt.Sprintf("nvrh-socket-%s", sessionId)), + LocalSocketPath: filepath.Join(os.TempDir(), fmt.Sprintf("nvrh-socket-%s", sessionId)), BrowserScriptPath: fmt.Sprintf("/tmp/nvrh-browser-%s", sessionId), SshPath: c.String("ssh-path"), - Debug: c.Bool("debug"), + Debug: isDebug, SshClient: sshClient, } - // Prepare the logger. - logLevel := slog.LevelInfo - if nvrhContext.Debug { - logLevel = slog.LevelDebug - } - log := slog.New(prettylog.New( - &slog.HandlerOptions{ - Level: logLevel, - AddSource: nvrhContext.Debug, - }, - prettylog.WithDestinationWriter(os.Stderr), - prettylog.WithColor(), - )) - slog.SetDefault(log) - if nvrhContext.ShouldUsePorts { min := 1025 max := 65535 @@ -157,27 +146,27 @@ var CliClientOpenCommand = cli.Command{ // Prepare remote instance. go func() { - go ssh_helpers.TunnelSshSocket(&nvrhContext, ssh_helpers.SshTunnelInfo{ + go ssh_helpers.TunnelSshSocket(nvrhContext, ssh_helpers.SshTunnelInfo{ Mode: "unix", LocalSocket: nvrhContext.LocalSocketPath, RemoteSocket: nvrhContext.RemoteSocketPath, }) - nvimCommandString := ssh_helpers.BuildRemoteCommandString(&nvrhContext) + nvimCommandString := ssh_helpers.BuildRemoteCommandString(nvrhContext) nvimCommandString = fmt.Sprintf("$SHELL -i -c '%s'", nvimCommandString) slog.Info("Starting remote nvim", "nvimCommandString", nvimCommandString) - if err := ssh_helpers.RunCommand(&nvrhContext, nvimCommandString); err != nil { + if err := ssh_helpers.RunCommand(nvrhContext, nvimCommandString); err != nil { doneChan <- err } - ssh_helpers.RunCommand(&nvrhContext, fmt.Sprintf("rm -f '%s'", nvrhContext.RemoteSocketPath)) - ssh_helpers.RunCommand(&nvrhContext, fmt.Sprintf("rm -f '%s'", nvrhContext.BrowserScriptPath)) + ssh_helpers.RunCommand(nvrhContext, fmt.Sprintf("rm -f '%s'", nvrhContext.RemoteSocketPath)) + ssh_helpers.RunCommand(nvrhContext, fmt.Sprintf("rm -f '%s'", nvrhContext.BrowserScriptPath)) }() // Prepare client instance. nvChan := make(chan *nvim.Nvim, 1) go func() { - nv, err := nvim_helpers.WaitForNvim(&nvrhContext) + nv, err := nvim_helpers.WaitForNvim(nvrhContext) if err != nil { slog.Error("Error connecting to nvim", "err", err) @@ -187,11 +176,11 @@ var CliClientOpenCommand = cli.Command{ slog.Info("Connected to nvim") nvChan <- nv - if err := prepareRemoteNvim(&nvrhContext, nv); err != nil { - slog.Error("Error preparing remote nvim", "err", err) + if err := prepareRemoteNvim(nvrhContext, nv); err != nil { + slog.Warn("Error preparing remote nvim", "err", err) } - clientCmd := BuildClientNvimCmd(&nvrhContext) + clientCmd := BuildClientNvimCmd(nvrhContext) if nvrhContext.Debug { clientCmd.Stdout = os.Stdout clientCmd.Stderr = os.Stderr @@ -230,6 +219,7 @@ var CliClientOpenCommand = cli.Command{ slog.Info("Closing nvrh") closeNvimSocket(nv) killAllCmds(nvrhContext.CommandsToKill) + os.Remove(nvrhContext.LocalSocketPath) if err != nil { return err @@ -269,8 +259,6 @@ func prepareRemoteNvim(nvrhContext *context.NvrhContext, nv *nvim.Nvim) error { batch.Command(fmt.Sprintf(`let $BROWSER="%s"`, nvrhContext.BrowserScriptPath)) // Add command to tunnel port. - // TODO use `vim.api.nvim_create_user_command`, and check to see if the - // port is already mapped somehow. batch.ExecLua(` vim.api.nvim_create_user_command( 'NvrhTunnelPort', @@ -353,7 +341,7 @@ func killAllCmds(cmds []*exec.Cmd) { slog.Debug("Killing command", "cmd", cmd.Args) if cmd.Process != nil { if err := cmd.Process.Kill(); err != nil { - slog.Error("Error killing command", "err", err) + slog.Warn("Error killing command", "err", err) } } } @@ -366,7 +354,7 @@ func closeNvimSocket(nv *nvim.Nvim) { slog.Info("Closing nvim") if err := nv.ExecLua("vim.cmd('qall!')", nil, nil); err != nil { - slog.Error("Error closing remote nvim", "err", err) + slog.Warn("Error closing remote nvim", "err", err) } nv.Close() } diff --git a/src/context/main.go b/src/context/main.go index eb6c285..74f0093 100644 --- a/src/context/main.go +++ b/src/context/main.go @@ -30,7 +30,7 @@ type NvrhContext struct { SshClient *ssh.Client } -func (nc NvrhContext) LocalSocketOrPort() string { +func (nc *NvrhContext) LocalSocketOrPort() string { if nc.ShouldUsePorts { // nvim-qt, at least on Windows (and might have something to do with // running in a VM) seems to prefer `127.0.0.1` to `0.0.0.0`, and I think @@ -41,9 +41,9 @@ func (nc NvrhContext) LocalSocketOrPort() string { return nc.LocalSocketPath } -func (nc NvrhContext) RemoteSocketOrPort() string { +func (nc *NvrhContext) RemoteSocketOrPort() string { if nc.ShouldUsePorts { - return fmt.Sprintf("0.0.0.0:%d", nc.PortNumber) + return fmt.Sprintf("127.0.0.1:%d", nc.PortNumber) } return nc.RemoteSocketPath diff --git a/src/logger/main.go b/src/logger/main.go new file mode 100644 index 0000000..882ad07 --- /dev/null +++ b/src/logger/main.go @@ -0,0 +1,27 @@ +package logger + +import ( + "log/slog" + "os" + + "github.com/dusted-go/logging/prettylog" +) + +func PrepareLogger(isDebug bool) { + logLevel := slog.LevelInfo + + if isDebug { + logLevel = slog.LevelDebug + } + + log := slog.New(prettylog.New( + &slog.HandlerOptions{ + Level: logLevel, + AddSource: isDebug, + }, + prettylog.WithDestinationWriter(os.Stderr), + prettylog.WithColor(), + )) + + slog.SetDefault(log) +} diff --git a/src/ssh_helpers/main.go b/src/ssh_helpers/main.go index 1a10fa0..ffb0f6e 100644 --- a/src/ssh_helpers/main.go +++ b/src/ssh_helpers/main.go @@ -20,7 +20,7 @@ func BuildRemoteNvimCmd(nvrhContext *context.NvrhContext) *exec.Cmd { tunnel := fmt.Sprintf("%s:%s", nvrhContext.LocalSocketPath, nvrhContext.RemoteSocketPath) if nvrhContext.ShouldUsePorts { - tunnel = fmt.Sprintf("%d:0.0.0.0:%d", nvrhContext.PortNumber, nvrhContext.PortNumber) + tunnel = fmt.Sprintf("%d:127.0.0.1:%d", nvrhContext.PortNumber, nvrhContext.PortNumber) } sshCommand := exec.Command( @@ -59,10 +59,13 @@ func BuildRemoteCommandString(nvrhContext *context.NvrhContext) string { } return fmt.Sprintf( - "%s nvim --headless --listen \"%s\" --cmd \"cd %s\"", + "%s nvim --headless --listen \"%s\" --cmd \"cd %s\"; rm -f \"%s\"; [ %t = true ] && rm -f \"%s\"", envPairsString, nvrhContext.RemoteSocketOrPort(), nvrhContext.RemoteDirectory, + nvrhContext.BrowserScriptPath, + !nvrhContext.ShouldUsePorts, + nvrhContext.RemoteSocketPath, ) }