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))) (cond ((null (or command (doom-cli-get (list prefix) t)))
(signal 'doom-cli-invalid-prefix-error (list prefix))) (signal 'doom-cli-invalid-prefix-error (list prefix)))
(doom-cli--dump (doom-cli--dump (doom-cli-find command)))
((doom-cli-context-meta-p context) ((doom-cli-context-meta-p context)
(pcase (doom-cli-context-meta-p context) (pcase (doom-cli-context-meta-p context)
("--version" ("--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)) (dolist (key (hash-table-keys doom-cli--table))
(doom-cli-load (gethash key 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 ;;; 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 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). 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. 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), To interpolate values into COMMANDSPEC (e.g. to dynamically generate commands),
use the comma operator: use the comma operator:
@ -1635,100 +1639,97 @@ Once done, this function kills Emacs gracefully and writes output to log files
errors to `doom-cli-error-file')." errors to `doom-cli-error-file')."
(when doom-cli--context (when doom-cli--context
(error "Cannot nest `run!' calls")) (error "Cannot nest `run!' calls"))
(let ((args (flatten-list args))) (letf! ((args (flatten-list args))
(if (and doom-cli--dump (equal args '("-"))) (context (doom-cli-context-create :prefix prefix :whole args))
(doom-cli--dump (doom-cli--context context)
(progn (doom-cli-load-all) (write-logs-fn (doom-partial #'doom-cli--output-write-logs-h context))
(hash-table-values doom-cli--table))) (show-benchmark-fn (doom-partial #'doom-cli--output-benchmark-h context))
(letf! ((context (doom-cli-context-create :prefix prefix)) ;; Write more user-friendly backtraces
(doom-cli--context context) (debugger (doom-rpartial #'doom-cli-debugger context))
(write-logs-fn (doom-partial #'doom-cli--output-write-logs-h context)) (debug-on-error t)
(show-benchmark-fn (doom-partial #'doom-cli--output-benchmark-h context)) ;; Clone output to stdout/stderr buffers for logging.
;; Write more user-friendly backtraces (standard-output (doom-rpartial #'doom-cli--output context))
(debugger (doom-rpartial #'doom-cli-debugger context)) (#'message (doom-partial #'doom-cli--redirect-output-a context)))
(debug-on-error t) (doom-log "Starting!")
;; Clone output to stdout/stderr buffers for logging. (add-hook 'kill-emacs-hook show-benchmark-fn 94)
(standard-output (doom-rpartial #'doom-cli--output context)) (add-hook 'kill-emacs-hook write-logs-fn 95)
(#'message (doom-partial #'doom-cli--redirect-output-a context))) (when (doom-cli-context-pipe-p context :out t)
(doom-log "Starting!") (setq doom-print-backend nil))
(add-hook 'kill-emacs-hook show-benchmark-fn 94) (when (doom-cli-context-pipe-p context :in)
(add-hook 'kill-emacs-hook write-logs-fn 95) (doom-cli--output-read-stdin (doom-cli-context-stdin context)))
(when (doom-cli-context-pipe-p context :out t) (doom-log "doom-cli-run: %s" command-line-args)
(setq doom-print-backend nil)) (doom-cli--exit
(when (doom-cli-context-pipe-p context :in) (condition-case e
(doom-cli--output-read-stdin (doom-cli-context-stdin context))) (let* ((args (cons (if (getenv "__DOOMDUMP") :dump prefix) args))
(doom-log "doom-cli-run: %s" command-line-args) (context
(doom-cli--exit (or (doom-cli-context-restore (getenv "__DOOMCONTEXT") context)
(condition-case e (doom-cli-context-parse args context))))
(let ((context (run-hook-with-args 'doom-cli-before-run-functions context)
(or (doom-cli-context-restore (getenv "__DOOMCONTEXT") context) (let ((result (doom-cli-context-execute context)))
(doom-cli-context-parse (cons prefix args) context)))) (run-hook-with-args 'doom-cli-after-run-functions context result))
(run-hook-with-args 'doom-cli-before-run-functions context) 0)
(let ((result (doom-cli-context-execute context))) (doom-cli-wrong-number-of-arguments-error
(run-hook-with-args 'doom-cli-after-run-functions context result)) (pcase-let ((`(,command ,flag ,args ,min ,max) (cdr e)))
0) (print! (red "Error: %S expected %s argument%s, but got %d")
(doom-cli-wrong-number-of-arguments-error (or flag (doom-cli-command-string
(pcase-let ((`(,command ,flag ,args ,min ,max) (cdr e))) (if (keywordp (car command))
(print! (red "Error: %S expected %s argument%s, but got %d") command
(or flag (doom-cli-command-string (cdr command))))
(if (keywordp (car command)) (if (or (= min max)
command (= max most-positive-fixnum))
(cdr command)))) min
(if (or (= min max) (format "%d-%d" min max))
(= max most-positive-fixnum)) (if (or (= min 0) (> min 1)) "s" "")
min (length args))
(format "%d-%d" min max)) (doom-cli-call `(:help "--synopsis" "--postamble" ,@(cdr (doom-cli--command context))) context e))
(if (or (= min 0) (> min 1)) "s" "") 5)
(length args)) (doom-cli-unrecognized-option-error
(doom-cli-call `(:help "--synopsis" "--postamble" ,@(cdr (doom-cli--command context))) context e)) (print! (red "Error: unknown option %s") (cadr e))
5) (doom-cli-call `(:help "--synopsis" "--postamble" ,@(cdr (doom-cli--command context))) context e)
(doom-cli-unrecognized-option-error 5)
(print! (red "Error: unknown option %s") (cadr e)) (doom-cli-invalid-option-error
(doom-cli-call `(:help "--synopsis" "--postamble" ,@(cdr (doom-cli--command context))) context e) (pcase-let ((`(,types ,option ,value ,errors) (cdr e)))
5) (print! (red "Error: %s received invalid value %S")
(doom-cli-invalid-option-error (string-join (doom-cli-option-switches option) "/")
(pcase-let ((`(,types ,option ,value ,errors) (cdr e))) value)
(print! (red "Error: %s received invalid value %S") (print! (bold "\nValidation errors:"))
(string-join (doom-cli-option-switches option) "/") (dolist (err errors) (print! (item "%s." (fill err)))))
value) (doom-cli-call `(:help "--postamble" ,@(cdr (doom-cli--command context))) context e)
(print! (bold "\nValidation errors:")) 5)
(dolist (err errors) (print! (item "%s." (fill err))))) (doom-cli-command-not-found-error
(doom-cli-call `(:help "--postamble" ,@(cdr (doom-cli--command context))) context e) (let* ((command (cdr e))
5) (cli (doom-cli-get command)))
(doom-cli-command-not-found-error (cond ((null cli)
(let* ((command (cdr e)) (print! (red "Error: unrecognized command '%s'")
(cli (doom-cli-get command))) (doom-cli-command-string (or (cdr command) command)))
(cond ((null cli) (doom-cli-call `(:help "--similar" "--postamble" ,@(cdr command)) context e))
(print! (red "Error: unrecognized command '%s'") ((null (doom-cli-fn cli))
(doom-cli-command-string (or (cdr command) command))) (print! (red "Error: a subcommand is required"))
(doom-cli-call `(:help "--similar" "--postamble" ,@(cdr command)) context e)) (doom-cli-call `(:help "--subcommands" "--postamble" ,@(cdr command)) context e))))
((null (doom-cli-fn cli)) 4)
(print! (red "Error: a subcommand is required")) (doom-cli-invalid-prefix-error
(doom-cli-call `(:help "--subcommands" "--postamble" ,@(cdr command)) context e)))) (let ((prefix (cadr e)))
4) (print! (red "Error: `run!' called with invalid prefix %S") prefix)
(doom-cli-invalid-prefix-error (if-let (suggested (cl-loop for cli being the hash-value of doom-cli--table
(let ((prefix (cadr e))) unless (doom-cli-type cli)
(print! (red "Error: `run!' called with invalid prefix %S") prefix) return (car (doom-cli-command cli))))
(if-let (suggested (cl-loop for cli being the hash-value of doom-cli--table (print! "Did you mean %S?" suggested)
unless (doom-cli-type cli) (print! "There are no commands defined under %S." prefix)))
return (car (doom-cli-command cli)))) 4)
(print! "Did you mean %S?" suggested) (doom-cli-deprecated-error
(print! "There are no commands defined under %S." prefix))) (pcase-let ((`(,command ,replacement ,when) (cdr e)))
4) (print! (yellow "Error: %S was removed in %s")
(doom-cli-deprecated-error (doom-cli-command-string command)
(pcase-let ((`(,command ,replacement ,when) (cdr e))) when)
(print! (yellow "Error: %S was removed in %s") (print-group!
(doom-cli-command-string command) (print! "\nUse %S instead." replacement)))
when) (doom-cli-call `(:help "--postamble" ,@(cdr command)) context e)
(print-group! 4)
(print! "\nUse %S instead." replacement))) (user-error
(doom-cli-call `(:help "--postamble" ,@(cdr command)) context e) (print! (red "Error: %s") (cadr e))
4) (print! "\nAborting...")
(user-error 3))
(print! (red "Error: %s") (cadr e)) context)))
(print! "\nAborting...")
3))
context)))))
(defalias 'sh! #'doom-call-process) (defalias 'sh! #'doom-call-process)

View file

@ -105,9 +105,21 @@
;; Load standard :help and :version handlers. ;; Load standard :help and :version handlers.
(load! "cli/help") (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." "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) (provide 'core-cli)
;;; core-cli.el ends here ;;; core-cli.el ends here