Fork me on GitHub
#mount
<
2021-12-07
>
lgessler02:12:37

anybody have experience using mount with cli-matic? I'm passing mount/start-with-args into a subcommand's :runs attribute, and while the subcommand works, my program exits immediately, which is bad since I want an http server to run. not quite sure what i'm doing wrong

lgessler03:12:17

couldn't get to the bottom of this so using a workaround for now--leaving here for posterity

(defn stop-app []
  (doseq [component (:stopped (mount/stop))]
    (log/info component "stopped"))
  (shutdown-agents))

(def cli-config
  {:app         '...
   :global-opts []
   :commands    [{:command     "run" :short "r"
                  :description ["Start the web app and begin listening for requests."]
                  :opts        [{:option "port" :short "p" :as "port for HTTP server" :type :int}]
                  :runs        mount/start-with-args
                  :on-shutdown stop-app}]})


(defn run-cmd
  "like cli-matic's run-cmd, but doesn't exit at the end"
  [args supplied-config]
  (let [config (U2/cfg-v2 supplied-config)
        {:keys [help stderr subcmd retval] :as result} (run-cmd* config args)]

    ; prints the error message, if present
    (when (seq stderr)
      (U/printErr ["** ERROR: **" stderr "" ""]))

    ; prints help
    (cond
      (= :HELP-GLOBAL help)
      (let [helpFn (H/getGlobalHelperFn config subcmd)]
        (U/printErr (helpFn config subcmd)))

      (= :HELP-SUBCMD help)
      (let [helpFn (H/getSubcommandHelperFn config subcmd)]
        (U/printErr (helpFn config subcmd))))

    ;; For some reason, the run subcommand exits immediately when combined with cli-matic. Use this as a workaround.
    (if (#{"run" "r"} (first args))
      (log/info "Started server successfully")
      (P/exit-script retval))))

(defn start-app [args]
  (run-cmd args cli-config))

(defn -main [& args]
  (start-app args))