aboutsummaryrefslogtreecommitdiff
path: root/tea-cleaner/TeaCleaner/Configuration.hs
diff options
context:
space:
mode:
Diffstat (limited to 'tea-cleaner/TeaCleaner/Configuration.hs')
-rw-r--r--tea-cleaner/TeaCleaner/Configuration.hs72
1 files changed, 72 insertions, 0 deletions
diff --git a/tea-cleaner/TeaCleaner/Configuration.hs b/tea-cleaner/TeaCleaner/Configuration.hs
new file mode 100644
index 0000000..a7342d3
--- /dev/null
+++ b/tea-cleaner/TeaCleaner/Configuration.hs
@@ -0,0 +1,72 @@
+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"