From acb35040afec9da6cd0c45860ce4b8918ecfdcfe Mon Sep 17 00:00:00 2001 From: Mike Wyatt Date: Sun, 6 Oct 2024 01:05:34 -0300 Subject: [PATCH] move things around some more --- src/client/main.go | 52 +++++++++++++++++++++++++++++----------- src/nvim_helpers/main.go | 52 ---------------------------------------- src/ssh_helpers/main.go | 28 ++++++++++++++++++++++ 3 files changed, 66 insertions(+), 66 deletions(-) diff --git a/src/client/main.go b/src/client/main.go index 864c8c4..874fe94 100644 --- a/src/client/main.go +++ b/src/client/main.go @@ -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" @@ -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() @@ -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) @@ -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) + } +} diff --git a/src/nvim_helpers/main.go b/src/nvim_helpers/main.go index 861c313..fbd3ee5 100644 --- a/src/nvim_helpers/main.go +++ b/src/nvim_helpers/main.go @@ -1,10 +1,6 @@ package nvim_helpers import ( - "fmt" - "log" - "os/exec" - "runtime" "time" "github.com/neovim/go-client/nvim" @@ -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) - } - }() - } -} diff --git a/src/ssh_helpers/main.go b/src/ssh_helpers/main.go index c317849..d1be30c 100644 --- a/src/ssh_helpers/main.go +++ b/src/ssh_helpers/main.go @@ -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) { @@ -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) + } + }() + } +}