Skip to content

Commit cf09de5

Browse files
committed
verbose refactor but it works
1 parent 28e37d2 commit cf09de5

3 files changed

Lines changed: 215 additions & 99 deletions

File tree

src/client/main.go

Lines changed: 80 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,8 @@ var CliClientOpenCommand = cli.Command{
142142
RemoteEnv: c.StringSlice("server-env"),
143143
LocalEditor: c.StringSlice("local-editor"),
144144

145-
ShouldUsePorts: c.Bool("use-ports"),
146145

147-
RemoteSocketPath: fmt.Sprintf("/tmp/nvrh-socket-%s", sessionId),
148-
LocalSocketPath: filepath.Join(os.TempDir(), fmt.Sprintf("nvrh-socket-%s", sessionId)),
149-
AutomapPorts: c.Bool("enable-automap-ports"),
146+
AutomapPorts: c.Bool("enable-automap-ports"),
150147

151148
Debug: isDebug,
152149

@@ -157,6 +154,16 @@ var CliClientOpenCommand = cli.Command{
157154
SshArgs: c.StringSlice("ssh-arg"),
158155
}
159156

157+
shouldUsePorts := c.Bool("use-ports")
158+
remoteSocketPath := fmt.Sprintf("/tmp/nvrh-socket-%s", sessionId)
159+
localSocketPath := filepath.Join(os.TempDir(), fmt.Sprintf("nvrh-socket-%s", sessionId))
160+
161+
randomPort := getRandomPort()
162+
localPortNumber := randomPort
163+
remotePortNumber := randomPort
164+
165+
var tunnelInfo *ssh_tunnel_info.SshTunnelInfo
166+
160167
// Setup SSH client
161168
sshClient, sshClientErr := getSshClient(nvrhContext, endpoint, sshPath)
162169
if sshClientErr != nil {
@@ -172,14 +179,13 @@ var CliClientOpenCommand = cli.Command{
172179
slog.Info("Cleaning up")
173180
closeNvimSocket(nv, didClientFail)
174181
killAllCmds(nvrhContext.CommandsToKill)
175-
os.Remove(nvrhContext.LocalSocketPath)
182+
os.Remove(localSocketPath)
176183
if nvrhContext.SshClient != nil {
177184
nvrhContext.SshClient.Close()
178185
}
179186
}()
180187

181188
siDone := make(chan error, 1)
182-
randomPort := getRandomPort()
183189

184190
siTunnelInfo := &ssh_tunnel_info.SshTunnelInfo{
185191
Mode: "port",
@@ -191,8 +197,9 @@ var CliClientOpenCommand = cli.Command{
191197
// Start server info nvim instance.
192198
slog.Info("Starting server info nvim instance")
193199
go func() {
194-
// Not quoting here because Powershell doesn't like it, and we don't know
195-
// what shell we're using at this point.
200+
// Not quoting here because Powershell doesn't like it without the
201+
// preceding ampersand, and we don't know what shell we're using at this
202+
// point.
196203
nvimCmd := strings.Join(nvrhContext.NvimCmd, " ")
197204

198205
siDone <- nvrhContext.SshClient.Run(
@@ -201,7 +208,7 @@ var CliClientOpenCommand = cli.Command{
201208
)
202209
}()
203210

204-
// Grab server info.
211+
// Grab server info and potentially prepare Windows.
205212
go func() {
206213
siNv, err := nvim_helpers.WaitForNvim(ctx, siTunnelInfo)
207214

@@ -218,28 +225,43 @@ var CliClientOpenCommand = cli.Command{
218225
nvrhContext.ServerInfo = serverInfo
219226

220227
if nvrhContext.ServerInfo.Os == "windows" {
228+
shouldUsePorts = true
229+
221230
nvrhContext.WindowsLauncherPath = fmt.Sprintf(
222231
`%s\nvim-launcher-%s.bat`,
223232
nvrhContext.ServerInfo.Tmpdir,
224233
nvrhContext.SessionId,
225234
)
235+
236+
tunnelInfo = &ssh_tunnel_info.SshTunnelInfo{
237+
Mode: "port",
238+
LocalSocket: fmt.Sprintf("%d", localPortNumber),
239+
RemoteSocket: fmt.Sprintf("%d", remotePortNumber),
240+
Public: false,
241+
}
242+
226243
nvimLauncherScript := bridge_files.ReadFileWithoutError("shell/nvim-launcher.bat")
227244

228245
cdPortion := ""
229246
if nvrhContext.RemoteDirectory != "" {
230247
cdPortion = fmt.Sprintf("cd /d %s", nvrhContext.RemoteDirectory)
231248
}
232249

233-
envPortion := ""
234-
for _, envPair := range nvrhContext.RemoteEnv {
235-
envPortion += fmt.Sprintf("set %s\n", envPair)
236-
}
250+
envPortion := nvim_helpers.BuildRemoteEnvString(nvrhContext.RemoteEnv, "bat")
251+
252+
nvimCmd := nvim_helpers.BuildRemoteCommandString(
253+
nvrhContext.NvimCmd,
254+
"cmd",
255+
"",
256+
[]string{},
257+
tunnelInfo,
258+
)
237259

238260
nvimLauncherScript = fmt.Sprintf(
239261
nvimLauncherScript,
240262
envPortion,
241263
cdPortion,
242-
"nvim",
264+
nvimCmd,
243265
)
244266

245267
err := siNv.ExecLua(
@@ -274,45 +296,44 @@ var CliClientOpenCommand = cli.Command{
274296
}
275297

276298
// Prep with new server info.
277-
if nvrhContext.ServerInfo.Os == "windows" {
278-
nvrhContext.ShouldUsePorts = true
279-
}
280-
281-
if nvrhContext.ShouldUsePorts {
282-
randomPort := getRandomPort()
283-
284-
nvrhContext.LocalPortNumber = randomPort
285-
nvrhContext.RemotePortNumber = randomPort
286-
}
287-
288-
tunnelInfo := &ssh_tunnel_info.SshTunnelInfo{
289-
Mode: "unix",
290-
LocalSocket: nvrhContext.LocalSocketPath,
291-
RemoteSocket: nvrhContext.RemoteSocketPath,
292-
Public: false,
299+
// if nvrhContext.ServerInfo.Os == "windows" {
300+
// nvrhContext.ShouldUsePorts = true
301+
// }
302+
303+
// Even though this happens in the Windows Server path, we still need a
304+
// check here in case that path isn't hit.
305+
if tunnelInfo == nil {
306+
tunnelInfo = &ssh_tunnel_info.SshTunnelInfo{
307+
Mode: "unix",
308+
LocalSocket: localSocketPath,
309+
RemoteSocket: remoteSocketPath,
310+
Public: false,
311+
}
293312
}
294313

295-
if nvrhContext.ShouldUsePorts {
296-
tunnelInfo.SwitchToPorts(nvrhContext.LocalPortNumber, nvrhContext.RemotePortNumber)
314+
if shouldUsePorts {
315+
tunnelInfo.SwitchToPorts(localPortNumber, remotePortNumber)
297316
}
298317

299318
// Start remote nvim
300319
go func() {
301-
var cmdTemplate string
302-
if nvrhContext.ServerInfo.ShellName == "powershell" {
303-
cmdTemplate = `cd "%s"; %s`
304-
} else if nvrhContext.ServerInfo.ShellName == "cmd" {
305-
cmdTemplate = `cmd /c cd /d "%s" && %s`
320+
var nvimCommandString string
321+
if nvrhContext.ServerInfo.Os == "windows" {
322+
if nvrhContext.ServerInfo.ShellName == "bash" {
323+
nvimCommandString = fmt.Sprintf("/tmp/nvim-launcher-%s.bat", nvrhContext.SessionId)
324+
} else {
325+
nvimCommandString = nvrhContext.WindowsLauncherPath
326+
}
306327
} else {
307-
cmdTemplate = `exec "$SHELL" -i -c 'cd "%s" && %s'`
328+
nvimCommandString = nvim_helpers.BuildRemoteCommandString(
329+
nvrhContext.NvimCmd,
330+
nvrhContext.ServerInfo.ShellName,
331+
nvrhContext.RemoteDirectory,
332+
nvrhContext.RemoteEnv,
333+
tunnelInfo,
334+
)
308335
}
309336

310-
nvimCommandString := fmt.Sprintf(
311-
cmdTemplate,
312-
nvrhContext.RemoteDirectory,
313-
nvim_helpers.BuildRemoteCommandString(nvrhContext, tunnelInfo),
314-
)
315-
316337
slog.Info("Starting remote nvim", "nvimCommandString", nvimCommandString)
317338
done <- nvrhContext.SshClient.Run(nvimCommandString, tunnelInfo)
318339
// Call stop so WaitForNvim can exit.
@@ -443,10 +464,7 @@ var CliClientReconnectCommand = cli.Command{
443464
// RemoteEnv: c.StringSlice("server-env"),
444465
LocalEditor: c.StringSlice("local-editor"),
445466

446-
ShouldUsePorts: c.Bool("use-ports"),
447467

448-
RemoteSocketPath: fmt.Sprintf("/tmp/nvrh-socket-%s", sessionId),
449-
LocalSocketPath: filepath.Join(os.TempDir(), fmt.Sprintf("nvrh-socket-%s-%s", sessionId, randomId)),
450468
// TODO Handle mapping ports better with multiple clients.
451469
// AutomapPorts: c.Bool("enable-automap-ports"),
452470

@@ -459,16 +477,22 @@ var CliClientReconnectCommand = cli.Command{
459477
SshArgs: c.StringSlice("ssh-arg"),
460478
}
461479

480+
shouldUsePorts := c.Bool("use-ports")
481+
remoteSocketPath := fmt.Sprintf("/tmp/nvrh-socket-%s", sessionId)
482+
localSocketPath := filepath.Join(os.TempDir(), fmt.Sprintf("nvrh-socket-%s-%s", sessionId, randomId))
483+
484+
randomPort := getRandomPort()
485+
localPortNumber := randomPort
486+
remotePortNumber := randomPort
487+
462488
// Setup SSH client
463489
sshClient, sshClientErr := getSshClient(nvrhContext, endpoint, sshPath)
464490
if sshClientErr != nil {
465491
return sshClientErr
466492
}
467493
nvrhContext.SshClient = sshClient
468494

469-
if nvrhContext.ShouldUsePorts {
470-
randomPort := getRandomPort()
471-
495+
if shouldUsePorts {
472496
portNumberString := c.Args().Get(2)
473497
portNumber := 0
474498
if portNumberString != "" {
@@ -481,11 +505,8 @@ var CliClientReconnectCommand = cli.Command{
481505
portNumber = converted
482506
}
483507

484-
nvrhContext.LocalPortNumber = randomPort
485508
if portNumber != 0 {
486-
nvrhContext.RemotePortNumber = portNumber
487-
} else {
488-
nvrhContext.RemotePortNumber = randomPort
509+
remotePortNumber = portNumber
489510
}
490511
}
491512

@@ -496,7 +517,7 @@ var CliClientReconnectCommand = cli.Command{
496517
slog.Info("Cleaning up")
497518
closeNvimSocket(nv, false)
498519
killAllCmds(nvrhContext.CommandsToKill)
499-
os.Remove(nvrhContext.LocalSocketPath)
520+
os.Remove(localSocketPath)
500521
if nvrhContext.SshClient != nil {
501522
nvrhContext.SshClient.Close()
502523
}
@@ -505,13 +526,13 @@ var CliClientReconnectCommand = cli.Command{
505526
// Setup SSH tunnel
506527
tunnelInfo := &ssh_tunnel_info.SshTunnelInfo{
507528
Mode: "unix",
508-
LocalSocket: nvrhContext.LocalSocketPath,
509-
RemoteSocket: nvrhContext.RemoteSocketPath,
529+
LocalSocket: localSocketPath,
530+
RemoteSocket: remoteSocketPath,
510531
Public: false,
511532
}
512533

513-
if nvrhContext.ShouldUsePorts {
514-
tunnelInfo.SwitchToPorts(nvrhContext.LocalPortNumber, nvrhContext.RemotePortNumber)
534+
if shouldUsePorts {
535+
tunnelInfo.SwitchToPorts(localPortNumber, remotePortNumber)
515536
}
516537

517538
go func() {

src/context/main.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,6 @@ type NvrhContext struct {
1212
Endpoint *ssh_endpoint.SshEndpoint
1313
RemoteDirectory string
1414

15-
// Deprecated: Might not need to be on the context.
16-
LocalSocketPath string
17-
// Deprecated: Might not need to be on the context.
18-
RemoteSocketPath string
19-
// Deprecated: Might not need to be on the context.
20-
ShouldUsePorts bool
21-
// Deprecated: Might not need to be on the context.
22-
LocalPortNumber int
23-
// Deprecated: Might not need to be on the context.
24-
RemotePortNumber int
25-
2615
AutomapPorts bool
2716

2817
RemoteEnv []string

0 commit comments

Comments
 (0)