{-# LANGUAGE ApplicativeDo  #-}
{-# LANGUAGE NamedFieldPuns #-}
module Plutus.ChainIndex.CommandLine(
  AppConfig(..)
  , Command(..)
  , CLIConfigOverrides(..)
  , applyOverrides
  , cmdWithHelpParser
  ) where

import Control.Lens (over)
import Options.Applicative (CommandFields, Mod, Parser, ParserInfo, argument, auto, command, flag, fullDesc, header,
                            help, helper, hsubparser, info, long, metavar, option, progDesc, short, str, value, (<**>))

import Cardano.Api (NetworkId (Testnet), NetworkMagic (NetworkMagic))
import Cardano.BM.Data.Severity (Severity (Debug))
import GHC.Word (Word32)
import Numeric.Natural (Natural)
import Plutus.ChainIndex.Config (ChainIndexConfig)
import Plutus.ChainIndex.Config qualified as Config

data CLIConfigOverrides =
  CLIConfigOverrides
    { CLIConfigOverrides -> Maybe String
ccSocketPath                 :: Maybe String
    , CLIConfigOverrides -> Maybe String
ccDbPath                     :: Maybe String
    , CLIConfigOverrides -> Maybe Int
ccPort                       :: Maybe Int
    , CLIConfigOverrides -> Maybe Word32
ccNetworkId                  :: Maybe Word32
    , CLIConfigOverrides -> Maybe Natural
ccAppendTransactionQueueSize :: Maybe Natural
    }
    deriving (CLIConfigOverrides -> CLIConfigOverrides -> Bool
(CLIConfigOverrides -> CLIConfigOverrides -> Bool)
-> (CLIConfigOverrides -> CLIConfigOverrides -> Bool)
-> Eq CLIConfigOverrides
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CLIConfigOverrides -> CLIConfigOverrides -> Bool
$c/= :: CLIConfigOverrides -> CLIConfigOverrides -> Bool
== :: CLIConfigOverrides -> CLIConfigOverrides -> Bool
$c== :: CLIConfigOverrides -> CLIConfigOverrides -> Bool
Eq, Eq CLIConfigOverrides
Eq CLIConfigOverrides
-> (CLIConfigOverrides -> CLIConfigOverrides -> Ordering)
-> (CLIConfigOverrides -> CLIConfigOverrides -> Bool)
-> (CLIConfigOverrides -> CLIConfigOverrides -> Bool)
-> (CLIConfigOverrides -> CLIConfigOverrides -> Bool)
-> (CLIConfigOverrides -> CLIConfigOverrides -> Bool)
-> (CLIConfigOverrides -> CLIConfigOverrides -> CLIConfigOverrides)
-> (CLIConfigOverrides -> CLIConfigOverrides -> CLIConfigOverrides)
-> Ord CLIConfigOverrides
CLIConfigOverrides -> CLIConfigOverrides -> Bool
CLIConfigOverrides -> CLIConfigOverrides -> Ordering
CLIConfigOverrides -> CLIConfigOverrides -> CLIConfigOverrides
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: CLIConfigOverrides -> CLIConfigOverrides -> CLIConfigOverrides
$cmin :: CLIConfigOverrides -> CLIConfigOverrides -> CLIConfigOverrides
max :: CLIConfigOverrides -> CLIConfigOverrides -> CLIConfigOverrides
$cmax :: CLIConfigOverrides -> CLIConfigOverrides -> CLIConfigOverrides
>= :: CLIConfigOverrides -> CLIConfigOverrides -> Bool
$c>= :: CLIConfigOverrides -> CLIConfigOverrides -> Bool
> :: CLIConfigOverrides -> CLIConfigOverrides -> Bool
$c> :: CLIConfigOverrides -> CLIConfigOverrides -> Bool
<= :: CLIConfigOverrides -> CLIConfigOverrides -> Bool
$c<= :: CLIConfigOverrides -> CLIConfigOverrides -> Bool
< :: CLIConfigOverrides -> CLIConfigOverrides -> Bool
$c< :: CLIConfigOverrides -> CLIConfigOverrides -> Bool
compare :: CLIConfigOverrides -> CLIConfigOverrides -> Ordering
$ccompare :: CLIConfigOverrides -> CLIConfigOverrides -> Ordering
$cp1Ord :: Eq CLIConfigOverrides
Ord, Int -> CLIConfigOverrides -> ShowS
[CLIConfigOverrides] -> ShowS
CLIConfigOverrides -> String
(Int -> CLIConfigOverrides -> ShowS)
-> (CLIConfigOverrides -> String)
-> ([CLIConfigOverrides] -> ShowS)
-> Show CLIConfigOverrides
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CLIConfigOverrides] -> ShowS
$cshowList :: [CLIConfigOverrides] -> ShowS
show :: CLIConfigOverrides -> String
$cshow :: CLIConfigOverrides -> String
showsPrec :: Int -> CLIConfigOverrides -> ShowS
$cshowsPrec :: Int -> CLIConfigOverrides -> ShowS
Show)

-- | Apply the CLI soverrides to the 'ChainIndexConfig'
applyOverrides :: CLIConfigOverrides -> ChainIndexConfig -> ChainIndexConfig
applyOverrides :: CLIConfigOverrides -> ChainIndexConfig -> ChainIndexConfig
applyOverrides CLIConfigOverrides{Maybe String
ccSocketPath :: Maybe String
ccSocketPath :: CLIConfigOverrides -> Maybe String
ccSocketPath, Maybe String
ccDbPath :: Maybe String
ccDbPath :: CLIConfigOverrides -> Maybe String
ccDbPath, Maybe Int
ccPort :: Maybe Int
ccPort :: CLIConfigOverrides -> Maybe Int
ccPort, Maybe Word32
ccNetworkId :: Maybe Word32
ccNetworkId :: CLIConfigOverrides -> Maybe Word32
ccNetworkId, Maybe Natural
ccAppendTransactionQueueSize :: Maybe Natural
ccAppendTransactionQueueSize :: CLIConfigOverrides -> Maybe Natural
ccAppendTransactionQueueSize} =
  ASetter ChainIndexConfig ChainIndexConfig String String
-> ShowS -> ChainIndexConfig -> ChainIndexConfig
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
over ASetter ChainIndexConfig ChainIndexConfig String String
Lens' ChainIndexConfig String
Config.socketPath (ShowS -> (String -> ShowS) -> Maybe String -> ShowS
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ShowS
forall a. a -> a
id String -> ShowS
forall a b. a -> b -> a
const Maybe String
ccSocketPath)
  (ChainIndexConfig -> ChainIndexConfig)
-> (ChainIndexConfig -> ChainIndexConfig)
-> ChainIndexConfig
-> ChainIndexConfig
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ASetter ChainIndexConfig ChainIndexConfig String String
-> ShowS -> ChainIndexConfig -> ChainIndexConfig
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
over ASetter ChainIndexConfig ChainIndexConfig String String
Lens' ChainIndexConfig String
Config.dbPath (ShowS -> (String -> ShowS) -> Maybe String -> ShowS
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ShowS
forall a. a -> a
id String -> ShowS
forall a b. a -> b -> a
const Maybe String
ccDbPath)
  (ChainIndexConfig -> ChainIndexConfig)
-> (ChainIndexConfig -> ChainIndexConfig)
-> ChainIndexConfig
-> ChainIndexConfig
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ASetter ChainIndexConfig ChainIndexConfig Int Int
-> (Int -> Int) -> ChainIndexConfig -> ChainIndexConfig
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
over ASetter ChainIndexConfig ChainIndexConfig Int Int
Lens' ChainIndexConfig Int
Config.port ((Int -> Int) -> (Int -> Int -> Int) -> Maybe Int -> Int -> Int
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Int -> Int
forall a. a -> a
id Int -> Int -> Int
forall a b. a -> b -> a
const Maybe Int
ccPort)
  (ChainIndexConfig -> ChainIndexConfig)
-> (ChainIndexConfig -> ChainIndexConfig)
-> ChainIndexConfig
-> ChainIndexConfig
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ASetter ChainIndexConfig ChainIndexConfig NetworkId NetworkId
-> (NetworkId -> NetworkId) -> ChainIndexConfig -> ChainIndexConfig
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
over ASetter ChainIndexConfig ChainIndexConfig NetworkId NetworkId
Lens' ChainIndexConfig NetworkId
Config.networkId ((NetworkId -> NetworkId)
-> (Word32 -> NetworkId -> NetworkId)
-> Maybe Word32
-> NetworkId
-> NetworkId
forall b a. b -> (a -> b) -> Maybe a -> b
maybe NetworkId -> NetworkId
forall a. a -> a
id (NetworkId -> NetworkId -> NetworkId
forall a b. a -> b -> a
const (NetworkId -> NetworkId -> NetworkId)
-> (Word32 -> NetworkId) -> Word32 -> NetworkId -> NetworkId
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NetworkMagic -> NetworkId
Testnet (NetworkMagic -> NetworkId)
-> (Word32 -> NetworkMagic) -> Word32 -> NetworkId
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word32 -> NetworkMagic
NetworkMagic) Maybe Word32
ccNetworkId)
  (ChainIndexConfig -> ChainIndexConfig)
-> (ChainIndexConfig -> ChainIndexConfig)
-> ChainIndexConfig
-> ChainIndexConfig
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ASetter ChainIndexConfig ChainIndexConfig Natural Natural
-> (Natural -> Natural) -> ChainIndexConfig -> ChainIndexConfig
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
over ASetter ChainIndexConfig ChainIndexConfig Natural Natural
Lens' ChainIndexConfig Natural
Config.appendTransactionQueueSize ((Natural -> Natural)
-> (Natural -> Natural -> Natural)
-> Maybe Natural
-> Natural
-> Natural
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Natural -> Natural
forall a. a -> a
id Natural -> Natural -> Natural
forall a b. a -> b -> a
const Maybe Natural
ccAppendTransactionQueueSize)

-- | Configuration
data Command =
    DumpDefaultConfig { Command -> String
dumpConfigPath :: !FilePath }
    -- ^ Write default chain index configuration
  | DumpDefaultLoggingConfig { Command -> String
dumpLoggingConfigPath :: !FilePath }
    -- ^ Write default logging configuration
  | StartChainIndex
    -- ^ Start the chain index and connect it to a cardano node
    deriving (Int -> Command -> ShowS
[Command] -> ShowS
Command -> String
(Int -> Command -> ShowS)
-> (Command -> String) -> ([Command] -> ShowS) -> Show Command
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Command] -> ShowS
$cshowList :: [Command] -> ShowS
show :: Command -> String
$cshow :: Command -> String
showsPrec :: Int -> Command -> ShowS
$cshowsPrec :: Int -> Command -> ShowS
Show)

