Add support for CLI
This commit is contained in:
@@ -62,6 +62,11 @@
|
||||
"mvn-repo": "https://repo1.maven.org/maven2/",
|
||||
"hash": "sha256-EOzku1+YKQENwWVh9C67g7ry9HYFtR+RBbkvPKoIlxU="
|
||||
},
|
||||
{
|
||||
"mvn-path": "org/clojure/pom.contrib/1.2.0/pom.contrib-1.2.0.pom",
|
||||
"mvn-repo": "https://repo.maven.apache.org/maven2/",
|
||||
"hash": "sha256-CRbXpBVYuVAKQnyIb6KYJ6zlJZIGvjrTPmTilvwaYRE="
|
||||
},
|
||||
{
|
||||
"mvn-path": "org/clojure/spec.alpha/0.2.194/spec.alpha-0.2.194.jar",
|
||||
"mvn-repo": "https://repo1.maven.org/maven2/",
|
||||
@@ -81,6 +86,16 @@
|
||||
"mvn-path": "org/clojure/spec.alpha/0.3.218/spec.alpha-0.3.218.pom",
|
||||
"mvn-repo": "https://repo1.maven.org/maven2/",
|
||||
"hash": "sha256-bY3hTDrIdXYMX/kJVi/5hzB3AxxquTnxyxOeFp/pB1g="
|
||||
},
|
||||
{
|
||||
"mvn-path": "org/clojure/tools.cli/1.1.230/tools.cli-1.1.230.jar",
|
||||
"mvn-repo": "https://repo.maven.apache.org/maven2/",
|
||||
"hash": "sha256-kWYwtTmkP/RotN0BbGKFfitMtdpmhvEpdYfN1DyhAs0="
|
||||
},
|
||||
{
|
||||
"mvn-path": "org/clojure/tools.cli/1.1.230/tools.cli-1.1.230.pom",
|
||||
"mvn-repo": "https://repo.maven.apache.org/maven2/",
|
||||
"hash": "sha256-v7Yh5LAaW4vOEWpgcIQNzdWUnomceEaNgRtuiqqf0cc="
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
3
deps.edn
3
deps.edn
@@ -1,2 +1,3 @@
|
||||
{:deps {org.clojure/clojure {:mvn/version "1.11.1"}}
|
||||
{:deps {org.clojure/clojure {:mvn/version "1.11.1"}
|
||||
org.clojure/tools.cli {:mvn/version "1.1.230"}}
|
||||
:paths ["src"]}
|
||||
|
||||
@@ -22,7 +22,7 @@
|
||||
# https://jlesquembre.github.io/clj-nix/options/
|
||||
{
|
||||
projectSrc = ./.;
|
||||
name = "com.bartlomiejpluta.packer";
|
||||
name = "packer";
|
||||
main-ns = "packer.main";
|
||||
|
||||
nativeImage.enable = false;
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
|
||||
(defn produce-item [item nights i18n]
|
||||
(let [qnt (int (Math/ceil ((:quantity item) nights)))]
|
||||
(str "- [ ] " (get i18n (:key item)) (if (> qnt 1) (str " x" qnt) ""))))
|
||||
(str "- [ ] " (get i18n (:key item)) (if (> qnt 1) (str " x" qnt) ""))))
|
||||
|
||||
(defn produce-items [items nights i18n]
|
||||
(->> items
|
||||
|
||||
@@ -1,17 +1,57 @@
|
||||
(ns packer.main
|
||||
(:use packer.model)
|
||||
(:require [packer.core :as core]
|
||||
(:require [clojure.tools.cli :refer [parse-opts]]
|
||||
[clojure.string :as str]
|
||||
[packer.core :as core]
|
||||
[packer.util :as util])
|
||||
(:import java.io.File)
|
||||
(:gen-class))
|
||||
|
||||
|
||||
(defn eval-file [file]
|
||||
(map eval (read-string (str \( (slurp file) \)))))
|
||||
|
||||
(def cli-options
|
||||
[["-t" "--template FILE" "File to be used as template instead of default ~/.packer.template.clj or the one from PACKER_TEMPLATE_FILE env var"
|
||||
:parse-fn #(File. %)
|
||||
:validate [#(.exists %) "File must exist"]]
|
||||
["-h" "--help" "Display help"]])
|
||||
|
||||
(defn -main [& args]
|
||||
(let [script (last (eval-file (first args)))
|
||||
i18n (:i18n script)
|
||||
nights (util/ask-int (:nights? i18n))
|
||||
store (:store script)
|
||||
profile ((:profile script) nights)]
|
||||
(println (core/produce store profile nights i18n))))
|
||||
(let [cli (parse-opts args cli-options)
|
||||
env-template (System/getenv "PACKER_TEMPLATE_FILE")
|
||||
env-template-file (when (some? env-template) (File. env-template))
|
||||
arg-template-file (get-in cli [:options :template])
|
||||
home-template-file (File. (System/getProperty "user.home") ".packer.template.clj")
|
||||
template-file (->> [arg-template-file env-template-file home-template-file]
|
||||
(filter some?)
|
||||
(filter #(.exists %))
|
||||
first)
|
||||
output-file (first (:arguments cli))]
|
||||
;(println (core/produce store profile nights i18n))))
|
||||
(cond
|
||||
(or (empty? args) (get-in cli [:options :help])) (do
|
||||
(println "Packer is a tool to generate luggage check-lists using custom templates.")
|
||||
(println "Usage: packer [-t FILE] [-h] output")
|
||||
(println "Options:")
|
||||
(println (:summary cli))
|
||||
(System/exit 0))
|
||||
|
||||
(nil? template-file) (do
|
||||
(binding [*out* *err*] (println "Template file must exist either as ~/.packer.template.clj, be provided with PACKER_TEMPLATE_FILE env var or with -t CLI option."))
|
||||
(System/exit 1))
|
||||
|
||||
(nil? output-file) (do
|
||||
(binding [*out* *err*] (println "The output file is required. Please provide it as a positional CLI argument."))
|
||||
(System/exit 1))
|
||||
|
||||
(not (empty? (:errors cli))) (do
|
||||
(binding [*out* *err*] (println (str/join "\n" (:errors cli))))
|
||||
(System/exit 1))
|
||||
|
||||
:else (let [script (last (eval-file template-file))
|
||||
i18n (:i18n script)
|
||||
nights (util/ask-int (:nights? i18n))
|
||||
store (:store script)
|
||||
profile ((:profile script) nights)]
|
||||
(spit output-file (str (core/produce store profile nights i18n) "\n"))))))
|
||||
Reference in New Issue
Block a user