(ns sydnix-cli.commands.rebuild
  (:require
   [babashka.process :as p]
   [sydnix-cli.commands.help :refer [adorn-with-help-option*]]))

(def command-options-spec
  {:flake {:coerce :string
           :ref "URI"
           :desc "URI is a path to a Flake, which is passed to `nixos-rebuild
                  --flake URI` as is."
           :default "path:///persist/dots"}})

(defn- command-fn [{:keys [args opts]}]
  (let [rebuild-cmd
        (concat ["sudo" "nixos-rebuild"]
                (or args ["switch"])
                ["--flake" (:flake opts)])]
    (apply println "$" rebuild-cmd)
    (-> (apply p/shell {:continue true} rebuild-cmd)
        :exit
        System/exit)))

(def commands
  [{:cmds ["rebuild"]
    :desc "Rebuild the system NixOS and Home-manager configuration"
    :fn command-fn
    :spec command-options-spec}])