data AppConfig = AppConfig
  { AppConfig -> Maybe String
acLogConfigPath      :: Maybe FilePath
  , AppConfig -> Maybe Severity
acMinLogLevel        :: Maybe Severity
  , AppConfig -> Maybe String
acConfigPath         :: Maybe FilePath
  , AppConfig -> CLIConfigOverrides
acCLIConfigOverrides :: CLIConfigOverrides
  , AppConfig -> Command
acCommand            :: Command
  } deriving (Int -> AppConfig -> ShowS
[AppConfig] -> ShowS
AppConfig -> String
(Int -> AppConfig -> ShowS)
-> (AppConfig -> String)
-> ([AppConfig] -> ShowS)
-> Show AppConfig
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [AppConfig] -> ShowS
$cshowList :: [AppConfig] -> ShowS
show :: AppConfig -> String
$cshow :: AppConfig -> String
showsPrec :: Int -> AppConfig -> ShowS
$cshowsPrec :: Int -> AppConfig -> ShowS
Show)

-- | Command line parser
optParser :: Parser AppConfig
optParser :: Parser AppConfig
optParser =
  Maybe String
-> Maybe Severity
-> Maybe String
-> CLIConfigOverrides
-> Command
-> AppConfig
AppConfig
    (Maybe String
 -> Maybe Severity
 -> Maybe String
 -> CLIConfigOverrides
 -> Command
 -> AppConfig)
