kazbek/tea-cleaner/TeaCleaner/Configuration.hs

73 lines
1.8 KiB
Haskell

module TeaCleaner.Configuration
( ProgramOptions(..)
, Settings(..)
, decodeSettingsFile
, commandLineInfo
, execParser
) where
import GHC.Records (HasField(..))
import Data.Text (StrictText)
import qualified Toml
import Toml ((.=))
import Options.Applicative
( Parser
, ParserInfo
, (<**>)
, execParser
, fullDesc
, help
, helper
, info
, long
, progDesc
, switch
)
import Text.URI (URI)
import qualified Text.URI as URI
import Data.Time (UTCTime(..), getCurrentTime)
data ConfigFile = ConfigFile
{ token :: StrictText
, server :: StrictText
} deriving (Eq, Show)
configFileCodec :: Toml.TomlCodec ConfigFile
configFileCodec = ConfigFile
<$> Toml.text "token" .= getField @"token"
<*> Toml.text "server" .= getField @"server"
data Settings = Settings
{ token :: StrictText
, server :: URI
, now :: UTCTime
} deriving (Eq, Show)
decodeSettingsFile :: FilePath -> IO Settings
decodeSettingsFile configPath = Toml.decodeFile configFileCodec configPath
>>= withConfiguration
where
withConfiguration configFile@ConfigFile{ server } = URI.mkURI server
>>= withServer configFile
withServer configFile parsedServer = getCurrentTime
>>= withTime configFile parsedServer
withTime ConfigFile{..} parsedServer now = pure $ Settings
{ token = token
, server = parsedServer
, now = now
}
newtype ProgramOptions = ProgramOptions
{ liveRun :: Bool
} deriving (Eq, Show)
commandLineInfo :: ParserInfo ProgramOptions
commandLineInfo = info (commandLine <**> helper)
$ fullDesc <> progDesc "Helps to detect some spam gitea accounts"
commandLine :: Parser ProgramOptions
commandLine = ProgramOptions
<$> liveRunOption
where
liveRunOption = switch $ long "live-run" <> help "Purge suspicious users"