-
Notifications
You must be signed in to change notification settings - Fork 2
/
use-package-secrets.el
101 lines (79 loc) · 3.18 KB
/
use-package-secrets.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
;;; use-package-secrets.el --- A :secret keyword for use-package -*- lexical-binding: t -*-
;; Homepage: https://github.com/a13/use-package-secrets
;; Version: 0.0.2
;; Package-Requires: ((emacs "25.1") (use-package "2.1") (dash "2.11"))
;; This file 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, or (at your option)
;; any later version.
;; This program 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.
;; For a full copy of the GNU General Public License
;; see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; A package to advice .el(.gpg) file loadind to commands.
;;; Code:
(require 'subr-x)
(require 'use-package)
(require 'dash)
(defvar use-package-secrets--files nil
"List of loaded secret files.")
(defgroup use-package-secrets nil
":secret keyword"
:group 'use-package)
(defcustom use-package-secrets-directories nil
"Paths to secrets storage."
:group 'use-package-secrets
:type '(repeat directory))
(defun use-package-secrets-file-exists-p (file &optional directory)
"Check if FILE exists in DIRECTORY and return its full name."
(let ((filename (expand-file-name file directory)))
(and (file-exists-p filename)
filename)))
(defun use-package-secrets-locate-file (file)
"Try to locate FILE."
(-some
(apply-partially #'use-package-secrets-file-exists-p file)
(cons nil use-package-secrets-directories)))
(defun use-package-secrets-load-files (&rest files)
"Try to load secret FILES."
(dolist (file files)
(unless (member file use-package-secrets--files)
(if-let ((path (use-package-secrets-locate-file file)))
(progn
;; TODO: add support to non-lisp password files
(load-file path)
(add-to-list 'use-package-secrets--files file))
(warn "file %s not found, skipped" file)))))
(defun use-package-normalize/:secret (_name keyword args)
(use-package-as-one (symbol-name keyword) args
(lambda (_label arg)
(cond
((and (listp arg) (stringp (car arg)))
arg)
(t (list arg))))))
(defun use-package-handler/:secret (name _keyword arg rest state)
(let ((body (use-package-process-keywords name rest state)))
(use-package-concat
(mapcar #'(lambda (secret-arg)
`,(pcase secret-arg
(`(,fn . ,files)
`(define-advice ,fn
(:before (&optional _args))
(use-package-secrets-load-files ,@files)))
(file
`(use-package-secrets-load-files ,file))))
arg)
body)))
(defun use-package-secrets-find-file ()
"Find secret file."
(interactive)
(find-file (use-package-secrets-locate-file
(completing-read
"Find secret file: "
use-package-secrets--files))))
(add-to-list 'use-package-keywords :secret t)
(provide 'use-package-secrets)
;;; use-package-secrets.el ends here