-> Parser (Maybe String)
-> Parser
     (Maybe Severity
      -> Maybe String -> CLIConfigOverrides -> Command -> AppConfig)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser (Maybe String)
loggingConfigParser
    Parser
  (Maybe Severity
   -> Maybe String -> CLIConfigOverrides -> Command -> AppConfig)
-> Parser (Maybe Severity)
-> Parser
     (Maybe String -> CLIConfigOverrides -> Command -> AppConfig)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser (Maybe Severity)
debuggingOutputParser
    Parser (Maybe String -> CLIConfigOverrides -> Command -> AppConfig)
-> Parser (Maybe String)
-> Parser (CLIConfigOverrides -> Command -> AppConfig)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser (Maybe String)
configParser
    Parser (CLIConfigOverrides -> Command -> AppConfig)
-> Parser CLIConfigOverrides -> Parser (Command -> AppConfig)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser CLIConfigOverrides
cliConfigOverridesParser
    Parser (Command -> AppConfig) -> Parser Command -> Parser AppConfig
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser Command
commandParser

cliConfigOverridesParser :: Parser CLIConfigOverrides
cliConfigOverridesParser :: Parser CLIConfigOverrides
cliConfigOverridesParser =
  Maybe String
-> Maybe String
-> Maybe Int
-> Maybe Word32
-> Maybe Natural
-> CLIConfigOverrides
CLIConfigOverrides (Maybe String
 -> Maybe String
 -> Maybe Int
 -> Maybe Word32
 -> Maybe Natural
 -> CLIConfigOverrides)
