aboutsummaryrefslogtreecommitdiff
path: root/src/Config/CmdConfig.hs
blob: f58412a54b95bdf313e26c50583f07ee284e5c07 (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
{-# LANGUAGE OverloadedStrings #-}
module Config.CmdConfig
    ( parseCmdLine
    , cmdConfigToInternalConfig
    ) where

import           Options.Applicative
import           Path
import           Config.InternalConfig
import           Types
import           Filter


data CmdConfig = CmdConfig
    { _cmdConfigFile     :: !(Maybe FilePath)
    , _cmdConfigDatabase :: !(Maybe FilePath)
    , _cmdConfigDebug    :: !Bool
    , _cmdConfigFeedUrls :: !(Maybe [Url])
    }

parser :: Parser CmdConfig
parser = CmdConfig
    <$> optional (strOption
        (  long "config"
        <> short 'c'
        <> metavar "FILE"
        <> help "Specify an alternate configuration file. Relative paths start from $XDG_CONFIG_HOME, or ~/.config if the XDG_CONFIG_HOME environment variable is unset.\n (Default: perfeed/perfeed.yaml)"
        ))
    <*> optional (strOption
        (  long "database"
        <> short 'd'
        <> metavar "DB"
        <> help "SQLite version 3 database file. Specify an alternate database file. Relative paths start from $XDG_DATA_HOME, or ~/.local/share if the XDG_DATA_HOME environment variable is unset.\n (Default: perfeed/perfeed.db)"
        ))
    <*> switch
        ( long "debug"
        <> help "Turn on debugging"
        )
    <*> optional (some (argument auto (metavar "URL ...")))

parseCmdLine :: IO InternalConfig
parseCmdLine = cmdConfigToInternalConfig <$> parseCmdLine'

parseCmdLine' :: IO CmdConfig
parseCmdLine' = execParser $ info (helper <*> parser)
    (  fullDesc
    <> header "perfeed - check feeds (Atom, RSS) and convert entries to emails"
    )

-- TODO cmdline afrom ato
cmdConfigToInternalConfig :: CmdConfig -> InternalConfig
cmdConfigToInternalConfig (CmdConfig mcfile mdb debug murls) = setLogLevel mlevel $ setFeedInfos (map mkFeedInfo <$> murls) $ setDatabase mdb' $ setConfigFile mcfile' mkInternalConfig
  where
    mcfile' = maybe Nothing parseAbsFile mcfile
    mdb' = maybe Nothing parseAbsFile mdb
    mlevel = if debug then Just LevelDebug else Nothing