-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
95 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module Main where | ||
|
||
import Wst.Cli qualified | ||
|
||
main :: IO () | ||
main = Wst.Cli.runMain |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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: <tx-id>.<index>" <> metavar "TX_IN") | ||
|
||
txInReader :: ReadM TxIn | ||
txInReader = eitherReader $ \str -> do | ||
(txId, txIx) <- case break ((==) '.') str of | ||
(txId, _:txIx) -> Right (txId, txIx) | ||
_ -> Left "Expected <tx-id>.<index>" | ||
-- 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters