Skip to content

Commit

Permalink
Replace error throwing with parsing failures
Browse files Browse the repository at this point in the history
This fits better into the Parser picture: If a value is invalid, fail in
parsing - don't throw errors later.
  • Loading branch information
supersven committed Nov 25, 2024
1 parent ad27cff commit 857328d
Showing 1 changed file with 26 additions and 22 deletions.
48 changes: 26 additions & 22 deletions services/brig/src/Brig/Index/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,14 @@ elasticServerParser =
restrictedElasticSettingsParser :: Parser ElasticSettings
restrictedElasticSettingsParser = do
server <- elasticServerParser
prefix <-
strOption
indexName <-
option
readTestIndexName
( long "elasticsearch-index-prefix"
<> metavar "PREFIX"
<> help "Elasticsearch Index Prefix. The actual index name will be PREFIX_test."
<> value "directory"
<> showDefault
<> value [ES.qqIndexName|directory_test|]
<> showDefaultWith (const "directory")
)
mCreds <- credentialsPathParser
mCaCert <- caCertParser
Expand All @@ -188,35 +189,39 @@ restrictedElasticSettingsParser = do
{ _esConnection =
localElasticSettings._esConnection
{ esServer = server,
esIndex = mkIndexName (prefix <> "_test"),
esIndex = indexName,
esCredentials = mCreds,
esCaCert = mCaCert,
esInsecureSkipVerifyTls = verifyCa
}
}
where
readTestIndexName :: ReadM ES.IndexName
readTestIndexName = eitherReader mkTestIndexName

mkTestIndexName :: String -> Either String ES.IndexName
mkTestIndexName prefix = mapLeft Text.unpack (ES.mkIndexName (Text.pack (prefix <> "_test")))

indexNameParser :: Parser String
indexNameParser :: Parser ES.IndexName
indexNameParser =
strOption
option
readIndexName
( long "elasticsearch-index"
<> metavar "STRING"
<> help "Elasticsearch Index Name."
<> value
( Text.unpack
( ES.unIndexName (localElasticSettings._esConnection.esIndex)
)
)
(localElasticSettings._esConnection.esIndex)
<> showDefault
)

mkIndexName :: String -> ES.IndexName
mkIndexName = either (error "invalid index name") id . ES.mkIndexName . Text.pack
readIndexName :: ReadM ES.IndexName
readIndexName = eitherReader $ (\s -> mapLeft Text.unpack $ ES.mkIndexName (Text.pack s))

connectionSettingsParser :: Parser ESConnectionSettings
connectionSettingsParser =
ESConnectionSettings
<$> elasticServerParser
<*> fmap mkIndexName indexNameParser
<*> indexNameParser
<*> caCertParser
<*> verifyCaParser
<*> credentialsPathParser
Expand Down Expand Up @@ -338,14 +343,13 @@ reindexToAnotherIndexSettingsParser :: Parser ReindexFromAnotherIndexSettings
reindexToAnotherIndexSettingsParser =
ReindexFromAnotherIndexSettings
<$> connectionSettingsParser
<*> fmap
mkIndexName
( strOption
( long "destination-index"
<> metavar "STRING"
<> help "Elasticsearch index name to reindex to"
)
)
<*> ( option
readIndexName
( long "destination-index"
<> metavar "STRING"
<> help "Elasticsearch index name to reindex to"
)
)
<*> option
auto
( long "timeout"
Expand Down

0 comments on commit 857328d

Please sign in to comment.