From 9c604f969bd7193922f40b24dc23f5f1bc56eef1 Mon Sep 17 00:00:00 2001 From: Dimitri Fontaine Date: Tue, 2 Sep 2014 22:33:51 +0200 Subject: [PATCH] Rename --load into --load-lisp-file To avoid wasting everybody's time when trying to debug --load command.load, rename the option to be more explicit about what it does. Also implement some basic guards in the form of testing that the filename extension is part of a very short whitelist: .lisp, .cl, .lsp and .asd. --- pgloader.1 | 6 +++--- pgloader.1.md | 4 ++-- src/main.lisp | 31 ++++++++++++++++++++++++++----- 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/pgloader.1 b/pgloader.1 index c87d147..a8c913e 100644 --- a/pgloader.1 +++ b/pgloader.1 @@ -1,7 +1,7 @@ .\" generated with Ronn/v0.7.3 .\" http://github.com/rtomayko/ronn/tree/0.7.3 . -.TH "PGLOADER" "1" "August 2014" "ff" "" +.TH "PGLOADER" "1" "September 2014" "ff" "" . .SH "NAME" \fBpgloader\fR \- PostgreSQL data loader @@ -76,11 +76,11 @@ List known encodings in this version of pgloader\. Parse given files in the command line as \fBpgloader\.conf\fR files with the \fBINI\fR syntax that was in use in pgloader versions 2\.x, and output the new command syntax for pgloader on standard output\. . .TP -\-l \fIfile\fR, \-\-load \fIfile\fR +\fB\-l \fR, \fB\-\-load\-lisp\-file \fR Specify a lisp \fIfile\fR to compile and load into the pgloader image before reading the commands, allowing to define extra transformation function\. Those functions should be defined in the \fBpgloader\.transforms\fR package\. This option can appear more than once in the command line\. . .TP -\-\-self\-upgrade \fIdirectory\fR: +\fB\-\-self\-upgrade \fR: . .IP Specify a \fIdirectory\fR where to find pgloader sources so that one of the very first things it does is dynamically loading\-in (and compiling to machine code) another version of itself, usually a newer one like a very recent git checkout\. diff --git a/pgloader.1.md b/pgloader.1.md index 60194dd..4b7c716 100644 --- a/pgloader.1.md +++ b/pgloader.1.md @@ -59,13 +59,13 @@ pgloader operates using commands which are read from files: `INI` syntax that was in use in pgloader versions 2.x, and output the new command syntax for pgloader on standard output. - * -l , --load : + * `-l `, `--load-lisp-file `: Specify a lisp to compile and load into the pgloader image before reading the commands, allowing to define extra transformation function. Those functions should be defined in the `pgloader.transforms` package. This option can appear more than once in the command line. - * --self-upgrade : + * `--self-upgrade `: Specify a where to find pgloader sources so that one of the very first things it does is dynamically loading-in (and compiling to diff --git a/src/main.lisp b/src/main.lisp index a15bf2c..94a06ba 100644 --- a/src/main.lisp +++ b/src/main.lisp @@ -39,8 +39,8 @@ (("logfile" #\L) :type string :documentation "Filename where to send the logs.") - (("load" #\l) :type string :list t :optional t - :documentation "Read user code from file") + (("load-lisp-file" #\l) :type string :list t :optional t + :documentation "Read user code from files") ("self-upgrade" :type string :optional t :documentation "Path to pgloader newer sources"))) @@ -118,6 +118,19 @@ (mkdir-or-die summary-dir debug) summary-pathname))) +(defvar *--load-list-file-extension-whitelist* '("lisp" "lsp" "cl" "asd") + "White list of file extensions allowed with the --load option.") + +(defun load-extra-transformation-functions (filename) + "Load an extra filename to tweak pgloader's behavior." + (let ((pathname (uiop:parse-native-namestring filename))) + (unless (member (pathname-type pathname) + *--load-list-file-extension-whitelist* + :test #'string=) + (error "Unknown lisp file extension: ~s" (pathname-type pathname))) + + (load (compile-file pathname :verbose nil :print nil)))) + (defun main (argv) "Entry point when building an executable image with buildapp" (let ((args (rest argv))) @@ -130,7 +143,8 @@ (usage argv :quit t))) (destructuring-bind (&key help version quiet verbose debug logfile - list-encodings upgrade-config load + list-encodings upgrade-config + ((:load-lisp-file load)) client-min-messages log-min-messages summary root-dir self-upgrade) options @@ -200,8 +214,15 @@ (uiop:quit)) (when load - (loop for filename in load - do (load (compile-file filename :verbose nil :print nil)))) + (loop for filename in load do + (handler-case + (load-extra-transformation-functions filename) + (condition (e) + (format *standard-output* + "Failed to load lisp source file ~s~%" + filename) + (format *standard-output* "~a~%" e) + (uiop:quit 3))))) ;; Now process the arguments (when arguments