From a1a26e5df41f218bf74a8a1ce241dfdd215c7bde Mon Sep 17 00:00:00 2001 From: Mike Russo Date: Fri, 18 Apr 2025 15:02:23 -0600 Subject: [PATCH] parameterize birthdays as well --- bot.lisp | 48 +++++++++++++++++++++++++++++++----------------- flake.nix | 25 ++++++++++++++----------- 2 files changed, 45 insertions(+), 28 deletions(-) diff --git a/bot.lisp b/bot.lisp index 8f45dcc..be5f4e9 100644 --- a/bot.lisp +++ b/bot.lisp @@ -4,7 +4,7 @@ ;; (:export main*)) ;; (in-package :birthday-boy) (load (sb-ext:posix-getenv "ASDF")) -(asdf:load-systems :dexador :local-time :jonathan) +(asdf:load-systems :dexador :local-time :jonathan :cl-yaml :cl-dotenv) ;; ;; (.env:load-env (merge-pathnames ".env")) ;; (ql:quickload '(:dexador :cl-dotenv :local-time :jonathan)) @@ -76,15 +76,7 @@ ;; (list ;; :|reason| "for birthday bot"))) -(defvar *birthdays* '((:person "carl" :month 4 :day 13) - (:person "noellie" :month 7 :day 15) - (:person "joe" :month 9 :day 17) - (:person "daniel" :month 7 :day 18) - (:person "mike" :month 2 :day 13) - (:person "James Franco" :month 4 :day 19) - (:person "David Tennant" :month 4 :day 18) - (:person "famous actor Sean Bean" :month 4 :day 17) - (:person "bwandice" :month 10 :day 15))) +(defvar *birthdays* nil) (defun is-today (tstamp) @@ -100,10 +92,9 @@ (defun is-today* (day month) (is-today (local-time:encode-timestamp 0 0 0 0 day month 1900))) - (defun collect-bdays () - (loop for person in *birthdays* - when (is-today* (getf person :day) (getf person :month)) + (loop for person in (gethash "birthdays" *birthdays*) + when (is-today* (gethash "day" person) (gethash "month" person)) collect person)) @@ -113,9 +104,32 @@ (defun main () ;; (.env:load-env env-file) (dolist (person (collect-bdays)) - (message (make-bday-message (getf person :person)) "ROOM_ID"))) + (message (make-bday-message (gethash "person" person)) "ROOM_ID"))) +(defun check-required-env-vars () + (let ((required-vars '("BOT_NAME" "BOT_PW" "BOT_ACCESS_TOKEN" "HOMESERVER_URL" "ROOM_ID")) + (missing-vars '())) + + (dolist (var required-vars) + (unless (uiop:getenv var) + (push var missing-vars))) + + (when missing-vars + (format t "ERROR: Missing required environment variables:~%") + (dolist (var missing-vars) + (format t " - ~A~%" var)) + (sb-ext:exit :code 1)) + + t)) (defun main* () - (unwind-protect (main) - (sb-ext:exit :code 0))) - + (let* ((args sb-ext:*posix-argv*) + (env-file (or (second args) #P".env")) + (birthdays-file (or (third args) "./birthdays.yml"))) + (format t "Using env file: ~A~%" env-file) + (format t "Using birthdays file: ~A~%" birthdays-file) + (.env:load-env env-file) + (check-required-env-vars) + (unwind-protect + (let ((*birthdays* (cl-yaml:parse (pathname birthdays-file)))) + (main)) + (sb-ext:exit :code 0)))) diff --git a/flake.nix b/flake.nix index 0bc6752..667c522 100644 --- a/flake.nix +++ b/flake.nix @@ -21,8 +21,15 @@ [ "x86_64-linux" "aarch64-linux" "aarch64-darwin" "x86_64-darwin" ]; perSystem = { config, self', inputs', pkgs, system, ... }: let - sbcl' = pkgs.sbcl.withPackages - (ps: with ps; [ dexador cl-dotenv jonathan local-time deploy ]); + sbcl' = pkgs.sbcl.withPackages (ps: + with ps; [ + dexador + cl-dotenv + jonathan + local-time + deploy + cl-yaml + ]); in { # Per-system attributes can be defined here. The self' and inputs' # module parameters provide easy access to attributes of the same @@ -48,13 +55,13 @@ cp birthday-boy $out/bin/_birthday-boy chmod +x $out/bin/_birthday-boy + touch $out/bin/birthday-boy echo "#!/usr/bin/env sh" >> $out/bin/birthday-boy - echo "env \$(grep -v '^#' \$1 | xargs -d '\n') LD_LIBRARY_PATH=\$LD_LIBRARY_PATH:${ - pkgs.lib.makeLibraryPath [ pkgs.openssl ] - } $out/bin/_birthday-boy" >> $out/bin/birthday-boy + echo "LD_LIBRARY_PATH=\$LD_LIBRARY_PATH:${ + pkgs.lib.makeLibraryPath [ pkgs.openssl pkgs.libyaml ] + } $out/bin/_birthday-boy \$1 \$2" >> $out/bin/birthday-boy - # cp run.sh $out/bin/birthday-boy chmod +x $out/bin/birthday-boy ''; }; @@ -65,11 +72,7 @@ devshells.default = { env = [ ]; commands = [ ]; - packages = with pkgs; [ - openssl - (sbcl.withPackages - (p: with p; [ dexador cl-dotenv jonathan local-time deploy ])) - ]; + packages = with pkgs; [ openssl sbcl' ]; }; }; flake = {