-> Parser (Maybe String)
-> Parser
     (Maybe String
      -> Maybe Int
      -> Maybe Word32
      -> Maybe Natural
      -> CLIConfigOverrides)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser (Maybe String)
socketPathParser Parser
  (Maybe String
   -> Maybe Int
   -> Maybe Word32
   -> Maybe Natural
   -> CLIConfigOverrides)
-> Parser (Maybe String)
-> Parser
     (Maybe Int -> Maybe Word32 -> Maybe Natural -> CLIConfigOverrides)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser (Maybe String)
dbPathParser Parser
  (Maybe Int -> Maybe Word32 -> Maybe Natural -> CLIConfigOverrides)
-> Parser (Maybe Int)
-> Parser (Maybe Word32 -> Maybe Natural -> CLIConfigOverrides)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser (Maybe Int)
portParser Parser (Maybe Word32 -> Maybe Natural -> CLIConfigOverrides)
-> Parser (Maybe Word32)
-> Parser (Maybe Natural -> CLIConfigOverrides)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser (Maybe Word32)
networkIDParser Parser (Maybe Natural -> CLIConfigOverrides)
-> Parser (Maybe Natural) -> Parser CLIConfigOverrides
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser (Maybe Natural)
appendTransactionQueueSizeParser where
    socketPathParser :: Parser (Maybe String)
socketPathParser =
      ReadM (Maybe String)
-> Mod OptionFields (Maybe String) -> Parser (Maybe String)
forall a. ReadM a -> Mod OptionFields a -> Parser a
option (String -> Maybe String
forall a. a -> Maybe a
Just (String -> Maybe String) -> ReadM String -> ReadM (Maybe String)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ReadM String
forall s. IsString s => ReadM s
str) (String -> Mod OptionFields (Maybe String)
forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"socket-path" Mod OptionFields (Maybe String)
-> Mod OptionFields (Maybe String)
-> Mod OptionFields (Maybe String)
forall a. Semigroup a => a -> a -> a
<> Maybe String -> Mod OptionFields (Maybe String)
forall (f :: * -> *) a. HasValue f => a -> Mod f a
value Maybe String
forall a. Maybe a
Nothing Mod OptionFields (Maybe String)
-> Mod OptionFields (Maybe String)
-> Mod OptionFields (Maybe String)
forall a. Semigroup a => a -> a -> a
<> String -> Mod OptionFields (Maybe String)
forall (f :: * -> *) a. String -> Mod f a
help String
"Node socket path")
    dbPathParser :: Parser (Maybe String)
dbPathParser =
      ReadM (Maybe String)
