refactor(sydnix-cli): rewrite sydnix-cli

holy shit. why. why did i write the first version LOL. so fucking ocmplicated. and half broken.
This commit is contained in:
2025-12-01 14:05:21 -07:00
parent 1a685b4bf4
commit ea0c455824
30 changed files with 103 additions and 1154 deletions

View File

@@ -1,25 +1,33 @@
(ns sydnix-cli.main
(:require
[babashka.cli :as cli]
[sydnix-cli.cli-table :refer [*cli-table]]
[sydnix-cli.commands.help :as cmd-help :refer [adorn-with-help-option*]]
[sydnix-cli.commands.rebuild :as cmd-rebuild]
[sydnix-cli.commands.status :as cmd-status]
[sydnix-cli.commands.util :as cmd-util])
(:gen-class))
(:require [babashka.cli :as cli]
[sydnix-cli.commands.rebuild :as rebuild]
[sydnix-cli.commands.help :as help]
[clojure.pprint :refer [pprint]]))
(def real-cli-table
(map adorn-with-help-option*
(concat cmd-status/commands
cmd-rebuild/commands
cmd-util/commands
[cmd-help/command
;; Show help when no other command matches.
(assoc cmd-help/command :cmds [])])))
(defn help [opts]
(println "Hellllpp"))
(def cli-table
[rebuild/command
help/command
;; Assume help command when no subcommand is given.
(assoc help/command :cmds [])])
(defmacro stderr [& body]
`(binding [*out* *err*]
~@body))
(defn error-fn [{:keys [spec type cause msg option] :as data}]
(if (= :org.babashka/cli type)
(stderr (printf "Error: %s\n" msg))
(stderr
(println "a mysterious error has occured...")
(pprint data))))
(defn -main [& args]
(reset! *cli-table real-cli-table)
(cli/dispatch @*cli-table args)
;; Process may hang without this form. D:{
;; https://github.com/babashka/process?tab=readme-ov-file#script-termination
(cli/dispatch cli-table args
{:restrict true
:error-fn #(do (error-fn %)
(stderr (flush))
(System/exit 1))})
(shutdown-agents))