8 changed files with 112 additions and 56 deletions
@ -1,2 +1,2 @@ |
|||||||
page-width = 800px |
page-width = 800 |
||||||
max-title-length = 80 |
max-title-length = 80 |
||||||
|
|||||||
@ -1,24 +1,29 @@ |
|||||||
(ns NoteHub.settings |
(ns NoteHub.settings |
||||||
(:require [clojure.string :as cs])) |
(:refer-clojure :exclude [replace reverse]) |
||||||
|
(:use [clojure.string])) |
||||||
|
|
||||||
|
; Load and parse te settings file returning a map |
||||||
|
(def settings-map |
||||||
|
(let [file-content (slurp "settings") |
||||||
|
lines (split file-content #"\n") |
||||||
|
pairs (map #(map trim (split % #"=")) lines)] |
||||||
|
(apply hash-map |
||||||
|
(mapcat #(list (keyword (first %)) (second %)) pairs)))) |
||||||
|
|
||||||
(defn get-setting |
(defn get-setting |
||||||
"Takes a settings key, a default value and a converter function and returns a corresponding |
"Takes a settings key, a converter function and a default value, and returns a corresponding |
||||||
settings value. The default value is returned back when no setting value was found. |
setting value. The default value is returned back when no setting value was found. |
||||||
The converter function can be provided to convert the setting from string to a needed format." |
The converter function can be provided to convert the setting from string to a needed type." |
||||||
[key & more] |
[key & more] |
||||||
(let [default (first more) |
(let [converter (first more) |
||||||
converter (second more) |
default (second more) |
||||||
file-content (slurp "settings") |
value (settings-map key) |
||||||
lines (cs/split file-content #"\n") |
|
||||||
pairs (map #(map cs/trim %) (map #(cs/split % #"=") lines)) |
|
||||||
config-map (apply hash-map (mapcat #(list (keyword (first %)) (second %)) pairs)) |
|
||||||
value (config-map key) |
|
||||||
; Through this hack we can read security-critical settings from (previously |
; Through this hack we can read security-critical settings from (previously |
||||||
; set) shell variables without commiting their content to CVS |
; set) shell variables without commiting their content to CVS |
||||||
value (if-not value |
value (if-not value |
||||||
(System/getenv |
(System/getenv |
||||||
(cs/upper-case |
(upper-case |
||||||
(cs/replace (name key) #"-" ""))))] |
(replace (name key) #"-" ""))))] |
||||||
(if value |
(if value |
||||||
(if (fn? converter) (converter value) value) |
(if (fn? converter) (converter value) value) |
||||||
default))) |
default))) |
||||||
|
|||||||
Loading…
Reference in new issue