Add --output option; default stdout

This commit is contained in:
Jaidyn Ann 2023-01-15 21:31:01 -06:00
parent 091e518828
commit b08db1b457

View File

@ -26,8 +26,14 @@
;; Write a to-be-JSON alist to the appropriate output ;; Write a to-be-JSON alist to the appropriate output
(define (chatd-json-write conn alist) (define (chatd-json-write conn alist)
(json-write alist) (let* ([configured-output? (hash-table-exists? conn 'output)]
(print "\n")) [output (if configured-output?
(open-output-file (hash-table-ref conn 'output))
(open-output-file* fileno/stdout))])
(json-write alist output)
(write-string "\n" #f output)
(if configured-output?
(close-output-port output))))
;; Return a user-info in chatd-friendly alist-format, by its alist ;; Return a user-info in chatd-friendly alist-format, by its alist
@ -174,13 +180,18 @@
(value (required PASSWORD))) (value (required PASSWORD)))
(name (name
"Set the realname of your connection." "Set the realname of your connection."
(value (required NAME))))) (value (required NAME)))
(output
"Output path for messages. Defaults to standard output."
(single-char #\o)
(value (required PATH)))))
;; Prints cli usage to stderr. ;; Prints cli usage to stderr.
(define (help) (define (help)
(write-string *help-msg* #f (open-output-file* fileno/stderr)) (write-string *help-msg* #f (open-output-file* fileno/stderr))
(write-string (usage *opts*) #f (open-output-file* fileno/stderr))) (write-string (usage *opts*) #f (open-output-file* fileno/stderr))
(exit 1))
;; The `main` procedure that should be called to run feedsnake-unix for use as script. ;; The `main` procedure that should be called to run feedsnake-unix for use as script.
@ -188,7 +199,8 @@
(let* ([args (getopt-long (command-line-arguments) *opts*)] (let* ([args (getopt-long (command-line-arguments) *opts*)]
[free-args (alist-ref '@ args)]) [free-args (alist-ref '@ args)])
(if (or (null? free-args) (alist-ref 'help args)) (if (or (null? free-args) (alist-ref 'help args))
(help) (help))
(let* (let*
([username (or (alist-ref 'username args) ([username (or (alist-ref 'username args)
(current-effective-user-name))] (current-effective-user-name))]
@ -199,12 +211,21 @@
[server (last free-args)] [server (last free-args)]
[hostname (first (string-split server ":"))] [hostname (first (string-split server ":"))]
[port (or (string->number (last (string-split server ":"))) [port (or (string->number (last (string-split server ":")))
6697)]) 6697)]
(if server [output (alist-ref 'output args)]
(irc:loop (irc:connect server port username nickname password fullname) [connection (if server
(irc:connect server port username nickname password fullname)
#f)])
(unless connection
(help))
(if output
(hash-table-set! connection 'output output))
(irc:loop connection
on-command on-command
on-reply) on-reply))))
(help))))))
(main) (main)