Skip to content

Commit

Permalink
oh I've botched this merge
Browse files Browse the repository at this point in the history
  • Loading branch information
mikew committed Oct 21, 2024
1 parent 32d6136 commit 917a7ff
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 36 deletions.
50 changes: 19 additions & 31 deletions src/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"golang.org/x/term"

"nvrh/src/context"
"nvrh/src/logger"
"nvrh/src/nvim_helpers"
"nvrh/src/ssh_helpers"
)
Expand Down Expand Up @@ -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())

Expand All @@ -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),
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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)
}
}
}
Expand All @@ -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()
}
Expand Down
6 changes: 3 additions & 3 deletions src/context/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
27 changes: 27 additions & 0 deletions src/logger/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
7 changes: 5 additions & 2 deletions src/ssh_helpers/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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,
)
}

Expand Down

0 comments on commit 917a7ff

Please sign in to comment.