<commit_before>module Config (
  Config (..),
  MetricsConfig (..),
  configInfo,
) where

import Control.Applicative (optional)
import Data.Maybe (fromMaybe)
import Data.Semigroup ((<>))
import Options.Applicative
import qualified Network.Wai.Handler.Warp as Warp
import qualified Text.Read as Read
import Data.String (fromString)
import qualified Data.List as List
import qualified Data.Text as Text
import qualified Web.JWT as JWT

-- command-line arguments
data Config = Config
  { configDataFile :: FilePath
    -- | Enables the use of JWT for authorization in JWT.
  , configEnableJwtAuth :: Bool
    -- | The secret used for verifying the JWT signatures. If no secret is
    -- specified even though JWT authorization is enabled, tokens will still be
    -- used, but not be verified.
  , configJwtSecret :: Maybe JWT.Secret
  , configMetricsEndpoint :: Maybe MetricsConfig
  , configQueueCapacity :: Word
  }

data MetricsConfig = MetricsConfig
  { metricsConfigHost :: Warp.HostPreference
  , metricsConfigPort :: Warp.Port
  }

metricsConfigReader :: ReadM MetricsConfig
metricsConfigReader = eitherReader $ \input ->
  case List.break (== ':') input of
    (hostStr, ':':portStr) -> MetricsConfig (fromString hostStr) <$> Read.readEither portStr
    (_, _) -> Left "no port specified"

-- Parsing of command-line arguments

type EnvironmentConfig = [(String, String)]

configParser :: EnvironmentConfig -> Parser Config
configParser environment = Config
  <$> strOption (long "data-file" <>
                 metavar "DATA_FILE" <>
                 value "icepeak.json" <>
                 help "File where data is persisted to. Default: icepeak.json")
  <*> switch (long "enable-jwt-auth" <>
                help "Enable authorization using JSON Web Tokens.")
  <*> optional (secretOption (
                       long "jwt-secret" <>
                       metavar "JWT_SECRET" <>
                       environ "JWT_SECRET" <>
                       help "Secret used for JWT verification, defaults to the value of the JWT_SECRET environment variable if present. If no secret is passed, JWT tokens are not checked for validity."))
  <*> optional (option metricsConfigReader
                 (long "metrics" <>
                  metavar "HOST:PORT" <>
                  help "If provided, Icepeak collects various metrics and provides them to Prometheus on the given endpoint."
                 ))
  <*> fmap (fromMaybe 256)
           (optional (option auto
                       (long "queue-capacity" <>
                        metavar "INTEGER" <>
                        help "Higher values allow more requests to come in within a short amount of time.")))
  where<commit_msg>Use value instead of fromMaybe for default option<commit_after>module Config (
  Config (..),
  MetricsConfig (..),
  configInfo,
) where

import Control.Applicative (optional)
import Data.Semigroup ((<>))
import Options.Applicative
import qualified Network.Wai.Handler.Warp as Warp
import qualified Text.Read as Read
import Data.String (fromString)
import qualified Data.List as List
import qualified Data.Text as Text
import qualified Web.JWT as JWT

-- command-line arguments
data Config = Config
  { configDataFile :: FilePath
    -- | Enables the use of JWT for authorization in JWT.
  , configEnableJwtAuth :: Bool
    -- | The secret used for verifying the JWT signatures. If no secret is
    -- specified even though JWT authorization is enabled, tokens will still be
    -- used, but not be verified.
  , configJwtSecret :: Maybe JWT.Secret
  , configMetricsEndpoint :: Maybe MetricsConfig
  , configQueueCapacity :: Word
  }

data MetricsConfig = MetricsConfig
  { metricsConfigHost :: Warp.HostPreference
  , metricsConfigPort :: Warp.Port
  }

metricsConfigReader :: ReadM MetricsConfig
metricsConfigReader = eitherReader $ \input ->
  case List.break (== ':') input of
    (hostStr, ':':portStr) -> MetricsConfig (fromString hostStr) <$> Read.readEither portStr
    (_, _) -> Left "no port specified"

-- Parsing of command-line arguments

type EnvironmentConfig = [(String, String)]

configParser :: EnvironmentConfig -> Parser Config
configParser environment = Config
  <$> strOption (long "data-file" <>
                 metavar "DATA_FILE" <>
                 value "icepeak.json" <>
                 help "File where data is persisted to. Default: icepeak.json")
  <*> switch (long "enable-jwt-auth" <>
                help "Enable authorization using JSON Web Tokens.")
  <*> optional (secretOption (
                       long "jwt-secret" <>
                       metavar "JWT_SECRET" <>
                       environ "JWT_SECRET" <>
                       help "Secret used for JWT verification, defaults to the value of the JWT_SECRET environment variable if present. If no secret is passed, JWT tokens are not checked for validity."))
  <*> optional (option metricsConfigReader
                 (long "metrics" <>
                  metavar "HOST:PORT" <>
                  help "If provided, Icepeak collects various metrics and provides them to Prometheus on the given endpoint."
                 ))
  <*> option auto
       (long "queue-capacity" <>
        metavar "INTEGER" <>
        value 256 <>
        help "Higher values allow more requests to come in within a short amount of time.")
  where
{'fraction_non_alphanumeric': 0.09037955655768508, 'fraction_numerical': 0.0011273957158962795, 'mean_word_length': 2.6559065934065935, 'pattern_counts': {'":': 0, '<': 40, '<?xml version=': 0, '>': 48, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3996992', 'n_tokens_mistral': 1464, 'n_tokens_neox': 1379, 'n_words': 507}