;;; kkk-pass.el --- Pass Related Code                -*- lexical-binding: t; -*-

;; Copyright (C) 2023  Vinicius Teshima <vini.tes@pm.me>

;; Author: Vinicius Teshima <vini.tes@pm.me>
;; This program 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 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.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;; pass Related Code

;;; Code:
(defgroup kkk-pass nil
  "Initial Tweaks to Emacs."
  :group 'info)

(defconst kkk-pass--pass-cmd "pass"
  "The `pass` binary to be run.")

(defun kkk-pass-get (NAME &optional LINE)
  "Gets the password NAME.
Optionaly returns the line LINE.

NAME is a string.
LINE is a non zero positive integer."
  (catch 'early-return

    (unless (stringp NAME)
      (throw 'early-return "NAME has to be a string"))

    (when LINE
      (unless (integerp LINE)
	(throw 'early-return "LINE has to be an integer"))
      (when (= LINE 0)
	(throw 'early-return "LINE can't be zero"))
      (when (< LINE 0)
	(throw 'early-return "LINE can't be less than zero")))

    (let ((raw (shell-command-to-string
		 (format "%s %s" kkk-pass--pass-cmd NAME))))
      (if LINE
	  (nth (1- LINE) (string-lines raw t nil))
	raw))))

(provide 'kkk-pass)
;;; kkk-pass.el ends here
