Add gist to encrypt files with dwim-shell-command

This commit is contained in:
Bram Schoenmakers 2023-04-06 19:15:28 +02:00
parent 509cccce46
commit 69fe8a2d8f
2 changed files with 57 additions and 0 deletions

View File

@ -496,6 +496,37 @@ It turns out it didn't work as well as I hoped, paredit steals the RET binding s
(advice-add #'tempel-insert :around #'my/store-region)
#+end_src
* dwim-shell-command to encrypt files with age :emacs:age:
#+begin_src elisp :tangle gists/dwim-shell-command-encrypt-with-age.el
(defconst my/cygwin-p (string-equal system-type "cygwin"))
(defconst my/age-pubkey "ageXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
(defun my/dwim-shell-command/convert-path-cygwin (template unix-path)
"Convert a Unix path to a Windows path.
This can be used as a :post-process-template of a
dwim-shell-command-on-marked-files function, where <<f>>
templates expand to Unix-like paths. However, a non-Cygwin binary
does not understand Unix paths so use cygpath to convert it."
(if my/cygwin-p
(let* ((command (format "cygpath -w %s" unix-path))
(cygwin-path (string-trim (shell-command-to-string command)))
(escaped-cygwin-path (string-replace "\\" "\\\\" cygwin-path)))
(string-replace unix-path escaped-cygwin-path template))
;; just return the template as-is
template))
(defun my/dwim-shell-command/age-encrypt ()
"Encrypt marked files with the age encryption tool."
(interactive)
(dwim-shell-command-on-marked-files "Age Encypt"
(format "age -e -r %s -o <<f>>.age <<f>>" my/age-pubkey)
:utils "age"
:post-process-template #'my/dwim-shell-command/convert-path-cygwin))
#+end_src
* Meta
** License

View File

@ -0,0 +1,26 @@
(defconst my/cygwin-p (string-equal system-type "cygwin"))
(defconst my/age-pubkey "ageXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
(defun my/dwim-shell-command/convert-path-cygwin (template unix-path)
"Convert a Unix path to a Windows path.
This can be used as a :post-process-template of a
dwim-shell-command-on-marked-files function, where <<f>>
templates expand to Unix-like paths. However, a non-Cygwin binary
does not understand Unix paths so use cygpath to convert it."
(if my/cygwin-p
(let* ((command (format "cygpath -w %s" unix-path))
(cygwin-path (string-trim (shell-command-to-string command)))
(escaped-cygwin-path (string-replace "\\" "\\\\" cygwin-path)))
(string-replace unix-path escaped-cygwin-path template))
;; just return the template as-is
template))
(defun my/dwim-shell-command/age-encrypt ()
"Encrypt marked files with the age encryption tool."
(interactive)
(dwim-shell-command-on-marked-files "Age Encypt"
(format "age -e -r %s -o <<f>>.age <<f>>" my/age-pubkey)
:utils "age"
:post-process-template #'my/dwim-shell-command/convert-path-cygwin))