1 {-# LANGUAGE OverloadedStrings #-}
3 -- | The program will parse ~/.twatrc for any available configuration
4 -- directives, resulting in an OptionalCfg. The command-line
5 -- arguments will be used to create another OptionalCfg, and the two
6 -- will be merged. Finally, a default_config will be updated from
7 -- the merged OptionalCfgs.
10 module OptionalConfiguration
13 import Data.Monoid (Monoid(..))
14 import qualified Data.Configurator as DC
16 -- The same as Cfg, except everything is optional. It's easy to merge
17 -- two of these by simply dropping the Nothings in favor of the Justs.
19 OptionalCfg { consumer_key :: Maybe String,
20 consumer_secret :: Maybe String,
21 access_token :: Maybe String,
22 access_secret :: Maybe String,
23 heartbeat :: Maybe Int,
24 ignore_replies :: Maybe Bool,
25 ignore_retweets :: Maybe Bool,
26 sendmail_path :: Maybe String,
27 from_address :: Maybe String,
28 to_address :: Maybe String,
29 verbose :: Maybe Bool }
31 instance Monoid OptionalCfg where
47 (merge (consumer_key cfg1) (consumer_key cfg2))
48 (merge (consumer_secret cfg1) (consumer_secret cfg2))
49 (merge (access_token cfg1) (access_token cfg2))
50 (merge (access_secret cfg1) (access_secret cfg2))
51 (merge (heartbeat cfg1) (heartbeat cfg2))
52 (merge (ignore_replies cfg1) (ignore_replies cfg2))
53 (merge (ignore_retweets cfg1) (ignore_retweets cfg2))
54 (merge (sendmail_path cfg1) (sendmail_path cfg2))
55 (merge (from_address cfg1) (from_address cfg2))
56 (merge (to_address cfg1) (to_address cfg2))
57 (merge (verbose cfg1) (verbose cfg2))
59 merge :: (Maybe a) -> (Maybe a) -> (Maybe a)
60 merge Nothing Nothing = Nothing
61 merge (Just x) Nothing = Just x
62 merge Nothing (Just x) = Just x
63 merge (Just _) (Just y) = Just y
65 from_rc :: IO OptionalCfg
67 cfg <- DC.load [ DC.Optional "$(HOME)/.twatrc" ]
68 cfg_consumer_key <- DC.lookup cfg "consumer-key"
69 cfg_consumer_secret <- DC.lookup cfg "consumer-secret"
70 cfg_access_token <- DC.lookup cfg "access-token"
71 cfg_access_secret <- DC.lookup cfg "access-secret"
72 cfg_heartbeat <- DC.lookup cfg "heartbeat"
73 cfg_ignore_replies <- DC.lookup cfg "ignore-replies"
74 cfg_ignore_retweets <- DC.lookup cfg "ignore-retweets"
75 cfg_sendmail_path <- DC.lookup cfg "sendmail-path"
76 cfg_from_address <- DC.lookup cfg "from"
77 cfg_to_address <- DC.lookup cfg "to"
78 cfg_verbose <- DC.lookup cfg "verbose"