diff --git a/src/exe/wst-poc-cli/Main.hs b/src/exe/wst-poc-cli/Main.hs new file mode 100644 index 0000000..7440fd1 --- /dev/null +++ b/src/exe/wst-poc-cli/Main.hs @@ -0,0 +1,6 @@ +module Main where + +import Wst.Cli qualified + +main :: IO () +main = Wst.Cli.runMain diff --git a/src/exe/wst-poc/Main.hs b/src/exe/wst-poc/Main.hs deleted file mode 100644 index a1246b9..0000000 --- a/src/exe/wst-poc/Main.hs +++ /dev/null @@ -1,6 +0,0 @@ -module Main where - -import qualified Wst.Cli - -main :: IO () -main = Wst.Cli.runMain \ No newline at end of file diff --git a/src/lib/Wst/Cli.hs b/src/lib/Wst/Cli.hs index dad94ab..a2a8629 100644 --- a/src/lib/Wst/Cli.hs +++ b/src/lib/Wst/Cli.hs @@ -1,4 +1,17 @@ module Wst.Cli(runMain) where +import Options.Applicative (customExecParser, disambiguate, helper, idm, info, + prefs, showHelpOnEmpty, showHelpOnError) +import Wst.Cli.Command (Command, parseCommand) + runMain :: IO () -runMain = putStrLn "Starting stablecoin POC server" +runMain = do + customExecParser + (prefs $ disambiguate <> showHelpOnEmpty <> showHelpOnError) + (info (helper <*> parseCommand) idm) + >>= runCommand + +runCommand :: Command -> IO () +runCommand com = do + putStrLn "runCommand" + print com diff --git a/src/lib/Wst/Cli/Command.hs b/src/lib/Wst/Cli/Command.hs new file mode 100644 index 0000000..35ce3ac --- /dev/null +++ b/src/lib/Wst/Cli/Command.hs @@ -0,0 +1,71 @@ +{-| The CLI commands and parsers +-} +module Wst.Cli.Command( + parseCommand, + Command(..), + ManageCommand(..) +) where + +import Cardano.Api (TxIn (..), TxIx (..)) +import Control.Monad (when) +import Data.String (IsString (..)) +import Options.Applicative (CommandFields, Mod, Parser, ReadM, argument, + command, eitherReader, fullDesc, help, info, long, + many, metavar, optional, progDesc, short, str, + strOption, subparser, (<|>)) +import Text.Read (readMaybe) + + +parseCommand :: Parser Command +parseCommand = + subparser $ + mconcat + [ parseDeploy + , parseManage + ] + +data Command = + Deploy + | Manage TxIn ManageCommand + deriving Show + +-- | Commands that require a deployed system +data ManageCommand = + Status + deriving stock Show + +parseDeploy :: Mod CommandFields Command +parseDeploy = + command "deploy" $ + info (pure Deploy) (fullDesc <> progDesc "Deploy the directory and global params") + +parseManage :: Mod CommandFields Command +parseManage = + command "manage" $ + info (Manage <$> parseTxIn <*> parseManageCommand) (fullDesc <> progDesc "Manage a deployed system") + +parseManageCommand :: Parser ManageCommand +parseManageCommand = subparser $ mconcat [parseStatus] + +parseStatus :: Mod CommandFields ManageCommand +parseStatus = + command "status" $ + info (pure Status) (fullDesc <> progDesc "Show the status of the programmable tokens") + +parseTxIn :: Parser TxIn +parseTxIn = + argument + txInReader + (help "The TxIn that was selected when deploying the system. Format: ." <> metavar "TX_IN") + +txInReader :: ReadM TxIn +txInReader = eitherReader $ \str -> do + (txId, txIx) <- case break ((==) '.') str of + (txId, _:txIx) -> Right (txId, txIx) + _ -> Left "Expected ." + -- 8c728a68fed42fe4893fb84ee2c6276a25e642d9892962af3234f952ea641993 + when (length txId /= 64) $ Left "Expected tx ID with 64 characters" + ix <- case readMaybe @Word txIx of + Nothing -> Left "Expected tx index" + Just n -> Right (TxIx n) + return $ TxIn (fromString txId) ix diff --git a/src/wst-poc.cabal b/src/wst-poc.cabal index b2ba695..3607c1c 100644 --- a/src/wst-poc.cabal +++ b/src/wst-poc.cabal @@ -69,6 +69,7 @@ library SmartTokens.Types.PTokenDirectory Types.Constants Wst.Cli + Wst.Cli.Command Wst.Client Wst.Offchain Wst.Offchain.BuildTx.Blacklist @@ -104,6 +105,7 @@ library , generics-sop , lens , mtl + , optparse-applicative , plutarch , plutarch-ledger-api , plutarch-onchain-lib @@ -119,10 +121,10 @@ library hs-source-dirs: lib -executable wst-poc +executable wst-poc-cli import: lang main-is: Main.hs - hs-source-dirs: exe/wst-poc + hs-source-dirs: exe/wst-poc-cli build-depends: , base , wst-poc