-> Mod OptionFields (Maybe String) -> Parser (Maybe String)
forall a. ReadM a -> Mod OptionFields a -> Parser a
option (String -> Maybe String
forall a. a -> Maybe a
Just (String -> Maybe String) -> ReadM String -> ReadM (Maybe String)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ReadM String
forall s. IsString s => ReadM s
str) (String -> Mod OptionFields (Maybe String)
forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"db-path" Mod OptionFields (Maybe String)
-> Mod OptionFields (Maybe String)
-> Mod OptionFields (Maybe String)
forall a. Semigroup a => a -> a -> a
<> Maybe String -> Mod OptionFields (Maybe String)
forall (f :: * -> *) a. HasValue f => a -> Mod f a
value Maybe String
forall a. Maybe a
Nothing Mod OptionFields (Maybe String)
-> Mod OptionFields (Maybe String)
-> Mod OptionFields (Maybe String)
forall a. Semigroup a => a -> a -> a
<> String -> Mod OptionFields (Maybe String)
forall (f :: * -> *) a. String -> Mod f a
help String
"Sqlite database file path")
    portParser :: Parser (Maybe Int)
portParser =
      ReadM (Maybe Int)
-> Mod OptionFields (Maybe Int) -> Parser (Maybe Int)
forall a. ReadM a -> Mod OptionFields a -> Parser a
option (Int -> Maybe Int
forall a. a -> Maybe a
Just (Int -> Maybe Int) -> ReadM Int -> ReadM (Maybe Int)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ReadM Int
forall a. Read a => ReadM a
auto) (String -> Mod OptionFields (Maybe Int)
forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"port" Mod OptionFields (Maybe Int)
-> Mod OptionFields (Maybe Int) -> Mod OptionFields (Maybe Int)
forall a. Semigroup a => a -> a -> a
<> Maybe Int -> Mod OptionFields (Maybe Int)
forall (f :: * -> *) a. HasValue f => a -> Mod f a
value Maybe Int
forall a. Maybe a
Nothing Mod OptionFields (Maybe Int)
-> Mod OptionFields (Maybe Int) -> Mod OptionFields (Maybe Int)
forall a. Semigroup a => a -> a -> a
<> String -> Mod OptionFields (Maybe Int)
forall (f :: * -> *) a. String -> Mod f a
help String
"Port")
    networkIDParser :: Parser (Maybe Word32)
networkIDParser =
      ReadM (Maybe Word32)
-> Mod OptionFields (Maybe Word32) -> Parser (Maybe Word32)
forall a. ReadM a -> Mod OptionFields a -> Parser a
option (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just (Word32 -> Maybe Word32) -> ReadM Word32 -> ReadM (Maybe Word32)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ReadM Word32
forall a. Read a => ReadM a
auto) (String -> Mod OptionFields (Maybe Word32)
forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"network-id" Mod OptionFields (Maybe Word32)
-> Mod OptionFields (Maybe Word32)
-> Mod OptionFields (Maybe Word32)
forall a. Semigroup a => a -> a -> a
<> Maybe Word32 -> Mod OptionFields (Maybe Word32)
forall (f :: * -> *) a. HasValue f => a -> Mod f a
value Maybe Word32
forall a. Maybe a
Nothing Mod OptionFields (Maybe Word32)
-> Mod OptionFields (Maybe Word32)
-> Mod OptionFields (Maybe Word32)
forall a. Semigroup a => a -> a -> a
<> String -> Mod OptionFields (Maybe Word32)
forall (f :: * -> *) a. String -> Mod f a
help String
"Network ID")
    appendTransactionQueueSizeParser :: Parser (Maybe Natural)
appendTransactionQueueSizeParser =
      ReadM (Maybe Natural)
-> Mod OptionFields (Maybe Natural) -> Parser (Maybe Natural)
forall a. ReadM a -> Mod OptionFields a -> Parser a
option (Natural -> Maybe Natural
forall a. a -> Maybe a
Just (Natural -> Maybe Natural)
-> ReadM Natural -> ReadM (Maybe Natural)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ReadM Natural
forall a. Read a => ReadM a
auto) (String -> Mod OptionFields (Maybe Natural)
forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"append-transaction-queue-size" Mod OptionFields (Maybe Natural)
-> Mod OptionFields (Maybe Natural)
-> Mod OptionFields (Maybe Natural)
forall a. Semigroup a => a -> a -> a
<> Maybe Natural -> Mod OptionFields (Maybe Natural)
forall (f :: * -> *) a. HasValue f => a -> Mod f a
value Maybe Natural
forall a. Maybe a
Nothing Mod OptionFields (Maybe Natural)
-> Mod OptionFields (Maybe Natural)
-> Mod OptionFields (Maybe Natural)
forall a. Semigroup a => a -> a -> a
<> String -> Mod OptionFields (Maybe Natural)
forall (f :: * -> *) a. String -> Mod f a
help String
"Append transaction queue size")

