HEX
Server: Apache/2.4.46 (Ubuntu)
System: Linux localhost 5.11.0-49-generic #55-Ubuntu SMP Wed Jan 12 17:36:34 UTC 2022 x86_64
User: root (0)
PHP: 7.4.16
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/html/mysql-postgres/pgloader-3.6.2/src/parsers/template.lisp
;;;
;;; Allow the pgloader load command to be a Mustache Template.
;;;
;;; Variables are to be found either in the OS environment for the process,
;;; or in the .ini file given as a --context command line argument.
;;;

(in-package #:pgloader.parser)

(defun apply-template (string)
  (mustache:render* string *context*))

(defun initialize-context (filename)
  "Initialize a context from the environment variables and from the given
   context-filename (might be nil). CONTEXT-FILENAME is an INI file."

  (when filename
    (setf *context* (read-ini-file filename))))

(defun read-ini-file (filename)
  (let ((ini (ini:make-config)))
    (ini:read-files ini (list filename))

    (loop :for section :in (ini:sections ini)
       :append (loop :for option :in (ini:options ini section)
                  :for key := (string-upcase option)
                  :for val := (ini:get-option ini section option)
                  :collect (cons key val)))))


;;;
;;; cl-mustache doesn't read variables from the environment, and we want to.
;;; cl-mustache uses CLOS for finding values in a context from a key, so we
;;; can derive that.
;;;
(defmethod mustache::context-get :around ((key string) (context hash-table))
  (multiple-value-bind (data find)
      (call-next-method)
    (if find
        (values data find)
        (context-get-from-environment key))))

(defmethod mustache::context-get :around ((key string) (context null))
  (multiple-value-bind (data find)
      (call-next-method)
    (if find
        (values data find)
        (context-get-from-environment key))))

(defun context-get-from-environment (key)
  (let ((val (uiop:getenv key)))
    (if val
        (values val t)
        (values))))