diff --git a/scripts/sydnix/src/sydnix/commands/help.clj b/scripts/sydnix/src/sydnix/commands/help.clj index 21145d4..7b43f5c 100644 --- a/scripts/sydnix/src/sydnix/commands/help.clj +++ b/scripts/sydnix/src/sydnix/commands/help.clj @@ -2,8 +2,10 @@ (:require [clojure.pprint :refer [pprint]] [clojure.spec.alpha :as s] + [clojure.string :as str] [clojure.zip :as zip] - [spec-dict :refer [dict dict*]])) + [spec-dict :refer [dict]] + [sydnix.cli-table :refer [*cli-table*]])) (s/def ::block-context #{:document :section :paragraph}) @@ -118,12 +120,18 @@ -(defn docs-for-command [command]) +(defn- docs-for-command [command]) -(defn command [opts] +(defn- command-fn [opts] (prn "help" opts)) (defn adorn-with-help-option [command-fn] - (fn [opts] - (prn "adorned help" opts) - (command-fn opts))) + (fn [{:keys [opts dispatch]}] + (if (:help opts) + (do (printf "\n" (str/join " " dispatch)) + (pprint *cli-table*)) + (command-fn opts)))) + +(def command + {:cmds ["help"] + :fn command-fn}) diff --git a/scripts/sydnix/src/sydnix/commands/rebuild.clj b/scripts/sydnix/src/sydnix/commands/rebuild.clj index dc8c22c..01d1076 100644 --- a/scripts/sydnix/src/sydnix/commands/rebuild.clj +++ b/scripts/sydnix/src/sydnix/commands/rebuild.clj @@ -1,11 +1,19 @@ (ns sydnix.commands.rebuild (:require - [clojure.java.shell :refer [sh]])) + [clojure.java.shell :refer [sh]] + [sydnix.commands.help :refer [adorn-with-help-option]])) -(defn command [{:keys [args opts]}] - (let [command +(defn- command-fn [{:keys [args opts]}] + (let [rebuild-cmd (concat ["sudo" "nixos-rebuild"] (or args ["switch"]) ["--flake" (:flake opts)])] - (apply println "$" command) - (apply sh command))) + (apply println "$" rebuild-cmd) + (apply sh rebuild-cmd))) + +(def command + {:cmds ["rebuild"] + :desc "Rebuild the system NixOS and Home-manager configuration" + :fn (adorn-with-help-option command-fn) + :spec {:flake {:coerce :string + :default "path:///persist/dots"}}}) diff --git a/scripts/sydnix/src/sydnix/commands/status.clj b/scripts/sydnix/src/sydnix/commands/status.clj index a2fe42c..eb03dce 100644 --- a/scripts/sydnix/src/sydnix/commands/status.clj +++ b/scripts/sydnix/src/sydnix/commands/status.clj @@ -1,4 +1,11 @@ -(ns sydnix.commands.status) +(ns sydnix.commands.status + (:require + [sydnix.commands.help :refer [adorn-with-help-option]])) -(defn command [opts] +(defn- command-fn [opts] (prn opts)) + +(def command + {:cmds ["status"] + :desc "View system info" + :fn (adorn-with-help-option command-fn)}) diff --git a/scripts/sydnix/src/sydnix/main.clj b/scripts/sydnix/src/sydnix/main.clj index f509f66..15ad8bf 100755 --- a/scripts/sydnix/src/sydnix/main.clj +++ b/scripts/sydnix/src/sydnix/main.clj @@ -7,27 +7,12 @@ [sydnix.commands.rebuild :as cmd-rebuild]) (:gen-class)) -(def cmd-status-spec - {}) - -(def with-help cmd-help/adorn-with-help-option) - -(def cmd-rebuild-spec - {:flake {:coerce :string - :default "path:///persist/dots"}}) - (def cli-table - [{:cmds ["status"] - :desc "View system info" - :fn (with-help cmd-status/command)} - {:cmds ["rebuild"] - :desc "Rebuild the system NixOS and Home-manager configuration" - :fn (with-help cmd-rebuild/command) - :spec cmd-rebuild-spec} - {:cmds ["help"] - :fn cmd-help/command} - {:cmds [] - :fn cmd-help/command}]) + [cmd-status/command + cmd-rebuild/command + cmd-help/command + ;; Show help when no other command matches. + (assoc cmd-help/command :cmds [])]) (defn -main [& args] (binding [*cli-table* cli-table]