From 719517d04051431db5a598731743b2d58907a587 Mon Sep 17 00:00:00 2001 From: Uday Verma Date: Fri, 4 Apr 2014 14:43:50 -0500 Subject: [PATCH] Use keywords for tags store, update deps, more unit tests, Fix #22 --- project.clj | 4 ++-- src/dakait/config.clj | 2 +- src/dakait/handler.clj | 4 ++-- src/dakait/tags.clj | 9 ++++++--- src/dakait/util.clj | 15 ++++++++++++--- test/dakait/test/util.clj | 21 +++++++++++++++++++++ 6 files changed, 44 insertions(+), 11 deletions(-) create mode 100644 test/dakait/test/util.clj diff --git a/project.clj b/project.clj index 6b438e9..cd5425e 100644 --- a/project.clj +++ b/project.clj @@ -11,7 +11,7 @@ [ring/ring-devel "1.2.2"] [ring/ring-core "1.2.2"] [http-kit "2.1.18"] - [org.clojure/clojurescript "0.0-2197"] + [org.clojure/clojurescript "0.0-2202"] [org.clojure/data.json "0.2.4"] [de.ubercode.clostache/clostache "1.3.1"] [org.clojure/tools.logging "0.2.6"] @@ -20,7 +20,7 @@ [reagent "0.4.2"] [crate "0.2.5"] [jayq "2.5.0"]] - :plugins [[lein-cljsbuild "1.0.2"] + :plugins [[lein-cljsbuild "1.0.3"] [lein-ancient "0.5.5"]] :main dakait.main diff --git a/src/dakait/config.clj b/src/dakait/config.clj index 494bdee..91f13a0 100644 --- a/src/dakait/config.clj +++ b/src/dakait/config.clj @@ -8,6 +8,7 @@ (def defaults { :server-name "Server" :sftp-port 22 :concurrency 4 + :local-base-path "." :username (System/getProperty "user.name") :base-path "." }) @@ -18,7 +19,6 @@ (defn- make-sure-required-exist [] "Makes sure that required properties exist" (let [required '(:config-data-dir - :local-base-path :sftp-host :private-key)] (doseq [k required] (when (nil? (config k)) diff --git a/src/dakait/handler.clj b/src/dakait/handler.clj index 612a0dc..85629fc 100644 --- a/src/dakait/handler.clj +++ b/src/dakait/handler.clj @@ -76,11 +76,11 @@ (do-with-cond (or (nil? tag) (nil? target)) 400 "Tag and taget file needs to be specified" (let [tag-obj (find-tag tag) - dest (get tag-obj "target")] + dest (:target tag-obj)] (do-with-cond (or (nil? tag-obj) (nil? dest)) 400 "The specified tag is invalid" (let [target-path (join-path (config :base-path) target) - dest-path (join-path (config :local-base-path) (get tag-obj "target"))] + dest-path (join-path (config :local-base-path) (:target tag-obj))] ;; start the download ;; (start-download target-path dest-path) diff --git a/src/dakait/tags.clj b/src/dakait/tags.clj index f544ef6..e48e171 100644 --- a/src/dakait/tags.clj +++ b/src/dakait/tags.clj @@ -3,9 +3,11 @@ ;; disk files (ns dakait.tags + (:use dakait.util) (:require [clojure.java.io :as io] - [clojure.data.json :as json])) + [clojure.data.json :as json] + [clojure.walk :refer [keywordize-keys]])) (def source-file (atom nil)) (def all-tags (atom {})) @@ -23,7 +25,8 @@ (when (.exists (io/as-file file)) (reset! all-tags (->> file slurp - json/read-str)))) + json/read-str + (map-vals keywordize-keys))))) (defn get-all-tags "Return all tags that we know of" @@ -33,7 +36,7 @@ (defn add-tag "Add the given tag to the list of tags" [name target color] - (reset! all-tags (assoc @all-tags name {"target" target "color" color})) + (reset! all-tags (assoc @all-tags name {:target target :color color})) (flush-to-disk)) (defn find-tag diff --git a/src/dakait/util.clj b/src/dakait/util.clj index 1f2cd34..fe6c35d 100644 --- a/src/dakait/util.clj +++ b/src/dakait/util.clj @@ -3,9 +3,14 @@ [clojure.java.io :as io])) (defn join-path - "Join path elements together" - [& parts] - (.getPath (reduce #(io/file %1 %2) parts))) + "Join path elements together, if any of the path components start with a / + the function assumes that the path is being reset to root and will ignore all parts + before that" + [p & parts] + (let [p (if (= p "") "." p)] + (.getPath (reduce #(if (.startsWith %2 "/") + (io/file %2) + (io/file %1 %2)) (io/file p) parts)))) (defn filename "Get the last component from the given path" @@ -15,4 +20,8 @@ last)) +(defn map-vals + "Maps the values of the given given map using the given function" + [f col] + (into {} (for [[k v] col] [k (f v)]))) diff --git a/test/dakait/test/util.clj b/test/dakait/test/util.clj new file mode 100644 index 0000000..195ae76 --- /dev/null +++ b/test/dakait/test/util.clj @@ -0,0 +1,21 @@ +(ns dakait.test.util + (:use clojure.test + dakait.util)) + +(defmacro join-path-as-string [& args] + `(.toString (join-path ~@args))) + +(deftest util + (testing "combines paths correctly" + (is (= (join-path-as-string "/home" "test") "/home/test")) + (is (= (join-path-as-string "/home" "/tmp") "/tmp")) + (is (= (join-path-as-string "tmp") "tmp")) + (is (= (join-path-as-string "." "/stuff") "/stuff")) + (is (= (join-path-as-string "" "stuff") "./stuff")) + (is (= (join-path-as-string "." "tmp") "./tmp"))) + + (testing "map-vals works correctly" + (is (= (map-vals inc {:a 1 :b 1}) {:a 2 :b 2})) + (is (= (map-vals keyword {:hello "world" :bye "world"}) {:hello :world :bye :world})))) + +