Skip to content

Commit

Permalink
Add customizable TRAMP method for container connections across Emacs …
Browse files Browse the repository at this point in the history
…shells

- Introduced `docker-container-tramp-method` to allow selection of
TRAMP method, enabling compatibility with Docker or Podman.

- Refactored all Emacs shells to use the new variable for container
connections, improving flexibility and cross-tool support.
  • Loading branch information
LionyxML committed Nov 7, 2024
1 parent 6f8bba0 commit 37bb23b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ Here is a list of other customizations you can set:
| docker-container-columns | Columns definition for containers | `/bin/sh` |
| docker-container-default-sort-key | Sort key for containers | `("Image")` |
| docker-container-shell-file-name | Shell to use when entering containers | `/bin/sh` |
| docker-container-tramp-method | Tramp prefix when connecting to containers | `docker` |
| docker-image-columns | Columns definition for images | Too complex to show |
| docker-image-default-sort-key | Sort key for images | `("Repository")` |
| docker-image-run-default-args | Base arguments to use for docker run | `("-i" "-t" "--rm")` |
Expand Down
31 changes: 18 additions & 13 deletions docker-container.el
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@
:group 'docker-container
:type 'string)

(defcustom docker-container-tramp-method "docker"
"TRAMP method to use for connecting to Docker containers."
:group 'docker-container
:type 'string)

(defcustom docker-container-default-sort-key '("Image" . nil)
"Sort key for docker containers.
Expand Down Expand Up @@ -163,7 +168,7 @@ string that transforms the displayed values in the column."
(defun docker-container-eshell (container)
"Open `eshell' in CONTAINER."
(interactive (list (docker-container-read-name)))
(let* ((container-address (format "docker:%s:/" container))
(let* ((container-address (format "%s:%s:/" docker-container-tramp-method container))
(file-prefix (let ((prefix (file-remote-p default-directory)))
(if prefix
(format "%s|" (s-chop-suffix ":" prefix))
Expand All @@ -176,18 +181,18 @@ string that transforms the displayed values in the column."
"Assert tramp docker support is available."
(unless (or (docker-utils-package-p 'docker-container)
(docker-utils-package-p 'docker-tramp))
(error "tramp docker support was not detected, try installing docker-tramp")))
(error "Tramp docker support was not detected, try installing docker-tramp")))

;;;###autoload (autoload 'docker-container-find-directory "docker-container" nil t)
(defun docker-container-find-directory (container directory)
"Inside CONTAINER open DIRECTORY."
(interactive
(let* ((container-name (docker-container-read-name))
(tramp-filename (read-directory-name "Directory: " (format "/docker:%s:/" container-name))))
(tramp-filename (read-directory-name "Directory: " (format "/%s:%s:/" docker-container-tramp-method container-name))))
(with-parsed-tramp-file-name tramp-filename nil
(list host localname))))
(docker-container-assert-tramp-docker)
(dired (format "/docker:%s:%s" container directory)))
(dired (format "/%s:%s:%s" docker-container-tramp-method container directory)))

(defalias 'docker-container-dired 'docker-container-find-directory)

Expand All @@ -196,11 +201,11 @@ string that transforms the displayed values in the column."
"Open FILE inside CONTAINER."
(interactive
(let* ((container-name (docker-container-read-name))
(tramp-filename (read-file-name "File: " (format "/docker:%s:/" container-name))))
(tramp-filename (read-file-name "File: " (format "/%s:%s:/" docker-container-tramp-method container-name))))
(with-parsed-tramp-file-name tramp-filename nil
(list host localname))))
(docker-container-assert-tramp-docker)
(find-file (format "/docker:%s:%s" container file)))
(find-file (format "/%s:%s:%s" docker-container-tramp-method container file)))

;;;###autoload (autoload 'docker-container-shell "docker-container" nil t)
(defun docker-container-shell (container &optional read-shell)
Expand All @@ -209,7 +214,7 @@ string that transforms the displayed values in the column."
(docker-container-read-name)
current-prefix-arg))
(let* ((shell-file-name (docker-container--read-shell read-shell))
(container-address (format "docker:%s:/" container))
(container-address (format "%s:%s:/" docker-container-tramp-method container))
(file-prefix (let ((prefix (file-remote-p default-directory)))
(if prefix
(format "%s|" (s-chop-suffix ":" prefix))
Expand All @@ -227,7 +232,7 @@ nil, ask the user for it."
current-prefix-arg))
(docker-container-assert-tramp-docker)
(let* ((shell-file-name (docker-container--read-shell read-shell))
(container-address (format "docker:%s:" container))
(container-address (format "%s:%s:" docker-container-tramp-method container))
(file-prefix (let ((prefix (file-remote-p default-directory)))
(if prefix
(format "%s|" (s-chop-suffix ":" prefix))
Expand All @@ -245,7 +250,7 @@ nil, ask the user for it."
"Open `vterm' in CONTAINER."
(interactive (list (docker-container-read-name)))
(if (fboundp 'vterm-other-window)
(let* ((container-address (format "docker:%s:/" container))
(let* ((container-address (format "%s:%s:/" docker-container-tramp-method container))
(file-prefix (let ((prefix (file-remote-p default-directory)))
(if prefix
(format "%s|" (s-chop-suffix ":" prefix))
Expand All @@ -261,7 +266,7 @@ default directory set to workdir."
(interactive (list
(docker-container-read-name)))
(docker-container-assert-tramp-docker)
(let* ((container-address (format "docker:%s:" container))
(let* ((container-address (format "%s:%s:" docker-container-tramp-method container))
(file-prefix (let ((prefix (file-remote-p default-directory)))
(if prefix
(format "%s|" (s-chop-suffix ":" prefix))
Expand All @@ -283,7 +288,7 @@ default directory set to workdir."
"Open `eat' in CONTAINER."
(interactive (list (docker-container-read-name)))
(if (fboundp 'eat-other-window)
(let* ((container-address (format "docker:%s:/" container))
(let* ((container-address (format "%s:%s:/" docker-container-tramp-method container))
(file-prefix (let ((prefix (file-remote-p default-directory)))
(if prefix
(format "%s|" (s-chop-suffix ":" prefix))
Expand All @@ -300,7 +305,7 @@ default directory set to workdir."
(interactive (list
(docker-container-read-name)))
(docker-container-assert-tramp-docker)
(let* ((container-address (format "docker:%s:" container))
(let* ((container-address (format "%s:%s:" docker-container-tramp-method container))
(file-prefix (let ((prefix (file-remote-p default-directory)))
(if prefix
(format "%s|" (s-chop-suffix ":" prefix))
Expand All @@ -314,7 +319,7 @@ default directory set to workdir."
(eat-buffer-name (format "*eat-env:%s" default-directory)))
(if (fboundp 'eat-other-window)
(eat-other-window)
(error "The eat package is not installed"))))
(error "The eat package is not installed")))

(defun docker-container-cp-from-selection (container-path host-path)
"Run \"docker cp\" from CONTAINER-PATH to HOST-PATH for selected container."
Expand Down

0 comments on commit 37bb23b

Please sign in to comment.