Reputation: 320
UPDATE
I have accepted @Sean answer, with some small modifications.
(defun sudo-shell-command (buffer password command)
(let ((proc (start-process-shell-command
"*sudo*"
buffer
(concat "sudo bash -c "
(shell-quote-argument command)))))
;;; Added to @Sean answer to display the passed buffer
(display-buffer buffer '((display-buffer . nil)) nil)
(process-send-string proc password)
(process-send-string proc "\r")
(process-send-eof proc)))
(defun sudo-bundle-install (password)
(interactive (list (read-passwd "Sudo password for bundle install: ")))
(let ((default-directory (concat default-directory
"./fixtures/test-kitchen-mode-test-run/"))
;;; Added from accepted answer below by @sean
;;; need a buffer to display process in.
(generated-buffer (generate-new-buffer "*test-kitchen-test-setup*")))
(sudo-shell-command
;;; pass reference to generated process buffer by name.
;;; Need to add a defun to get the current test-kitchen buffer
;;; if it exists to use, but that is outside the scope of the question.
(buffer-name generated-buffer)
password
"bundle install; bundle exec berks install")
(clear-string password)))
Say I need to call a process in elisp, and that process requires sudo priveleges. Example, running Ruby's bundle install
:
(let ((generated-buffer (generate-new-buffer "*test-kitchen-test-setup*")))
(display-buffer generated-buffer '((display-buffer . nil)) nil)
(call-process-shell-command
(concat "cd " (concat default-directory "./fixtures/test-kitchen-mode-test-run")
"; sudo bundle install; sudo bundle exec berks install;")
nil generated-buffer t))
The bundle
command requires sudo
to install gems correctly. How can I call this shell command in elisp with sudo, enter the password, and still be able to display the results in the generated window?
Upvotes: 2
Views: 1085
Reputation: 29772
Here's a helper function that executes a single shell command as sudo with a supplied password:
(defun sudo-shell-command (buffer password command)
(let ((proc (start-process-shell-command
"*sudo*"
buffer
(concat "sudo bash -c "
(shell-quote-argument command)))))
(process-send-string proc password)
(process-send-string proc "\r")
(process-send-eof proc)))
You might apply it to your situation like so:
(defun sudo-bundle-install (password)
(interactive (list (read-passwd "Sudo password for bundle install: ")))
(let ((default-directory (concat default-directory
"./fixtures/test-kitchen-mode-test-run/")))
(sudo-shell-command
"*test-kitchen-test-setup*"
password
"bundle install; bundle exec berks install")
(clear-string password)))
Upvotes: 5
Reputation: 60004
You need to create a file with your password:
(defun my-run-command-under-sudo (password command &rest cpsc-args)
"Run COMMAND under sudo with your PASSWORD.
Other arguments are passed to `call-process-shell-command'
and should start with BUFFER (the output destination).
NB: this is _not_ secure: it creates a temp file with your password."
(let ((password-file (make-temp-file "elisp-sudo")))
(with-temp-file password-file (insert password))
(unwind-protect
(apply 'call-process-shell-command
(concat "sudo " command)
password-file cpsc-args)
(delete-file password-file))))
Now you can do:
(let ((default-directory (concat default-directory "./fixtures/test-kitchen-mode-test-run")))
(my-run-command-under-sudo "my-password" "bundle install" t)
(my-run-command-under-sudo "my-password" "bundle exec berks install" t))
Upvotes: 0