{-# LANGUAGE DataKinds #-}
{-| Commands for the wallet CLI
-}
module Convex.Wallet.Cli.Command(
  CliCommand(..),
  commandParser
  ) where

import           Convex.Wallet.Cli.Config (Config, configParser)
import           Convex.Wallet.Operator   (OperatorConfigVerification,
                                           parseOperatorConfigVerification)
import           Options.Applicative      (CommandFields, Mod, Parser, auto,
                                           command, fullDesc, help, info, long,
                                           option, progDesc, strOption,
                                           subparser, value)

data CliCommand =
  GenerateWallet
  | GenerateSigningKey{CliCommand -> FilePath
verificationKeyFile :: FilePath, CliCommand -> FilePath
signingKeyFile :: FilePath }
  | RunWallet Config OperatorConfigVerification Int
  | ShowAddress Config OperatorConfigVerification

commandParser :: Parser CliCommand
commandParser :: Parser CliCommand
commandParser =
  forall a. Mod CommandFields a -> Parser a
subparser forall a b. (a -> b) -> a -> b
$
    forall a. Monoid a => [a] -> a
mconcat
      [ Mod CommandFields CliCommand
generateWallet
      , Mod CommandFields CliCommand
generateSigningKey
      , Mod CommandFields CliCommand
runWallet
      , Mod CommandFields CliCommand
showAddress
      ]

generateWallet :: Mod CommandFields CliCommand
generateWallet :: Mod CommandFields CliCommand
generateWallet = forall a. FilePath -> ParserInfo a -> Mod CommandFields a
command FilePath
"generate-wallet" forall a b. (a -> b) -> a -> b
$
  forall a. Parser a -> InfoMod a -> ParserInfo a
info (forall (f :: * -> *) a. Applicative f => a -> f a
pure CliCommand
GenerateWallet) (forall a. InfoMod a
fullDesc forall a. Semigroup a => a -> a -> a
<> forall a. FilePath -> InfoMod a
progDesc FilePath
"Generate a private key for a wallet")

generateSigningKey :: Mod CommandFields CliCommand
generateSigningKey :: Mod CommandFields CliCommand
generateSigningKey = forall a. FilePath -> ParserInfo a -> Mod CommandFields a
command FilePath
"generate-signing-key" forall a b. (a -> b) -> a -> b
$
  forall a. Parser a -> InfoMod a -> ParserInfo a
info (FilePath -> FilePath -> CliCommand
GenerateSigningKey forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser FilePath
verificationKeyFileParser forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser FilePath
signingKeyFileParser) (forall a. InfoMod a
fullDesc forall a. Semigroup a => a -> a -> a
<> forall a. FilePath -> InfoMod a
progDesc FilePath
"Generate a signing and verification key pair")

runWallet :: Mod CommandFields CliCommand
runWallet :: Mod CommandFields CliCommand
runWallet = forall a. FilePath -> ParserInfo a -> Mod CommandFields a
command FilePath
"run-wallet" forall a b. (a -> b) -> a -> b
$
  forall a. Parser a -> InfoMod a -> ParserInfo a
info (Config -> OperatorConfigVerification -> Int -> CliCommand
RunWallet forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser Config
configParser forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser OperatorConfigVerification
parseOperatorConfigVerification forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser Int
portParser) (forall a. InfoMod a
fullDesc forall a. Semigroup a => a -> a -> a
<> forall a. FilePath -> InfoMod a
progDesc FilePath
"Start the wallet")

showAddress :: Mod CommandFields CliCommand
showAddress :: Mod CommandFields CliCommand
showAddress = forall a. FilePath -> ParserInfo a -> Mod CommandFields a
command FilePath
"show-address" forall a b. (a -> b) -> a -> b
$
  forall a. Parser a -> InfoMod a -> ParserInfo a
info (Config -> OperatorConfigVerification -> CliCommand
ShowAddress forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser Config
configParser forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser OperatorConfigVerification
parseOperatorConfigVerification) (forall a. InfoMod a
fullDesc forall a. Semigroup a => a -> a -> a
<> forall a. FilePath -> InfoMod a
progDesc FilePath
"Show the wallet's address")

portParser :: Parser Int
portParser :: Parser Int
portParser =
  forall a. ReadM a -> Mod OptionFields a -> Parser a
option forall a. Read a => ReadM a
auto
  (forall (f :: * -> *) a. HasName f => FilePath -> Mod f a
long FilePath
"http.port" forall a. Semigroup a => a -> a -> a
<> forall (f :: * -> *) a. HasValue f => a -> Mod f a
value Int
9988 forall a. Semigroup a => a -> a -> a
<> forall (f :: * -> *) a. FilePath -> Mod f a
help FilePath
"The port of the wallet HTTP server")

verificationKeyFileParser :: Parser FilePath
verificationKeyFileParser :: Parser FilePath
verificationKeyFileParser = forall s. IsString s => Mod OptionFields s -> Parser s
strOption (forall (f :: * -> *) a. HasName f => FilePath -> Mod f a
long FilePath
"verification.file" forall a. Semigroup a => a -> a -> a
<> forall (f :: * -> *) a. FilePath -> Mod f a
help FilePath
"File for the verification key")

signingKeyFileParser :: Parser FilePath
signingKeyFileParser :: Parser FilePath
signingKeyFileParser = forall s. IsString s => Mod OptionFields s -> Parser s
strOption (forall (f :: * -> *) a. HasName f => FilePath -> Mod f a
long FilePath
"signing.file" forall a. Semigroup a => a -> a -> a
<> forall (f :: * -> *) a. FilePath -> Mod f a
help FilePath
"File for the signing key")