loggingConfigParser :: Parser (Maybe FilePath)
loggingConfigParser :: Parser (Maybe String)
loggingConfigParser =
  ReadM (Maybe String)
-> Mod OptionFields (Maybe String) -> Parser (Maybe String)
forall a. ReadM a -> Mod OptionFields a -> Parser a
option ( String -> Maybe String
forall a. a -> Maybe a
Just (String -> Maybe String) -> ReadM String -> ReadM (Maybe String)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ReadM String
forall s. IsString s => ReadM s
str)
         ( String -> Mod OptionFields (Maybe String)
forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"log-config"
        Mod OptionFields (Maybe String)
-> Mod OptionFields (Maybe String)
-> Mod OptionFields (Maybe String)
forall a. Semigroup a => a -> a -> a
<> String -> Mod OptionFields (Maybe String)
forall (f :: * -> *) a. HasMetavar f => String -> Mod f a
metavar String
"LOGGING"
        Mod OptionFields (Maybe String)
-> Mod OptionFields (Maybe String)
-> Mod OptionFields (Maybe String)
forall a. Semigroup a => a -> a -> a
<> Maybe String -> Mod OptionFields (Maybe String)
forall (f :: * -> *) a. HasValue f => a -> Mod f a
value Maybe String
forall a. Maybe a
Nothing
        Mod OptionFields (Maybe String)
-> Mod OptionFields (Maybe String)
-> Mod OptionFields (Maybe String)
forall a. Semigroup a => a -> a -> a
<> Char -> Mod OptionFields (Maybe String)
forall (f :: * -> *) a. HasName f => Char -> Mod f a
short Char
'l'
        Mod OptionFields (Maybe String)
-> Mod OptionFields (Maybe String)
-> Mod OptionFields (Maybe String)
forall a. Semigroup a => a -> a -> a
<> String -> Mod OptionFields (Maybe String)
forall (f :: * -> *) a. String -> Mod f a
help String
"Path to the logging configuration file." )

configParser :: Parser (Maybe FilePath)
configParser :: Parser (Maybe String)
configParser =
  ReadM (Maybe String)
-> Mod OptionFields (Maybe String) -> Parser (Maybe String)
forall a. ReadM a -> Mod OptionFields a -> Parser a
option ( String -> Maybe String
forall a. a -> Maybe a
Just (String -> Maybe String) -> ReadM String -> ReadM (Maybe String)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ReadM String
forall s. IsString s => ReadM s
str)
         ( String -> Mod OptionFields (Maybe String)
forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"config"
        Mod OptionFields (Maybe String)
-> Mod OptionFields (Maybe String)
-> Mod OptionFields (Maybe String)
forall a. Semigroup a => a -> a -> a
<> String -> Mod OptionFields (Maybe String)
forall (f :: * -> *) a. HasMetavar f => String -> Mod f a
metavar String
"CONFIG"
        Mod OptionFields (Maybe String)
-> Mod OptionFields (Maybe String)
-> Mod OptionFields (Maybe String)
forall a. Semigroup a => a -> a -> a
<> Maybe String -> Mod OptionFields (Maybe String)
forall (f :: * -> *) a. HasValue f => a -> Mod f a
value Maybe String
forall a. Maybe a
Nothing
        Mod OptionFields (Maybe String)
-> Mod OptionFields (Maybe String)
-> Mod OptionFields (Maybe String)
forall a. Semigroup a => a -> a -> a
<> Char -> Mod OptionFields (Maybe String)
forall (f :: * -> *) a. HasName f => Char -> Mod f a
short Char
'c'
        Mod OptionFields (Maybe String)
-> Mod OptionFields (Maybe String)
-> Mod OptionFields (Maybe String)
forall a. Semigroup a => a -> a -> a
<> String -> Mod OptionFields (Maybe String)
forall (f :: * -> *) a. String -> Mod f a
help String
"Path to the chain index configuration file." )

debuggingOutputParser :: Parser (Maybe Severity)
debuggingOutputParser :: Parser (Maybe Severity)
debuggingOutputParser =
  Maybe Severity
