feat(cli): add :dump pseudo command

And fix a void-variable doom-cli--dump error accidentally introduced in
d231755.

Ref: d231755bdf
This commit is contained in:
Henrik Lissner 2022-06-21 00:31:01 +02:00
parent 19ce459138
commit 0511445339
No known key found for this signature in database
GPG key ID: B60957CA074D39A3
2 changed files with 117 additions and 104 deletions

View file

@ -620,8 +620,6 @@ executable context."
(cond ((null (or command (doom-cli-get (list prefix) t)))
(signal 'doom-cli-invalid-prefix-error (list prefix)))
(doom-cli--dump (doom-cli--dump (doom-cli-find command)))
((doom-cli-context-meta-p context)
(pcase (doom-cli-context-meta-p context)
("--version"
@ -1227,12 +1225,6 @@ ARGS are options passed to less. If DOOMPAGER is set, ARGS are ignored."
(dolist (key (hash-table-keys doom-cli--table))
(doom-cli-load (gethash key doom-cli--table))))
(defun doom-cli--dump (&optional obj)
(let (kill-emacs-hook)
(prin1 obj)
(terpri)
(kill-emacs 0)))
;;
;;; DSL
@ -1263,6 +1255,18 @@ COMMANDSPEC may be prefixed with any of these special keywords:
A special handler, executed when help documentation is requested for a
command. E.g. 'doom help foo' or 'doom foo --help' will call (:help foo).
You can define your own global :help handler, or one for a specific command.
:dump COMMAND...
A special handler, executed when the __DOOMDUMP environment variable is set.
You can define one for a specific COMMAND, or omit it to redefine the
catch-all :dump handler.
The default implementation (living in core/core-cli.el) will either:
a) Dump to stdout a list of `doom-cli' structs for the commands and pseudo
commands that would've been executed had __DOOMDUMP not been set.
b) Or, given only \"-\" as an argument, dump all of `doom-cli--table' to
stdout. This table contains all known `doom-cli's (after loading
autoloaded ones).
To interpolate values into COMMANDSPEC (e.g. to dynamically generate commands),
use the comma operator:
@ -1635,12 +1639,8 @@ Once done, this function kills Emacs gracefully and writes output to log files
errors to `doom-cli-error-file')."
(when doom-cli--context
(error "Cannot nest `run!' calls"))
(let ((args (flatten-list args)))
(if (and doom-cli--dump (equal args '("-")))
(doom-cli--dump
(progn (doom-cli-load-all)
(hash-table-values doom-cli--table)))
(letf! ((context (doom-cli-context-create :prefix prefix))
(letf! ((args (flatten-list args))
(context (doom-cli-context-create :prefix prefix :whole args))
(doom-cli--context context)
(write-logs-fn (doom-partial #'doom-cli--output-write-logs-h context))
(show-benchmark-fn (doom-partial #'doom-cli--output-benchmark-h context))
@ -1660,9 +1660,10 @@ errors to `doom-cli-error-file')."
(doom-log "doom-cli-run: %s" command-line-args)
(doom-cli--exit
(condition-case e
(let ((context
(let* ((args (cons (if (getenv "__DOOMDUMP") :dump prefix) args))
(context
(or (doom-cli-context-restore (getenv "__DOOMCONTEXT") context)
(doom-cli-context-parse (cons prefix args) context))))
(doom-cli-context-parse args context))))
(run-hook-with-args 'doom-cli-before-run-functions context)
(let ((result (doom-cli-context-execute context)))
(run-hook-with-args 'doom-cli-after-run-functions context result))
@ -1728,7 +1729,7 @@ errors to `doom-cli-error-file')."
(print! (red "Error: %s") (cadr e))
(print! "\nAborting...")
3))
context)))))
context)))
(defalias 'sh! #'doom-call-process)

View file

@ -105,9 +105,21 @@
;; Load standard :help and :version handlers.
(load! "cli/help")
(defcli! (:root :dump) (&args commands)
;; When __DOOMDUMP is set, doomscripts trigger this special handler.
(defcli! (:root :dump)
((pretty? ("--pretty") "Pretty print output")
&context context
&args commands)
"Dump metadata to stdout for other commands to read."
(doom-cli--dump (doom-cli-find commands)))
(let* ((prefix (doom-cli-context-prefix context))
(command (cons prefix commands)))
(funcall (if pretty? #'pp #'prin1)
(cond ((equal commands '("-")) (hash-table-values doom-cli--table))
(commands (doom-cli-find command))
((doom-cli-find (list prefix)))))
(terpri)
;; Kill manually so we don't save output to logs.
(let (kill-emacs) (kill-emacs 0))))
(provide 'core-cli)
;;; core-cli.el ends here