aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorColin Okay <okay@toyful.space>2022-02-22 11:24:57 -0600
committerColin Okay <okay@toyful.space>2022-02-22 11:24:57 -0600
commite18e4e43d0467b2341c2176dcdb467f12d4358c7 (patch)
tree0aaf188ae03c3255ea3551c4e963e036e2ca3d45
parent72d11288e24349b875171db6b116a3661f5bdbac (diff)
running oneliners
-rw-r--r--build-app.lisp6
-rw-r--r--src/lib.lisp62
2 files changed, 60 insertions, 8 deletions
diff --git a/build-app.lisp b/build-app.lisp
index c95d163..8be3639 100644
--- a/build-app.lisp
+++ b/build-app.lisp
@@ -135,8 +135,7 @@ than the users."
((getopt :long-name "unlock")
(cli::unlock-item hist-number))
(t
- (format t "TBD: Going to run command ~a with arguments ~a~%"
- hist-number (rest arguments))))
+ (cli::run-item hist-number (rest arguments))))
(uiop:quit))
;; otherwise search for oneliners
(cli::search-for-oneliners arguments
@@ -146,8 +145,7 @@ than the users."
(help)))
(uiop:quit))
(error (e)
- (format *error-output* "ERROR: ~a~%" e)
- (help)
+ (format *error-output* "~%ERROR: ~a~%" e)
(uiop:quit))))
;;; DUMP EXECUTABLE
diff --git a/src/lib.lisp b/src/lib.lisp
index 300d6d5..442e023 100644
--- a/src/lib.lisp
+++ b/src/lib.lisp
@@ -52,6 +52,9 @@
(defun (setf api-token) (newval)
(setf (getf *config* :api-token) newval))
+(defun get-shell ()
+ (getf *config* :shell))
+
(defun make-temp-file-name ()
(namestring
(merge-pathnames (format nil "~a" (gensym "oneliners")) (uiop:temporary-directory))))
@@ -137,6 +140,53 @@ the directories that appear in the value of that variable."
(:host (host))
(api:put--oneliner-oneliner-locked (getf ol :id) :token (api-token) :value "false"))))
+(defun collect-positional-arguments (oneliner)
+ (remove-duplicates
+ (sort
+ (ppcre:all-matches-as-strings "\\$[1-9][0-9]*" oneliner)
+ #'string<)
+ :test #'equal))
+
+(defun collect-named-arguments (oneliner)
+ (remove-duplicates
+ (ppcre:all-matches-as-strings "\\$[a-zA-Z_][a-zA-Z0-9_]*" oneliner)
+ :test #'equal))
+
+(defun handle-run-oneliner (ol &key (runstyle "auto"))
+ (if (equalp runstyle "manual")
+ (progn (trivial-clipboard:text ol)
+ (format t "Copied oneliner to clipboard~%"))
+ (progn
+ (ensure-config)
+ (format t "Attempting to run: ~%")
+ (princ ol) (terpri)
+ (run-with-shell ol :shell-name (or (get-shell) "bash")))))
+
+
+(defun run-item (item-number args)
+ (with-cached-result (ol item-number)
+ (let* ((oneliner (getf ol :oneliner))
+ (runstyle (getf ol :runstyle))
+ (pos-args (collect-positional-arguments oneliner))
+ (named-args (collect-named-arguments oneliner)))
+
+ (when (or (not (getf ol :isflagged))
+ (y-or-n-p "This oneliner is flagged. Are you sure you want to run it?"))
+ ;; substitute positional args
+ (loop for param in pos-args
+ for arg in args
+ do (setf oneliner (str:replace-all param arg oneliner)))
+ ;; substitute named args
+ (setf args
+ (mapcar (lambda (s) (str:split "=" s))
+ (nthcdr (length pos-args) args)))
+ (loop for var in named-args
+ for bound = (assoc (subseq var 1) args :test #'equal)
+ when bound
+ do (setf oneliner
+ (str:replace-all var (second bound) oneliner)))
+
+ (handle-run-oneliner oneliner :runstyle runstyle)))))
(defun add-new-oneliner ()
@@ -151,7 +201,7 @@ the directories that appear in the value of that variable."
(tags
(append init-tags
(ppcre:split " +"
- (prompt (format nil "Additional Tags? [~{~a ~}]" init-tags)))))
+ (prompt (format nil "Tags in addition to ~{~a ~} ?" init-tags)))))
(explanation
(when (y-or-n-p "Provide an explanation?")
(string-from-editor)))
@@ -213,7 +263,9 @@ the directories that appear in the value of that variable."
(print results output)))
(defun print-oneliner-result-for-user (number oneliner)
- (format t "~3a~a~a~a: ~a"
+ (dotimes (n 80) (princ #\_))
+ (terpri)
+ (format t "~3a~a~a~a ~a"
number
(if (getf oneliner :isflagged)
"⚠" " ")
@@ -222,7 +274,8 @@ the directories that appear in the value of that variable."
(if (equalp "manual" (getf oneliner :runstyle))
"📋" " ")
(getf oneliner :brief))
- (format t "~% ~a~%~%" (getf oneliner :oneliner)))
+ (format t "~% by: ~12a tags: ~{~a~^ ~}" (getf oneliner :createdby) (getf oneliner :tags))
+ (format t "~%~% ~a~%~%" (getf oneliner :oneliner)))
(defun search-for-oneliners (terms limit not-flagged-p)
@@ -241,7 +294,8 @@ the directories that appear in the value of that variable."
(loop for number from 1
for oneliner in oneliners
collect (list* :result-number number oneliner)
- do (print-oneliner-result-for-user number oneliner)))))))
+ do (print-oneliner-result-for-user number oneliner)))
+ (format t "Run a command with: ol NUMBER [ARGS...]~%")))))
;;; RUNNING COMMANDS