-> Maybe Severity
-> Mod FlagFields (Maybe Severity)
-> Parser (Maybe Severity)
forall a. a -> a -> Mod FlagFields a -> Parser a
flag Maybe Severity
forall a. Maybe a
Nothing
       (Severity -> Maybe Severity
forall a. a -> Maybe a
Just Severity
Debug)
       ( String -> Mod FlagFields (Maybe Severity)
forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"verbose"
      Mod FlagFields (Maybe Severity)
-> Mod FlagFields (Maybe Severity)
-> Mod FlagFields (Maybe Severity)
forall a. Semigroup a => a -> a -> a
<> Char -> Mod FlagFields (Maybe Severity)
forall (f :: * -> *) a. HasName f => Char -> Mod f a
short Char
'v'
      Mod FlagFields (Maybe Severity)
-> Mod FlagFields (Maybe Severity)
-> Mod FlagFields (Maybe Severity)
forall a. Semigroup a => a -> a -> a
<> String -> Mod FlagFields (Maybe Severity)
forall (f :: * -> *) a. String -> Mod f a
help String
"Enable debugging output" )

cmdWithHelpParser :: ParserInfo AppConfig
cmdWithHelpParser :: ParserInfo AppConfig
cmdWithHelpParser =
  Parser AppConfig -> InfoMod AppConfig -> ParserInfo AppConfig
forall a. Parser a -> InfoMod a -> ParserInfo a
info (Parser AppConfig
optParser Parser AppConfig
-> Parser (AppConfig -> AppConfig) -> Parser AppConfig
forall (f :: * -> *) a b. Applicative f => f a -> f (a -> b) -> f b
<**> Parser (AppConfig -> AppConfig)
forall a. Parser (a -> a)
helper)
       ( InfoMod AppConfig
forall a. InfoMod a
fullDesc
      InfoMod AppConfig -> InfoMod AppConfig -> InfoMod AppConfig
forall a. Semigroup a => a -> a -> a
<> String -> InfoMod AppConfig
forall a. String -> InfoMod a
progDesc String
"Start the chain index process and connect to the node"
      InfoMod AppConfig -> InfoMod AppConfig -> InfoMod AppConfig
forall a. Semigroup a => a -> a -> a
<> String -> InfoMod AppConfig
forall a. String -> InfoMod a
header String
"chain-index - the chain index server" )

commandParser :: Parser Command
commandParser :: Parser Command
commandParser =
  Mod CommandFields Command -> Parser Command
forall a. Mod CommandFields a -> Parser a
hsubparser (Mod CommandFields Command -> Parser Command)
-> Mod CommandFields Command -> Parser Command
forall a b. (a -> b) -> a -> b
$
  [Mod CommandFields Command] -> Mod CommandFields Command
forall a. Monoid a => [a] -> a
mconcat
    [ Mod CommandFields Command
dumpDefaultConfigParser
    , Mod CommandFields Command
dumpDefaultLoggingConfigParser
    , Mod CommandFields Command
startChainIndexParser
    ]

dumpDefaultConfigParser :: Mod CommandFields Command
dumpDefaultConfigParser :: Mod CommandFields Command
dumpDefaultConfigParser =
  String -> ParserInfo Command -> Mod CommandFields Command
forall a. String -> ParserInfo a -> Mod CommandFields a
command String
"default-config" (ParserInfo Command -> Mod CommandFields Command)
-> ParserInfo Command -> Mod CommandFields Command
forall a b. (a -> b) -> a -> b
$
  (Parser Command -> InfoMod Command -> ParserInfo Command)
-> InfoMod Command -> Parser Command -> ParserInfo Command
forall a b c. (a -> b -> c) -> b -> a -> c
flip Parser Command -> InfoMod Command -> ParserInfo Command
forall a. Parser a -> InfoMod a -> ParserInfo a
info (InfoMod Command
forall a. InfoMod a
fullDesc InfoMod Command -> InfoMod Command -> InfoMod Command
forall a. Semigroup a => a -> a -> a
<> String -> InfoMod Command
forall a. String -> InfoMod a
progDesc String
"Write the default chain index JSON configuration to a file") (Parser Command -> ParserInfo Command)
-> Parser Command -> ParserInfo Command
forall a b. (a -> b) -> a -> b
$
    String -> Command
