我想在初始化脚本中定义一个登录功能,但是我不想对我的登录凭证进行硬编码。我认为一个不错的解决方法是让我的初始化脚本从本地文件中读取我的登录凭据,并将这些值另存为变量。这样,我可以从git索引中排除该文件,从而确保我的登录凭据安全。
关于这种方法或将参数设置为文件中定义的值的方法有什么建议吗?
例如,我想在我的内容中使用以下内容init.el
:
;; Set up our login variables here:
(setq file-location "~/.emacs.d/.login")
(setq erc-username "default-name")
(setq erc-password "default-password")
(setq erc-url "default-url")
(setq erc-port "default-port")
(defun read-lines (filePath)
"Return a list of lines of a file at filePath."
(with-temp-buffer
(insert-file-contents filePath)
(split-string (buffer-string) "\n" t)))
(if (file-exists-p file-location)
(progn (setq login-credentials (read-lines file-location))
(setq erc-username (nth 0 login-credentials))
(setq erc-password (nth 1 login-credentials))
(setq erc-url (nth 2 login-credentials))
(setq erc-port (nth 3 login-credentials)))
(message "No ERC login credentials provided. Please add login credentials as '<username>\n<password>\n<url>\n<port>' in ~/.emacs.d/.login to activate ERC mode."))
;; These message the values from my file correctly.
;; Everything up to this point works as expected
(message erc-username)
(message erc-password)
(message erc-url)
(message erc-port)
;; Use our login variables here
;; This doesn't work because the 'quote' function prevents evaluation of my variables, and a 'backquote' did not resolve it either
(custom-set-variables
;; custom-set-variables was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(markdown-command "/usr/bin/pandoc")
'(tls-program (quote ("openssl s_client -connect %h:%p -no_ssl2 -ign_eof -CAfile ~/.ssl/spi_ca.pem -cert ~/.ssl/znc.pem")))
'(znc-servers (quote ((,erc-url ,erc-port t ((irc\.freenode\.net ,erc-username ,erc-password)))))))
(custom-set-faces
;; custom-set-faces was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
)
请注意,我的示例在此处使用了znc.el
模块。我修改从Emacs的CONFIGS导致自动生成的代码和。M-x customize-group RET znc RET
M-x customize-variable RET tls-program RET
上面的代码的问题是变量没有加载到我custom-set-variables
上面的函数中。从文件中加载适当的值似乎可以正常工作,但是我似乎无法将它们用作参数。我认为这与quote
功能有关,该功能无法评估其内容。我尝试使用“反引号”(,
)进行强制评估,但它也不起作用。任何修复此错误或提供另一种方法的建议都将非常有帮助。