hs-hath-1.4.0: src/Hath/Config.hs
-- | The @client-login@ file: who we are and where the cache lives.
module Hath.Config
( ClientConfig(..)
, ClientProxy(..)
, CacheBackend(..)
, R2Config(..)
, defaultClientConfig
, readClientConfig
) where
import qualified Data.Text as T
import Data.Text.IO ( hPutStrLn )
import Dhall ( FromDhall, ToDhall, auto, input, inputExpr )
import Dhall.Core ( Expr(RecordLit) )
import qualified Dhall.Map as Dhall
import Relude
-- | Dhall record schema: deprecate a field in 'readClientConfig' before removing or renaming it.
data ClientConfig
= ClientConfig
{ clientId :: !Text
, key :: !Text
, version :: !Text
, proxy :: !(Maybe ClientProxy)
, downloadDir :: !Text
, cachePath :: !Text
-- ^ Directory (Filesystem) or database file (SQLite); unused by R2.
, cacheBackend :: !CacheBackend
, r2Config :: !(Maybe R2Config)
, lruCacheSize :: !Int64
}
deriving ( Show, Generic )
{-# DEPRECATED version "Unused: hs-hath announces Hath.Protocol.clientBuild. To be removed." #-}
{-# DEPRECATED proxy, ClientProxy "Unused: the image proxy comes from the H@H settings page. To be removed." #-}
{-# DEPRECATED lruCacheSize "Unused: there is no in-memory file cache. To be removed." #-}
instance FromDhall ClientConfig
instance ToDhall ClientConfig
data ClientProxy = ClientProxy { host :: !Text, port :: !Integer, auth :: !(Maybe ( Text, Text )) }
deriving ( Show, Generic )
instance FromDhall ClientProxy
instance ToDhall ClientProxy
data CacheBackend = CacheBackendSQLite | CacheBackendR2 | CacheBackendFilesystem
deriving ( Show, Eq, Generic )
instance FromDhall CacheBackend
instance ToDhall CacheBackend
-- | Bucket location; credentials come from @R2_ACCESS_KEY@ and @R2_SECRET_KEY@.
data R2Config = R2Config { r2Endpoint :: !Text, r2Bucket :: !Text }
deriving ( Show, Generic )
instance FromDhall R2Config
instance ToDhall R2Config
defaultClientConfig :: ClientConfig
defaultClientConfig
= ClientConfig
{ clientId = ""
, key = ""
, version = ""
, proxy = Nothing
, downloadDir = ""
, cachePath = ""
, cacheBackend = CacheBackendSQLite
, r2Config = Nothing
, lruCacheSize = 100 * 1024 * 1024
}
-- | Fields a @client-login@ may leave out, with the value they then take.
deprecatedFields :: [ ( Text, Text ) ]
deprecatedFields
= [ ( "version", "\"\"" )
, ( "proxy", "None { host : Text, port : Integer, auth : Optional { _1 : Text, _2 : Text } }" )
, ( "lruCacheSize", "+104857600" )
]
-- | Fills in the deprecated fields left out; warns on stderr about those present.
readClientConfig :: Text -> IO ClientConfig
readClientConfig source = do
present <- inputExpr source <&> \case
RecordLit fields -> filter (`Dhall.member` fields) (map fst deprecatedFields)
_ -> []
unless (null present)
$ hPutStrLn stderr
$ "Warning: client-login fields with no effect, to be removed in a later release: "
<> T.intercalate ", " present
<> ". Delete them, with their types and defaults (see client-login.example)."
input auto $ case [ name <> " = " <> value | ( name, value ) <- deprecatedFields, name `notElem` present ] of
[] -> source
missing -> "(" <> source <> ") // { " <> T.intercalate ", " missing <> " }"