]> gitweb.michael.orlitzky.com - dead/halcyon.git/blob - src/OptionalConfiguration.hs
53635439ac354c76bc1f9d96d074fac6ee89254c
[dead/halcyon.git] / src / OptionalConfiguration.hs
1 {-# LANGUAGE OverloadedStrings #-}
2
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.
8 --
9
10 module OptionalConfiguration
11 where
12
13 import Data.Monoid (Monoid(..))
14 import qualified Data.Configurator as DC
15
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.
18 data OptionalCfg =
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 }
30
31 instance Monoid OptionalCfg where
32 mempty = OptionalCfg
33 Nothing
34 Nothing
35 Nothing
36 Nothing
37 Nothing
38 Nothing
39 Nothing
40 Nothing
41 Nothing
42 Nothing
43 Nothing
44
45 cfg1 `mappend` cfg2 =
46 OptionalCfg
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))
58 where
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
64
65 from_rc :: IO OptionalCfg
66 from_rc = do
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"
79 return $ OptionalCfg
80 cfg_consumer_key
81 cfg_consumer_secret
82 cfg_access_token
83 cfg_access_secret
84 cfg_heartbeat
85 cfg_ignore_replies
86 cfg_ignore_retweets
87 cfg_sendmail_path
88 cfg_from_address
89 cfg_to_address
90 cfg_verbose
91