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:
msyds committed 2025-12-01 14:22:30 -07:00
1 parent 1a685b4bf4
commit ea0c455824
30 files changed
+103 -1154

No files matched your search

@@ -1,42 +1,8 @@
(ns sydnix-cli.commands.help
(:require
[babashka.process :as p]
[sydnix-cli.mangen :as mangen]))
(:require [babashka.cli :as cli]))
(defn adorn-with-help-option [wrapped-command-fn]
(fn [{:keys [opts dispatch]}]
(if (:help opts)
(mangen/with-pipe
(fn [man->]
(mangen/write-man-for-command (mangen/find-dispatched dispatch)
:out man->))
(fn [->man]
(p/shell {:in ->man}
"man -l -")))
(wrapped-command-fn opts))))
(defn- view-man-for-command [command-spec]
(mangen/with-pipe
(fn [man->]
(mangen/write-man-for-command command-spec :out man->))
(fn [->man]
(p/shell {:in ->man} "man -l -"))))
(defn- wrap-command-fn [wrapped-fn]
(fn [info]
(if (:help (:opts info))
(do (mangen/render-docs-for-command
(mangen/find-dispatched (:dispatch info)))
#_
(view-man-for-command (mangen/find-dispatched (:dispatch info))))
(when-not (nil? wrapped-fn)
(wrapped-fn info)))))
(defn adorn-with-help-option* [command-spec]
(update command-spec :fn wrap-command-fn))
(defn- command-fn [_opts]
(view-man-for-command (mangen/find-dispatched [])))
(defn command-fn [{:keys [args opts]}]
(println "hallp"))
(def command
{:cmds ["help"]
@@ -1,7 +1,5 @@
(ns sydnix-cli.commands.rebuild
(:require
[babashka.process :as p]
[sydnix-cli.commands.help :refer [adorn-with-help-option*]]))
(:require [babashka.process :as p]))
(defn- parse-target [target]
(when-some [[_ _ user host] (re-matches #"(([-a-zA-Z0-9_]+)@)?(.+)"
@@ -23,15 +21,19 @@
:desc "Deploy config to HOST."
:coerce parse-target}})
(defn- command-fn [{:keys [args opts]}]
(defn- default-flake [host]
(str "path:///persist/dots"
(some->> host (str "#"))))
(defn command-fn [{:keys [args opts]}]
(let [rebuild-cmd
(concat (when-not (:target opts)
["sudo"])
["nixos-rebuild"]
(or args ["switch"])
["--flake" (if-some [host (:host (:target opts))]
(format "/persist/dots#%s" host)
"/persist/dots")]
["--flake" (or (:flake opts)
(-> opts :target :host
default-flake))]
(when-some [target (:target opts)]
["--sudo" "--ask-sudo-password"
"--target-host" (str-target target)]))]
@@ -40,8 +42,8 @@
:exit
System/exit)))
(def commands
[{:cmds ["rebuild"]
:desc "Rebuild the system NixOS and Home-manager configuration"
:fn command-fn
:spec command-options-spec}])
(def command
{:cmds ["rebuild"]
:desc "Rebuild the system's NixOS configuration"
:fn command-fn
:spec command-options-spec})
@@ -1,11 +0,0 @@
(ns sydnix-cli.commands.status
(:require
[sydnix-cli.commands.help :refer [adorn-with-help-option*]]))
(defn- command-fn [opts]
(prn opts))
(def commands
[{:cmds ["status"]
:desc "View system info"
:fn command-fn}])
@@ -1,8 +0,0 @@
(ns sydnix-cli.commands.util
(:require
[sydnix-cli.commands.util.mangen :as cmd-util-mangen]))
(def commands
[{:cmds ["util"]
:desc "Infrequently-used utilities such as docgen and shell completions."}
cmd-util-mangen/command])
@@ -1,46 +0,0 @@
(ns sydnix-cli.commands.util.mangen
(:require
[asciidoc.render]
[babashka.fs :as fs]
[babashka.process :as p]
[clojure.java.io :as io]
[clojure.spec.alpha :as s]
[clojure.string :as str]
[sydnix-cli.mangen :as mangen]
[sydnix-cli.cli-table :refer [*cli-table]]
[sydnix-cli.commands.help :refer [adorn-with-help-option*]]
[sydnix-cli.prelude :as prelude]))
(defn command-fn [{:keys [yes output-directory]}]
(if (or yes
(prelude/y-or-n? (format "Write a bunch of man pages to %s?"
output-directory)))
(doseq [cmd-spec (deref *cli-table)]
(let [man-file-name (fs/file
output-directory
(str (str/join "-" (cons "sydnix" (:cmds cmd-spec)))
".1"))]
(with-open [man-file (io/writer man-file-name)]
(mangen/write-man-for-command cmd-spec :out man-file))
(printf "wrote %s\n" man-file-name)
(flush)))
(System/exit 1)))
(def command-options-spec
{:output-directory {:coerce :string
:ref "DIRECTORY"
:alias :o
:desc "Directory to which the generated man pages will be
written."
:require true
:validate fs/directory?}
:yes {:coerce :bool
:desc "Don't ask for confirmation."
:default false}})
(def command
(adorn-with-help-option*
{:cmds ["util" "mangen"]
:desc "Generate man pages for each subcommand of `sydnix`."
:fn command-fn
:spec command-options-spec}))