refactor: Move each command spec into its own file

This commit is contained in:
Madeleine Sydney
2025-01-10 16:09:58 -07:00
parent 06bfe00688
commit ec59de9827
4 changed files with 41 additions and 33 deletions

View File

@@ -2,8 +2,10 @@
(:require (:require
[clojure.pprint :refer [pprint]] [clojure.pprint :refer [pprint]]
[clojure.spec.alpha :as s] [clojure.spec.alpha :as s]
[clojure.string :as str]
[clojure.zip :as zip] [clojure.zip :as zip]
[spec-dict :refer [dict dict*]])) [spec-dict :refer [dict]]
[sydnix.cli-table :refer [*cli-table*]]))
(s/def ::block-context (s/def ::block-context
#{:document :section :paragraph}) #{: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)) (prn "help" opts))
(defn adorn-with-help-option [command-fn] (defn adorn-with-help-option [command-fn]
(fn [opts] (fn [{:keys [opts dispatch]}]
(prn "adorned help" opts) (if (:help opts)
(command-fn opts))) (do (printf "<help for %s>\n" (str/join " " dispatch))
(pprint *cli-table*))
(command-fn opts))))
(def command
{:cmds ["help"]
:fn command-fn})

View File

@@ -1,11 +1,19 @@
(ns sydnix.commands.rebuild (ns sydnix.commands.rebuild
(:require (:require
[clojure.java.shell :refer [sh]])) [clojure.java.shell :refer [sh]]
[sydnix.commands.help :refer [adorn-with-help-option]]))
(defn command [{:keys [args opts]}] (defn- command-fn [{:keys [args opts]}]
(let [command (let [rebuild-cmd
(concat ["sudo" "nixos-rebuild"] (concat ["sudo" "nixos-rebuild"]
(or args ["switch"]) (or args ["switch"])
["--flake" (:flake opts)])] ["--flake" (:flake opts)])]
(apply println "$" command) (apply println "$" rebuild-cmd)
(apply sh command))) (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"}}})

View File

@@ -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)) (prn opts))
(def command
{:cmds ["status"]
:desc "View system info"
:fn (adorn-with-help-option command-fn)})

View File

@@ -7,27 +7,12 @@
[sydnix.commands.rebuild :as cmd-rebuild]) [sydnix.commands.rebuild :as cmd-rebuild])
(:gen-class)) (: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 (def cli-table
[{:cmds ["status"] [cmd-status/command
:desc "View system info" cmd-rebuild/command
:fn (with-help cmd-status/command)} cmd-help/command
{:cmds ["rebuild"] ;; Show help when no other command matches.
:desc "Rebuild the system NixOS and Home-manager configuration" (assoc cmd-help/command :cmds [])])
:fn (with-help cmd-rebuild/command)
:spec cmd-rebuild-spec}
{:cmds ["help"]
:fn cmd-help/command}
{:cmds []
:fn cmd-help/command}])
(defn -main [& args] (defn -main [& args]
(binding [*cli-table* cli-table] (binding [*cli-table* cli-table]