Skip to content

Close an existing SSH tunnel before creating a new one with cider-connect #3537

Closed
@vemv

Description

@vemv

Discussed in #3536

Originally posted by JMacLulich October 18, 2023
I have had this problem for a long long time.

And with GPT-4 I finally got motivated enough to try and solve it.

My problem is when I cider-connect to a remote host via SSH it creates a tunnel -- that's fine.

Every subsequent connect to a different host uses the same tunnel which is tunneling to the first host.

It's so very annoying, I have to manually tear down the tunnel and re-establish it to connect to the right remote host.

I wanted a cider function that would look for existing SSH connections on the same port and tear them down so that a new one to the right remove host is established.

GPT-4 got me this far, but the var cider--connections is not defined (as is maybe others) and I don't know how ot proceed from here.

Any advice would be great (my emacs lisp is very basic, because I've actively tried to avoid it).

p.s. if possible I would love it to to host auto-completion the same way cider-connect already does, but cider-known-hosts seems to be nil.

(defun cider-connect-and-close-existing ()
  "Connect to a REPL, closing existing connections on the same port."
  (interactive)
  (if (boundp 'cider-known-hosts)
      (let ((hostname (completing-read "Host: " cider-known-hosts))
            (port (read-number "Port: " 7777))) ; Change the default port to 7777
        (let ((existing-connections (seq-filter
                                     (lambda (connection)
                                       (and (equal (cider--remote-host connection) hostname)
                                            (equal (cider--remote-port connection) port)))
                                     (cider--connections))))
          (dolist (conn existing-connections)
            (message "Closing existing connection to %s:%s" hostname port)
            (cider--close-connection conn)))
        (cider-connect-clj hostname port))
    (message "cider-known-hosts is not available, define your own list of known hosts.")))

(global-set-key (kbd "C-c C-o") 'cider-connect-and-close-existing)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions