aboutsummaryrefslogtreecommitdiff
path: root/src/Config.hs
blob: 0a421fae6451e90d52868748a9d754531aa46499 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
{-# LANGUAGE OverloadedStrings #-}

module Config
  ( configUsage
  , Config(..)
  , getConfig
  ) where

import Data.Maybe (catMaybes, isJust, fromMaybe)
import Data.Map (Map)
import qualified Data.Map as M
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.IO as T

import Control.Monad (guard)

import System.Directory (doesFileExist)

import Model.URL
import Model.Config

import Utils.Text

configUsage :: Text
configUsage =
  T.intercalate "\n"
    [ ""
    , T.concat
        [ "  Some information is required in the file `"
        , T.pack configPath
        , "`:"
        ]
    , ""
    , "    - url (required)"
    , "    - mailTo (optional)"
    , "    - properties (optional)"
    , ""
    , "  Example:"
    , ""
    , "    # The url field is required"
    , "    url = http://www.leboncoin.fr/locations/offres/ile_de_france/?f=a&th=1"
    , ""
    , "    # The mailTo field is an optional list"
    , "    # mailTo = jean.dupont@mail.fr, john.smith@mail.com"
    , ""
    , "    # The properties field is an optional list"
    , "    # properties = cp, city, surface, ges"
    ]

configPath :: FilePath
configPath = "conf"

getConfig :: IO (Maybe Config)
getConfig = do
  exists <- doesFileExist configPath
  if exists
    then
      configFromFile <$> T.readFile configPath
    else
      return Nothing

configFromFile :: Text -> Maybe Config
configFromFile =
  configFromMap
  . M.fromList
  . catMaybes
  . map lineConfig
  . filter (not . T.null)
  . filter (not . startsWith "#")
  . map T.strip
  . T.lines

configFromMap :: Map Text Text -> Maybe Config
configFromMap map = do
  url <- M.lookup "url" map
  let config =
        Config
          { url = url
          , mailTo = fieldValues "mailTo" map
          , properties = fieldValues "properties" map
          }
  return config

fieldValues :: Text -> Map Text Text -> [Text]
fieldValues field map = fromMaybe [] $ fmap T.strip . T.splitOn "," <$> M.lookup field map

lineConfig :: Text -> Maybe (Text, Text)
lineConfig line = do
  (key, value) <- keyValue line
  guard (T.length key > 0)
  return (key, value)

keyValue :: Text -> Maybe (Text, Text)
keyValue line =
  let sep = '='
  in  if isJust (T.find (== sep) line)
        then
          let key = T.takeWhile (/= sep) line
              value = T.drop 1 . T.dropWhile (/= sep) $ line
          in  Just (T.strip key, T.strip value)
        else
          Nothing