From 0fa64f5ab7bc2b0062f14a09c5197577c020b951 Mon Sep 17 00:00:00 2001 From: gered Date: Fri, 26 Feb 2016 18:34:30 -0500 Subject: [PATCH] clean up initial projects, add basic arg parsing to cli module --- vwowrla.cli/project.clj | 9 ++- vwowrla.cli/repl/user.clj | 2 + vwowrla.cli/resources/log4j.properties | 13 ++++ vwowrla.cli/src/vwowrla/cli.clj | 6 -- vwowrla.cli/src/vwowrla/cli/core.clj | 75 +++++++++++++++++++++ vwowrla.cli/test/vwowrla/cli_test.clj | 8 --- vwowrla.core/dev-resources/log4j.properties | 6 ++ vwowrla.core/project.clj | 4 +- vwowrla.core/src/vwowrla/core.clj | 6 -- vwowrla.core/test/vwowrla/core_test.clj | 8 --- 10 files changed, 107 insertions(+), 30 deletions(-) create mode 100644 vwowrla.cli/repl/user.clj create mode 100644 vwowrla.cli/resources/log4j.properties delete mode 100644 vwowrla.cli/src/vwowrla/cli.clj create mode 100644 vwowrla.cli/src/vwowrla/cli/core.clj delete mode 100644 vwowrla.cli/test/vwowrla/cli_test.clj create mode 100644 vwowrla.core/dev-resources/log4j.properties delete mode 100644 vwowrla.core/src/vwowrla/core.clj delete mode 100644 vwowrla.core/test/vwowrla/core_test.clj diff --git a/vwowrla.cli/project.clj b/vwowrla.cli/project.clj index 6a1eec0..23581df 100644 --- a/vwowrla.cli/project.clj +++ b/vwowrla.cli/project.clj @@ -4,4 +4,11 @@ :license {:name "MIT License" :url "http://opensource.org/licenses/MIT"} - :dependencies [[org.clojure/clojure "1.8.0"]]) + :dependencies [[org.clojure/clojure "1.8.0"] + [org.clojure/tools.logging "0.3.1"] + [log4j "1.2.16"] + [org.clojure/tools.cli "0.3.3"] + + [vwowrla.core "0.1.0-SNAPSHOT"]] + + :profiles {:repl {:source-paths ["repl"]}}) diff --git a/vwowrla.cli/repl/user.clj b/vwowrla.cli/repl/user.clj new file mode 100644 index 0000000..1409d54 --- /dev/null +++ b/vwowrla.cli/repl/user.clj @@ -0,0 +1,2 @@ +(ns user + (:use vwowrla.cli.core)) diff --git a/vwowrla.cli/resources/log4j.properties b/vwowrla.cli/resources/log4j.properties new file mode 100644 index 0000000..c62ef06 --- /dev/null +++ b/vwowrla.cli/resources/log4j.properties @@ -0,0 +1,13 @@ +log4j.rootLogger=INFO, stdout, R + +log4j.appender.stdout=org.apache.log4j.ConsoleAppender +log4j.appender.stdout.Target=System.out +log4j.appender.stdout.layout=org.apache.log4j.PatternLayout +log4j.appender.stdout.layout.ConversionPattern=%d %-5p %c - %m%n + +log4j.appender.R=org.apache.log4j.RollingFileAppender +log4j.appender.R.File=${log4j.logdir}vwowrla.cli.log +log4j.appender.R.MaxFileSize=10MB +log4j.appender.R.MaxBackupIndex=20 +log4j.appender.R.layout=org.apache.log4j.PatternLayout +log4j.appender.R.layout.ConversionPattern=%d %-5p %c - %m%n diff --git a/vwowrla.cli/src/vwowrla/cli.clj b/vwowrla.cli/src/vwowrla/cli.clj deleted file mode 100644 index 48f320c..0000000 --- a/vwowrla.cli/src/vwowrla/cli.clj +++ /dev/null @@ -1,6 +0,0 @@ -(ns vwowrla.cli) - -(defn foo - "I don't do a whole lot." - [x] - (println x "Hello, World!")) diff --git a/vwowrla.cli/src/vwowrla/cli/core.clj b/vwowrla.cli/src/vwowrla/cli/core.clj new file mode 100644 index 0000000..9f7a034 --- /dev/null +++ b/vwowrla.cli/src/vwowrla/cli/core.clj @@ -0,0 +1,75 @@ +(ns vwowrla.cli.core + (:gen-class) + (:import + (java.util Date TimeZone)) + (:require + [clojure.string :as string] + [clojure.tools.cli :as cli])) + +(defn- get-current-year [] + (+ 1900 (.getYear (Date.)))) + +(defn- get-timezone [^String tz] + (let [timezone (TimeZone/getTimeZone tz)] + (if (= (.getID timezone) tz) + timezone))) + +(defn- ->usage-string [options-summary] + (->> ["Vanilla World of Warcraft Raid Log Analyzer" + "" + "Usage: vwowrla.cli -n CHARNAME -y YEAR [options] LOGFILE" + "" + "LOGFILE is the path/filename of the combat log file to parse." + "" + options-summary + ""] + (string/join \newline))) + +(defn- ->error-string [errors] + (str "Invalid options/arguments.\n" + (string/join \newline errors))) + +(defn- exit [status msg] + (println msg) + (System/exit status)) + +(def ^:private cli-options + [["-n" "--charname CHARNAME" + "Required. Character name of the player who recorded the combat log file." + :parse-fn #(-> (str %) (string/trim)) + :validate [#(not (empty? %)) "Character name is required."]] + ["-y" "--year YEAR" + "The year that the combat log was taken in. Current year if not specified." + :parse-fn #(Integer/parseInt %)] + ["-z" "--timezone TIMEZONE" + "Timezone that the timestamps within the combat log file are in. Current timezone if not specified." + :parse-fn #(-> (str %) (string/trim)) + :validate [#(or (empty? %) + (not (nil? (get-timezone %)))) + "Valid timezone name/abbreviation is required."]] + ["-h" "--help"]]) + +(defn -main [& args] + (let [{:keys [options arguments errors summary]} (cli/parse-opts args cli-options)] + ; option pre-validations + (cond + (:help options) (exit 0 (->usage-string summary)) + (not= (count arguments) 1) (exit 1 (->usage-string summary)) + errors (exit 1 (->error-string errors))) + + (let [filename (first arguments) + {:keys [year charname timezone] + :or {year (get-current-year) + charname nil + timezone (.getID (TimeZone/getDefault))}} + options] + (cond + (nil? charname) + (exit 1 (->error-string ["Character name is required."]))) + + ; TODO: run parse + (println + "charname:" charname \newline + "year:" year \newline + "timezone:" timezone \newline + "logfile:" filename)))) \ No newline at end of file diff --git a/vwowrla.cli/test/vwowrla/cli_test.clj b/vwowrla.cli/test/vwowrla/cli_test.clj deleted file mode 100644 index 42c9948..0000000 --- a/vwowrla.cli/test/vwowrla/cli_test.clj +++ /dev/null @@ -1,8 +0,0 @@ -(ns vwowrla.cli-test - (:require - [clojure.test :refer :all] - [vwowrla.cli :refer :all])) - -(deftest a-test - (testing "FIXME, I fail." - (is (= 0 1)))) diff --git a/vwowrla.core/dev-resources/log4j.properties b/vwowrla.core/dev-resources/log4j.properties new file mode 100644 index 0000000..5b96a6f --- /dev/null +++ b/vwowrla.core/dev-resources/log4j.properties @@ -0,0 +1,6 @@ +log4j.rootLogger=INFO, stdout + +log4j.appender.stdout=org.apache.log4j.ConsoleAppender +log4j.appender.stdout.Target=System.out +log4j.appender.stdout.layout=org.apache.log4j.PatternLayout +log4j.appender.stdout.layout.ConversionPattern=%d %-5p %c - %m%n diff --git a/vwowrla.core/project.clj b/vwowrla.core/project.clj index 2aae86f..db6faf8 100644 --- a/vwowrla.core/project.clj +++ b/vwowrla.core/project.clj @@ -4,4 +4,6 @@ :license {:name "MIT License" :url "http://opensource.org/licenses/MIT"} - :dependencies [[org.clojure/clojure "1.8.0"]]) + :dependencies [[org.clojure/clojure "1.8.0"] + [org.clojure/tools.logging "0.3.1"] + [log4j "1.2.16"]]) diff --git a/vwowrla.core/src/vwowrla/core.clj b/vwowrla.core/src/vwowrla/core.clj deleted file mode 100644 index e9d7533..0000000 --- a/vwowrla.core/src/vwowrla/core.clj +++ /dev/null @@ -1,6 +0,0 @@ -(ns vwowrla.core) - -(defn foo - "I don't do a whole lot." - [x] - (println x "Hello, World!")) diff --git a/vwowrla.core/test/vwowrla/core_test.clj b/vwowrla.core/test/vwowrla/core_test.clj deleted file mode 100644 index cfc9dd1..0000000 --- a/vwowrla.core/test/vwowrla/core_test.clj +++ /dev/null @@ -1,8 +0,0 @@ -(ns vwowrla.core-test - (:require - [clojure.test :refer :all] - [vwowrla.core :refer :all])) - -(deftest a-test - (testing "FIXME, I fail." - (is (= 0 1))))