Skip to content

Instantly share code, notes, and snippets.

@Heliosmaster
Last active March 28, 2021 11:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Heliosmaster/1068ade3db2c765aa70db018b27ec5a7 to your computer and use it in GitHub Desktop.
Save Heliosmaster/1068ade3db2c765aa70db018b27ec5a7 to your computer and use it in GitHub Desktop.
(defn print-help [prefix cmds]
(println "Usage:" prefix "[COMMAND] [FLAGS]")
(println)
(println "Flags:")
(println (format " %s%-40s%s%s" log/blue "-v" log/nc "Verbose"))
(println (format " %s%-40s%s%s" log/blue "-h" log/nc "Help"))
(println)
(when (seq cmds)
(println "Commands:")
(doseq [[cmd {:keys [description]}] (sort-by first cmds)]
(println (format " %s%-40s%s%s" log/blue cmd log/nc description)))))
(def commands
{"deploy" {:description "Deploys Nextjournal"
:command deploy-journal
:subcommands []
:usages {"nj deploy --image $SHA" "Deploys a specific version of Nextjournal"}}
"stop" {:description "Stops Nextjournal"
:command stop-journal
:subcommands []}
"configure" {:description "Configures the infrastructure"
:command init/init}
"check" {:description "Checks that requirements (binaries & DNS) are met"
:command init/check-cmd}
"dump-logs" {:description "Retrieves from the system some logs and configuration files and dumps them locally in ./debug"
:command dump-logs}
"show-config" {:description "Prints out the Nextjournal configuration file in use (if present)"
:command show-config}
"setup" {:description "Sets up the nextjournal cluster"
:command setup}
"status" {:description "Shows up some information about the system"
:command status}
"help" {:description "Show this help information"
:command (fn [_] (print-help "nj" commands))}
"tail-logs" {:description "Tails logs for the specified target"
:command tail-logs
:subcommands []
:usages {"nj tail-logs --target TARGET --log-type LOG_TYPE" "Tails the logs of the specific TARGET (.e.g journal). LOG_TYPE can be stdout or stderr, defaults to stdout "}}})
(defn print-usage [prefix description usages]
(println description)
(println)
(println "Usage:" prefix "[COMMAND] [FLAGS]")
(println)
(println "Flags:")
(println (format " %s%-40s%s%s" log/blue "-v" log/nc "Verbose"))
(println (format " %s%-40s%s%s" log/blue "-h" log/nc "Help"))
(when (seq usages)
(println)
(println "Usage:")
(doseq [[cmd usage] (sort-by first usages)]
(println (format " %s%-40s%s%s" log/blue cmd log/nc usage)))))
(defn -main [& _args]
(let [cmd (get commands (first *command-line-args*))
cli-options (->> [["-i" "--image SHA" "Image SHA"
:parse-fn str]
["-t" "--target TARGET" "Target name (.e.g journal)"]
["-l" "--log-type LOG_TYPE" "Log type (stdout|stderr, defaults to stdout)"]
["-h" "--help"]
["-c" "--check"]
["-v" "--verbose"]]
(tools.cli/parse-opts *command-line-args*)
:options)]
(when (:verbose cli-options)
(reset! log/debug-enabled? true))
(if cmd
(if (:help cli-options)
(print-usage (first *command-line-args*) (:description cmd) (:usages cmd))
(let [command (:command cmd)]
(-> {:cli-options cli-options}
(initialize-context)
(command (next *command-line-args*)))))
((-> (get commands "help")
:command) nil))
(println)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment