Skip to content

Commit

Permalink
move things around some more
Browse files Browse the repository at this point in the history
  • Loading branch information
mikew committed Oct 6, 2024
1 parent 24aa5b4 commit acb3504
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 66 deletions.
52 changes: 38 additions & 14 deletions src/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import (
"log"
"os"
"os/exec"
"runtime"
"strings"
"time"

"github.com/neovim/go-client/nvim"
"github.com/urfave/cli/v2"

"nvrh/src/nvim_helpers"
Expand Down Expand Up @@ -70,8 +72,8 @@ var CliClientOpenCommand = cli.Command{
nv.Close()
}()

nv.RegisterHandler("tunnel-port", nvim_helpers.MakeTunnelHandler(server))
nv.RegisterHandler("open-url", nvim_helpers.HandleOpenUrl)
nv.RegisterHandler("tunnel-port", ssh_helpers.MakeRpcTunnelHandler(server))
nv.RegisterHandler("open-url", RpcHandleOpenUrl)

batch := nv.NewBatch()

Expand All @@ -88,23 +90,23 @@ var CliClientOpenCommand = cli.Command{
// Prepare the browser script.
var output any
batch.ExecLua(`
local browser_script_path, socket_path, channel_id = ...
local browser_script_path, socket_path, channel_id = ...
local script_contents = [[
#!/bin/sh
local script_contents = [[
#!/bin/sh
SOCKET_PATH="%s"
CHANNEL_ID="%s"
SOCKET_PATH="%s"
CHANNEL_ID="%s"
exec nvim --server "$SOCKET_PATH" --remote-expr "rpcnotify(str2nr($CHANNEL_ID), 'open-url', ['$1'])" > /dev/null
]]
script_contents = string.format(script_contents, socket_path, channel_id)
exec nvim --server "$SOCKET_PATH" --remote-expr "rpcnotify(str2nr($CHANNEL_ID), 'open-url', ['$1'])" > /dev/null
]]
script_contents = string.format(script_contents, socket_path, channel_id)
vim.fn.writefile(vim.fn.split(script_contents, '\n'), browser_script_path)
os.execute('chmod +x ' .. browser_script_path)
vim.fn.writefile(vim.fn.split(script_contents, '\n'), browser_script_path)
os.execute('chmod +x ' .. browser_script_path)
return true
`, &output, browserScriptPath, socketPath, nv.ChannelID())
return true
`, &output, browserScriptPath, socketPath, nv.ChannelID())

if err := batch.Execute(); err != nil {
log.Fatalf("Error while preparing remote nvim: %v", err)
Expand Down Expand Up @@ -143,3 +145,25 @@ func startLocalEditor(socketPath string, args []string) {
return
}
}

func RpcHandleOpenUrl(v *nvim.Nvim, args []string) {
goos := runtime.GOOS
url := args[0]

// if url == "" || !strings.HasPrefix(url, "http://") || !strings.HasPrefix(url, "https://") {
// log.Printf("Invalid url: %s", url)
// return
// }

log.Printf("Opening url: %s", url)

if goos == "darwin" {
exec.Command("open", url).Run()
} else if goos == "linux" {
exec.Command("xdg-open", url).Run()
} else if goos == "windows" {
exec.Command("start", "", url).Run()
} else {
log.Printf("Don't know how to open url on %s", goos)
}
}
52 changes: 0 additions & 52 deletions src/nvim_helpers/main.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package nvim_helpers

import (
"fmt"
"log"
"os/exec"
"runtime"
"time"

"github.com/neovim/go-client/nvim"
Expand All @@ -29,51 +25,3 @@ func WaitForNvim(socketPath string) (*nvim.Nvim, error) {

// return nil, errors.New("Timed out waiting for nvim")
}

func HandleOpenUrl(v *nvim.Nvim, args []string) {
goos := runtime.GOOS
url := args[0]

// if url == "" || !strings.HasPrefix(url, "http://") || !strings.HasPrefix(url, "https://") {
// log.Printf("Invalid url: %s", url)
// return
// }

log.Printf("Opening url: %s", url)

if goos == "darwin" {
exec.Command("open", url).Run()
} else if goos == "linux" {
exec.Command("xdg-open", url).Run()
} else if goos == "windows" {
exec.Command("start", "", url).Run()
} else {
log.Printf("Don't know how to open url on %s", goos)
}
}

func MakeTunnelHandler(server string) func(*nvim.Nvim, []string) {
return func(v *nvim.Nvim, args []string) {
go func() {
log.Printf("Tunneling %s:%s", server, args[0])

sshCommand := exec.Command(
"ssh",
"-NL",
fmt.Sprintf("%s:0.0.0.0:%s", args[0], args[0]),
server,
)

if err := sshCommand.Start(); err != nil {
log.Printf("Error starting command: %v", err)
return
}

defer sshCommand.Process.Kill()

if err := sshCommand.Wait(); err != nil {
log.Printf("Error waiting for command: %v", err)
}
}()
}
}
28 changes: 28 additions & 0 deletions src/ssh_helpers/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"log"
"os/exec"
"strings"

"github.com/neovim/go-client/nvim"
)

func StartRemoteNvim(server string, socketPath string, directory string, envPairs []string) {
Expand Down Expand Up @@ -66,3 +68,29 @@ func buildRemoteCommand(socketPath string, directory string, envPairs []string)
directory,
)
}

func MakeRpcTunnelHandler(server string) func(*nvim.Nvim, []string) {
return func(v *nvim.Nvim, args []string) {
go func() {
log.Printf("Tunneling %s:%s", server, args[0])

sshCommand := exec.Command(
"ssh",
"-NL",
fmt.Sprintf("%s:0.0.0.0:%s", args[0], args[0]),
server,
)

if err := sshCommand.Start(); err != nil {
log.Printf("Error starting command: %v", err)
return
}

defer sshCommand.Process.Kill()

if err := sshCommand.Wait(); err != nil {
log.Printf("Error waiting for command: %v", err)
}
}()
}
}

0 comments on commit acb3504

Please sign in to comment.