branch: externals/plz
commit 570eae7fe435e2655d09cd61ae9b5db90f5c30f6
Author: Joseph Turner <jos...@breatheoutbreathe.in>
Commit: Joseph Turner <jos...@breatheoutbreathe.in>

    Tidy: (plz) Consolidate duplicate (pcase as ...) forms
    
    This commit avoids duplicate headers by passing --dump-header with "-"
    as its value in all requests and then passing it again with the
    overriding "/dev/null" value forn HEAD requests which output to the
    terminal.  For HEAD requests which output to a file with --output,
    --dump-header "-" is necessary since plz--http-status expects the
    process buffer to always contain the headers.
---
 plz.el | 82 +++++++++++++++++++++---------------------------------------------
 1 file changed, 26 insertions(+), 56 deletions(-)

diff --git a/plz.el b/plz.el
index f7b0651e65..903d71e6b9 100644
--- a/plz.el
+++ b/plz.el
@@ -433,69 +433,39 @@ into the process buffer.
          (curl-config-args (append curl-config-header-args
                                    (list (cons "--url" url)
                                          (cons "--create-dirs" "")
-                                         (cons "--request" (upcase 
(symbol-name method))))
+                                         (cons "--request" (upcase 
(symbol-name method)))
+                                         (cons "--dump-header" "-"))
                                    (when connect-timeout
                                      (list (cons "--connect-timeout"
                                                  (number-to-string 
connect-timeout))))
                                    (when timeout
                                      (list (cons "--max-time" 
(number-to-string timeout))))
-                                   ;; NOTE: To make a HEAD request
-                                   ;; requires using the "--head"
-                                   ;; option rather than "--request
-                                   ;; HEAD", and doing so with
-                                   ;; "--dump-header" duplicates the
-                                   ;; headers, so we must instead
-                                   ;; specify that for each other
-                                   ;; method.
+                                   (pcase as
+                                     ('file
+                                      (setf filename (make-temp-file "plz-"))
+                                      (list (cons "--output" filename)))
+                                     (`(file ,(and (pred stringp) as-filename))
+                                      (when (file-exists-p as-filename)
+                                        (error "File exists, will not 
overwrite: %S" as-filename))
+                                      ;; Use `expand-file-name' because curl 
doesn't
+                                      ;; expand, e.g. "~" into "/home/...".
+                                      (setf filename (expand-file-name 
as-filename))
+                                      (list (cons "--output" filename)))
+                                     ((guard (eq 'head method))
+                                      ;; Don't duplicate headers for HEAD
+                                      ;; requests which output to the terminal.
+                                      (list (cons "--dump-header" 
null-device))))
                                    (pcase method
-                                     ('get
-                                      (append (list (cons "--dump-header" "-"))
-                                              (pcase as
-                                                ('file
-                                                 (setf filename 
(make-temp-file "plz-"))
-                                                 (list (cons "--output" 
filename)))
-                                                (`(file ,(and (pred stringp) 
as-filename))
-                                                 (when (file-exists-p 
as-filename)
-                                                   (error "File exists, will 
not overwrite: %S" as-filename))
-                                                 ;; Use `expand-file-name' 
because curl doesn't
-                                                 ;; expand, e.g. "~" into 
"/home/...".
-                                                 (setf filename 
(expand-file-name as-filename))
-                                                 (list (cons "--output" 
filename))))))
                                      ((or 'put 'post)
-                                      (append (list (cons "--dump-header" "-"))
-                                              (pcase as
-                                                ('file
-                                                 (setf filename 
(make-temp-file "plz-"))
-                                                 (list (cons "--output" 
filename)))
-                                                (`(file ,(and (pred stringp) 
as-filename))
-                                                 (when (file-exists-p 
as-filename)
-                                                   (error "File exists, will 
not overwrite: %S" as-filename))
-                                                 ;; Use `expand-file-name' 
because curl doesn't
-                                                 ;; expand, e.g. "~" into 
"/home/...".
-                                                 (setf filename 
(expand-file-name as-filename))
-                                                 (list (cons "--output" 
filename))))
-                                              (list
-                                               ;; It appears that this must be 
the last argument
-                                               ;; in order to pass data on the 
rest of STDIN.
-                                               (pcase body
-                                                 (`(file ,filename)
-                                                  ;; Use `expand-file-name' 
because curl doesn't
-                                                  ;; expand, e.g. "~" into 
"/home/...".
-                                                  (cons "--upload-file" 
(expand-file-name filename)))
-                                                 (_ (cons data-arg "@-"))))))
-                                     ('delete
-                                      (append (list (cons "--dump-header" "-"))
-                                              (pcase as
-                                                ('file
-                                                 (setf filename 
(make-temp-file "plz-"))
-                                                 (list (cons "--output" 
filename)))
-                                                (`(file ,(and (pred stringp) 
as-filename))
-                                                 (when (file-exists-p 
as-filename)
-                                                   (error "File exists, will 
not overwrite: %S" as-filename))
-                                                 ;; Use `expand-file-name' 
because curl doesn't
-                                                 ;; expand, e.g. "~" into 
"/home/...".
-                                                 (setf filename 
(expand-file-name as-filename))
-                                                 (list (cons "--output" 
filename))))))
+                                      (list
+                                       ;; It appears that this must be the 
last argument
+                                       ;; in order to pass data on the rest of 
STDIN.
+                                       (pcase body
+                                         (`(file ,filename)
+                                          ;; Use `expand-file-name' because 
curl doesn't
+                                          ;; expand, e.g. "~" into "/home/...".
+                                          (cons "--upload-file" 
(expand-file-name filename)))
+                                         (_ (cons data-arg "@-")))))
                                      ('head
                                       (list (cons "--head" ""))))))
          (curl-config (cl-loop for (key . value) in curl-config-args

Reply via email to