8 changed files with 112 additions and 56 deletions
@ -1,2 +1,2 @@
@@ -1,2 +1,2 @@
|
||||
page-width = 800px |
||||
page-width = 800 |
||||
max-title-length = 80 |
||||
|
||||
@ -1,24 +1,29 @@
@@ -1,24 +1,29 @@
|
||||
(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 |
||||
"Takes a settings key, a default value and a converter function and returns a corresponding |
||||
settings 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." |
||||
"Takes a settings key, a converter function and a default value, and returns a corresponding |
||||
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 type." |
||||
[key & more] |
||||
(let [default (first more) |
||||
converter (second more) |
||||
file-content (slurp "settings") |
||||
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) |
||||
(let [converter (first more) |
||||
default (second more) |
||||
value (settings-map key) |
||||
; Through this hack we can read security-critical settings from (previously |
||||
; set) shell variables without commiting their content to CVS |
||||
value (if-not value |
||||
(System/getenv |
||||
(cs/upper-case |
||||
(cs/replace (name key) #"-" ""))))] |
||||
(upper-case |
||||
(replace (name key) #"-" ""))))] |
||||
(if value |
||||
(if (fn? converter) (converter value) value) |
||||
default))) |
||||
|
||||
Loading…
Reference in new issue