-
Notifications
You must be signed in to change notification settings - Fork 38
/
vagrant-tramp.el
160 lines (132 loc) · 5.78 KB
/
vagrant-tramp.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
;;; vagrant-tramp.el --- Vagrant method for TRAMP
;; Copyright © 2016 The Vagrant-Tramp Contributors
;; Version: 0.6.0
;; Author: Doug MacEachern <[email protected]>
;; Ryan Prior <[email protected]> (rewrite)
;; This file is not part of GNU Emacs.
;; vagrant-tramp is free software: you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation, either version 3 of the
;; License, or (at your option) any later version.
;; This file is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this file. If not, see <http://www.gnu.org/licenses/>.
;; URL: https://github.com/dougm/vagrant-tramp
;; Keywords: vagrant
;; Package-Requires: ((dash "2.12.0"))
;;; Commentary:
;; This package adds a TRAMP method for Vagrant boxes.
;;; Code:
(require 'term)
(require 'tramp)
(require 'dash)
(defgroup vagrant-tramp nil
"TRAMP integration for Vagrant boxes."
:prefix "vagrant-tramp-"
:group 'tramp
:link '(url-link :tag "Github" "https://github.com/dougm/vagrant-tramp"))
(defconst vagrant-tramp-method "vagrant"
"Method to connect to vagrant boxes.")
(defconst vagrant-tramp-ssh
(shell-quote-argument
(executable-find (concat (file-name-directory
(or load-file-name
buffer-file-name))
"bin/vagrant-tramp-ssh")))
"TRAMP login helper script.")
(defun vagrant-tramp--all-boxes ()
"List of VMs per `vagrant global-status` as alists."
(let* ((status-cmd "vagrant global-status --machine-readable")
(status-raw (shell-command-to-string status-cmd))
(status-lines (split-string status-raw "\n"))
(separator-position (cl-position "------" status-lines :test 'cl-search))
(status-lines-dropped (-drop-last 3 (-drop (1+ separator-position) status-lines)))
(status-data-raw (--map (mapconcat 'identity
(-drop 4 (split-string it ",")) ",")
status-lines-dropped))
(status-data (--map (replace-regexp-in-string " " "" it) status-data-raw))
(status-groups (-split-on "" status-data))
(vm-attrs '(id name provider state dir)))
(--map (-zip vm-attrs it) status-groups)))
(defun vagrant-tramp--box-running-p (box)
"True if BOX is reported as running."
(string= (cdr (assoc 'state box)) "running"))
(defun vagrant-tramp--box-name (box)
"String representing BOX, using the Vagrantfile directory basename and the VM name (excluding 'default')."
(let ((name (cdr (assoc 'name box))))
(concat (file-name-base (cdr (assoc 'dir box)))
(unless (string= name "default")
(concat "--" name)))))
(defun vagrant-tramp--running-boxes ()
"List as per `vagrant-tramp--all-boxes', but excluding boxes not reported to be running."
(-filter 'vagrant-tramp--box-running-p
(vagrant-tramp--all-boxes)))
;;;###autoload
(defun vagrant-tramp--completions (&optional file)
"List for vagrant tramp completion. FILE argument is ignored."
(--map (list nil it)
(-map 'vagrant-tramp--box-name
(vagrant-tramp--running-boxes))))
(defun vagrant-tramp--get-or-prompt-for-box-name ()
"Get box name if only one box is running, otherwise prompt the user to choose."
(let* ((boxes (vagrant-tramp--running-boxes))
(names (-map 'vagrant-tramp--box-name boxes)))
(if (eq 1 (length names))
(car names)
(completing-read "vagrant ssh to: " names))))
;;;###autoload
(defun vagrant-tramp-shell (box-name)
"SSH into BOX-NAME using a `shell'."
(interactive (list (vagrant-tramp--get-or-prompt-for-box-name)))
(let ((buffer-name (concat "*vagrant shell:" box-name "*"))
(default-directory (format "/vagrant:%s:" box-name)))
(shell buffer-name)))
;;;###autoload
(defun vagrant-tramp-term (box-name)
"SSH into BOX-NAME using an `ansi-term'."
(interactive (list (vagrant-tramp--get-or-prompt-for-box-name)))
(let* ((name (concat "vagrant terminal:" box-name))
(buffer (get-buffer-create (concat "*" name "*"))))
(unless (term-check-proc buffer)
(let* ((boxes (vagrant-tramp--running-boxes))
(box (--first (string=
box-name
(vagrant-tramp--box-name it))
boxes))
(box-id (cdr (assoc 'id box))))
(with-current-buffer buffer
(cd (cdr (assoc 'dir box)))
(term-mode))
(term-exec buffer name "vagrant" nil (list "ssh" box-id))
(set-buffer buffer)
(term-mode)
(term-char-mode)))
(switch-to-buffer (concat "*" name "*"))))
;;;###autoload
(defun vagrant-tramp-add-method ()
"Add `vagrant-tramp-method' to `tramp-methods'."
(add-to-list 'tramp-methods
`(,vagrant-tramp-method
(tramp-login-program ,vagrant-tramp-ssh)
(tramp-login-args (("%h")))
(tramp-remote-shell "/bin/sh")
(tramp-remote-shell-args ("-c")))))
(defconst vagrant-tramp-completion-function-alist
'((vagrant-tramp--completions ""))
"Default list of (FUNCTION FILE) pairs to complete vagrant method.")
;;;###autoload
(define-obsolete-function-alias
'vagrant-tramp-enable
'vagrant-tramp-add-method
"2015-10-17")
;;;###autoload
(eval-after-load 'tramp
'(progn
(vagrant-tramp-add-method)
(tramp-set-completion-function
vagrant-tramp-method vagrant-tramp-completion-function-alist)))
(provide 'vagrant-tramp)
;;; vagrant-tramp.el ends here