DumpDefaultConfig (String -> Command) -> Parser String -> Parser Command
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
      ReadM String -> Mod ArgumentFields String -> Parser String
forall a. ReadM a -> Mod ArgumentFields a -> Parser a
argument ReadM String
forall s. IsString s => ReadM s
str
        (String -> Mod ArgumentFields String
forall (f :: * -> *) a. HasMetavar f => String -> Mod f a
metavar String
"OUTPUT_FILE" Mod ArgumentFields String
-> Mod ArgumentFields String -> Mod ArgumentFields String
forall a. Semigroup a => a -> a -> a
<> String -> Mod ArgumentFields String
forall (f :: * -> *) a. String -> Mod f a
help String
"Output JSON file to write chain index config to.")

dumpDefaultLoggingConfigParser :: Mod CommandFields Command
dumpDefaultLoggingConfigParser :: Mod CommandFields Command
dumpDefaultLoggingConfigParser =
  String -> ParserInfo Command -> Mod CommandFields Command
forall a. String -> ParserInfo a -> Mod CommandFields a
command String
"default-logging-config" (ParserInfo Command -> Mod CommandFields Command)
-> ParserInfo Command -> Mod CommandFields Command
forall a b. (a -> b) -> a -> b
$
  (Parser Command -> InfoMod Command -> ParserInfo Command)
-> InfoMod Command -> Parser Command -> ParserInfo Command
forall a b c. (a -> b -> c) -> b -> a -> c
flip Parser Command -> InfoMod Command -> ParserInfo Command
forall a. Parser a -> InfoMod a -> ParserInfo a
info (InfoMod Command
forall a. InfoMod a
fullDesc InfoMod Command -> InfoMod Command -> InfoMod Command
forall a. Semigroup a => a -> a -> a
<> String -> InfoMod Command
forall a. String -> InfoMod a
progDesc String
"Write the default logging YAML configuration to a file") (Parser Command -> ParserInfo Command)
-> Parser Command -> ParserInfo Command
forall a b. (a -> b) -> a -> b
$
    String -> Command
DumpDefaultLoggingConfig (String -> Command) -> Parser String -> Parser Command
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
      ReadM String -> Mod ArgumentFields String -> Parser String
forall a. ReadM a -> Mod ArgumentFields a -> Parser a
argument ReadM String
forall s. IsString s => ReadM s
str
        (String -> Mod ArgumentFields String
forall (f :: * -> *) a. HasMetavar f => String -> Mod f a
metavar String
"OUTPUT_FILE" Mod ArgumentFields String
-> Mod ArgumentFields String -> Mod ArgumentFields String
forall a. Semigroup a => a -> a -> a
<> String -> Mod ArgumentFields String
forall (f :: * -> *) a. String -> Mod f a
help String
"Output YAML file to write logging config to.")

startChainIndexParser :: Mod CommandFields Command
startChainIndexParser :: Mod CommandFields Command
startChainIndexParser =
  String -> ParserInfo Command -> Mod CommandFields Command
forall a. String -> ParserInfo a -> Mod CommandFields a
command String
"start-index" (ParserInfo Command -> Mod CommandFields Command)
-> ParserInfo Command -> Mod CommandFields Command
forall a b. (a -> b) -> a -> b
$
  (Parser Command -> InfoMod Command -> ParserInfo Command)
-> InfoMod Command -> Parser Command -> ParserInfo Command
forall a b c. (a -> b -> c) -> b -> a -> c
flip Parser Command -> InfoMod Command -> ParserInfo Command
forall a. Parser a -> InfoMod a -> ParserInfo a
info (InfoMod Command
forall a. InfoMod a
fullDesc InfoMod Command -> InfoMod Command -> InfoMod Command
forall a. Semigroup a => a -> a -> a
<> String -> InfoMod Command
forall a. String -> InfoMod a
progDesc String
"Start the chain index and connect it to a cardano node") (Parser Command -> ParserInfo Command)
-> Parser Command -> ParserInfo Command
forall a b. (a -> b) -> a -> b
$ do
    Command -> Parser Command
forall (f :: * -> *) a. Applicative f => a -> f a
pure Command
StartChainIndex