Skip to content

Instantly share code, notes, and snippets.

@metacoma
Last active April 10, 2025 16:35
Show Gist options
  • Save metacoma/3b5e5df53dd1c4084b1cb320756743c9 to your computer and use it in GitHub Desktop.
Save metacoma/3b5e5df53dd1c4084b1cb320756743c9 to your computer and use it in GitHub Desktop.
tmuxinator guile
(use-modules
((srfi srfi-1)) ;; for iota
(ice-9 pretty-print)
(json))
;; Custom function to map with index
(define (map-indexed proc lst)
(define (iter idx lst)
(cond ((null? lst) '()) ;; end of list
(else (cons (proc idx (car lst)) (iter (+ idx 1) (cdr lst))))))
(iter 0 lst))
(define (make-pane-content commands)
(map (lambda (idx cmd) (cons idx cmd))
(iota (length commands))
commands))
;; Modified function to accept layout argument
(define (make-window pane-commands layout)
(define layout (if layout layout "main-horizontal")) ;; Default to "main-horizontal"
(map-indexed
(lambda (idx pane) (cons idx pane))
(map make-pane-content pane-commands)))
;; Define tmuxinator function to accept tmux_name and a list of windows
(define (tmuxinator tmux_name pre-window windows)
(list
(cons 'name tmux_name)
(cons 'root "./")
(cons 'pre_window pre-window)
(cons 'windows windows)))
;; Define your list of windows
(define windows
(list
(cons 'editor
(make-window
(list
'("uptime" "who" "date")
'("id" "ps" "shutdown")
'("tree" "ifconfig" "ls"))
"main-horizontal"))
(cons 'logs
(make-window
(list
'("tail" "-f" "/var/log/syslog")
'("journalctl" "-f"))
"main-vertical"))))
;; Example usage with tmux_name
(define tmux_name "host-broker")
(define (pre-window commands)
(make-pane-content commands))
;; Generate tmuxinator structure
(define result (tmuxinator tmux_name
(pre-window '(
"export PAGER=cat"
"ns=user-bob"
"vm_name=ubuntu-dev"
"hostname=$(hostname -s)"
"nats_from_stream=KN_USER_BOB__HOST_UBUNTU_DEV_FROM"
"nats_to_stream=KN_USER_BOB__HOST_UBUNTU_DEV_TO"
"mindwm_host=ubuntu-dev"
"nats_port=4222"
"nats_url=nats://root:r00tpass@${vm_name}:${nats_port}"
))
(list
(cons 'editor
(make-window
(list
'(
"export EDITOR=nvim"
"tmuxinator edit $(basename $(pwd))"
":REPLStart bash"))
"main-horizontal"))
(cons 'dashboard
(make-window
(list
'("kubectl -n ${ns} get NatsJetStreamChannel"
"# kubectl -n ${ns} delete NatsJetStreamChannel --all"
"# kubectl -n ${ns} delete Subscriptions --all"
"# kubectl delete ns ${ns}"
"# host-ubuntu-dev-from False DispatcherNotReady http://host-ubuntu-dev-from-kn-jsm-channel.user-bob.svc.cluster.local 29h"
"# host-ubuntu-dev-to False DispatcherNotReady http://host-ubuntu-dev-to-kn-jsm-channel.user-bob.svc.cluster.local 29h"
"# test False DispatcherNotReady http://test-kn-jsm-channel.user-bob.svc.cluster.local 25h"
"kubectl -n ${ns} describe NatsJetStreamChannel host-ubuntu-dev-from"
"kubectl -n ${ns} get deploy"))
"main-vertical"))
(cons 'nats-topics
(make-window
(list
'("nats_stream_name=${nats_from_stream}"
"while :; do"
"topic_name=$(nats -s ${nats_url} stream info ${nats_stream_name} -j | jq -r '.config.subjects[0]')"
"nats -s ${nats_url} sub ${topic_name}"
"sleep 5"
"done")
'("nats_stream_name=${nats_from_stream}"
"")) ; the second pane seems to be empty, leaving it as is
"main-horizontal"))
(cons 'nats-streams
(make-window
(list
'("NATS_STERAM_NAME=${nats_from_stream}"
"nats -s ${nats_url} stream info ${nats_from_stream}")
'("NATS_STERAM_NAME=${nats_from_stream}"
"nats -s ${nats_url} stream info ${nats_from_stream}"))
"main-horizontal"))
(cons 'test
(make-window
(list
'("# action=create name=test namespace=default nats_channel"
"# kubectl create ns ${ns}"
"# action=\"create\" name=\"host-${hostname}-from\" ns=\"${ns}\" hostname=${hostname} nats_channel"
"# action=\"create\" name=\"host-${hostname}-to\" ns=\"${ns}\" hostname=${hostname} nats_channel"
"# action=\"create\" name=\"zzz\" ns=\"${ns}\" hostname=${hostname} nats_channel"))
"main-vertical"))
)
))
;; Convert the result to JSON format
(display (scm->json-string result))
(newline)
;; {
;; "name": "host-broker",
;; "root": "./",
;; "pre_window": {
;; "0": "export PAGER=cat",
;; "1": "ns=user-bob",
;; "2": "vm_name=ubuntu-dev",
;; "3": "hostname=$(hostname -s)",
;; "4": "nats_from_stream=KN_USER_BOB__HOST_UBUNTU_DEV_FROM",
;; "5": "nats_to_stream=KN_USER_BOB__HOST_UBUNTU_DEV_TO",
;; "6": "mindwm_host=ubuntu-dev",
;; "7": "nats_port=4222",
;; "8": "nats_url=nats://root:r00tpass@${vm_name}:${nats_port}"
;; },
;; "windows": {
;; "editor": {
;; "0": {
;; "0": "export EDITOR=nvim",
;; "1": "tmuxinator edit $(basename $(pwd))",
;; "2": ":REPLStart bash"
;; }
;; },
;; "dashboard": {
;; "0": {
;; "0": "kubectl -n ${ns} get NatsJetStreamChannel",
;; "1": "# kubectl -n ${ns} delete NatsJetStreamChannel --all",
;; "2": "# kubectl -n ${ns} delete Subscriptions --all",
;; "3": "# kubectl delete ns ${ns}",
;; "4": "# host-ubuntu-dev-from False DispatcherNotReady http://host-ubuntu-dev-from-kn-jsm-channel.user-bob.svc.cluster.local 29h",
;; "5": "# host-ubuntu-dev-to False DispatcherNotReady http://host-ubuntu-dev-to-kn-jsm-channel.user-bob.svc.cluster.local 29h",
;; "6": "# test False DispatcherNotReady http://test-kn-jsm-channel.user-bob.svc.cluster.local 25h",
;; "7": "kubectl -n ${ns} describe NatsJetStreamChannel host-ubuntu-dev-from",
;; "8": "kubectl -n ${ns} get deploy"
;; }
;; },
;; "nats-topics": {
;; "0": {
;; "0": "nats_stream_name=${nats_from_stream}",
;; "1": "while :; do",
;; "2": "topic_name=$(nats -s ${nats_url} stream info ${nats_stream_name} -j | jq -r '.config.subjects[0]')",
;; "3": "nats -s ${nats_url} sub ${topic_name}",
;; "4": "sleep 5",
;; "5": "done"
;; },
;; "1": {
;; "0": "nats_stream_name=${nats_from_stream}",
;; "1": ""
;; }
;; },
;; "nats-streams": {
;; "0": {
;; "0": "NATS_STERAM_NAME=${nats_from_stream}",
;; "1": "nats -s ${nats_url} stream info ${nats_from_stream}"
;; },
;; "1": {
;; "0": "NATS_STERAM_NAME=${nats_from_stream}",
;; "1": "nats -s ${nats_url} stream info ${nats_from_stream}"
;; }
;; },
;; "test": {
;; "0": {
;; "0": "# action=create name=test namespace=default nats_channel",
;; "1": "# kubectl create ns ${ns}",
;; "2": "# action=\"create\" name=\"host-${hostname}-from\" ns=\"${ns}\" hostname=${hostname} nats_channel",
;; "3": "# action=\"create\" name=\"host-${hostname}-to\" ns=\"${ns}\" hostname=${hostname} nats_channel",
;; "4": "# action=\"create\" name=\"zzz\" ns=\"${ns}\" hostname=${hostname} nats_channel"
;; }
;; }
;; }
;; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment