Skip to content

Commit d912d28

Browse files
committed
Add the short option to poll
1 parent b996c8e commit d912d28

File tree

5 files changed

+50
-25
lines changed

5 files changed

+50
-25
lines changed

kda-tool.cabal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ library
4141
Types.Node
4242
Types.TxInputs
4343
Utils
44+
Output
4445

4546
build-depends:
4647
Decimal
@@ -198,3 +199,4 @@ test-suite kda-tool-tests
198199
Types.KeyType
199200
Types.TxInputs
200201
Utils
202+
Output

src/Commands/Local.hs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,24 @@ module Commands.Local
77

88
------------------------------------------------------------------------------
99
import Control.Error
10-
import Control.Lens hiding ((.=))
1110
import Control.Monad
1211
import Control.Monad.Trans
1312
import Data.Aeson
1413
import Data.Aeson.Key
15-
import Data.Aeson.Lens
1614
import Data.Bifunctor
1715
import qualified Data.ByteString.Lazy as LB
1816
import Data.Function
1917
import Data.List
2018
import qualified Data.List.NonEmpty as NE
2119
import Data.Ord
22-
import Data.String.Conv
2320
import Katip
24-
import System.Exit
2521
import Text.Printf
2622
------------------------------------------------------------------------------
2723
import Types.Env
2824
import Types.HostPort
2925
import Types.Node
3026
import Utils
27+
import Output
3128
------------------------------------------------------------------------------
3229

3330
localCommand :: Env -> LocalCmdArgs -> IO ()
@@ -49,13 +46,4 @@ localCommand e (LocalCmdArgs args verifySigs shortOutput) = do
4946
(schemeHostPortToText shp) (length txs) (length groups)
5047
responses <- lift $ mapM (localNodeQuery le verifySigs n) txs
5148
pure $ fromText (schemeHostPortToText shp) .= map responseToValue responses
52-
case res of
53-
Left er -> putStrLn er >> exitFailure
54-
Right results -> do
55-
let out = Object $ mconcat results
56-
let status = out ^.. _Object . traverse . _Array . traverse . key "body" . key "result" . key "status" . _String
57-
if shortOutput
58-
then putStrLn $ toS $ encode status
59-
else putStrLn $ toS $ encode out
60-
when (any (/="success") status) $
61-
exitWith (ExitFailure 2)
49+
outputEitherResults shortOutput res

src/Commands/Poll.hs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,18 @@ import Data.Function
1717
import Data.List
1818
import qualified Data.List.NonEmpty as NE
1919
import Data.Ord
20-
import Data.String.Conv
21-
import qualified Data.Text.IO as T
2220
import Katip
23-
import System.Exit
2421
import Text.Printf
2522
------------------------------------------------------------------------------
2623
import Types.Env
2724
import Types.HostPort
2825
import Types.Node
2926
import Utils
27+
import Output
3028
------------------------------------------------------------------------------
3129

32-
pollCommand :: Env -> NodeTxCmdArgs -> IO ()
33-
pollCommand e args = do
30+
pollCommand :: Env -> PollCmdArgs -> IO ()
31+
pollCommand e (PollCmdArgs args shortOutput) = do
3432
let le = _env_logEnv e
3533
case _nodeTxCmdArgs_files args of
3634
[] -> putStrLn "No tx files specified"
@@ -48,7 +46,4 @@ pollCommand e args = do
4846
(schemeHostPortToText shp) (length txs) (length groups)
4947
responses <- lift $ mapM (\ts -> pollNode le n (txChain $ NE.head ts) (_transaction_hash <$> ts)) groups
5048
pure $ fromText (schemeHostPortToText shp) .= map responseToValue responses
51-
case res of
52-
Left er -> putStrLn er >> exitFailure
53-
Right results -> T.putStrLn $ toS $ encode $ Object $ mconcat results
54-
49+
outputEitherResults shortOutput res

src/Output.hs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{-# LANGUAGE OverloadedStrings #-}
2+
3+
module Output
4+
(
5+
outputResults
6+
, outputEitherResults
7+
) where
8+
9+
------------------------------------------------------------------------------
10+
import Data.Aeson
11+
import Control.Lens hiding ((.=))
12+
import Data.Aeson.Lens
13+
import Data.String.Conv
14+
import Control.Monad
15+
import System.Exit
16+
------------------------------------------------------------------------------
17+
18+
outputResults:: Bool -> [Object] -> IO ()
19+
outputResults shortOutput results = do
20+
let out = Object $ mconcat results
21+
let status = out ^.. cosmos . key "result" . key "status" . _String
22+
if shortOutput
23+
then putStrLn $ toS $ encode status
24+
else putStrLn $ toS $ encode out
25+
--when (any (/="success") status) $
26+
-- exitWith (ExitFailure 2)
27+
28+
outputEitherResults:: Bool -> Either String [Object] -> IO ()
29+
outputEitherResults shortOutput eRes =
30+
case eRes of
31+
Left er -> putStrLn er >> exitFailure
32+
Right results -> outputResults shortOutput results

src/Types/Env.hs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,11 @@ data LocalCmdArgs = LocalCmdArgs
295295
, _localTxCmdArgs_shortOutput :: Bool
296296
} deriving (Eq,Ord,Show,Read)
297297

298+
data PollCmdArgs = PollCmdArgs
299+
{ _pollTxCmdArgs_txArgs :: NodeTxCmdArgs
300+
, _pollTxCmdArgs_shortOutput :: Bool
301+
} deriving (Eq,Ord,Show,Read)
302+
298303
nodeOptP :: Parser HostPort
299304
nodeOptP = option (eitherReader (hostPortFromText . T.pack)) $ mconcat
300305
[ long "node"
@@ -329,6 +334,9 @@ nodeTxCmdP = NodeTxCmdArgs <$> many txFileP <*> optional schemeHostPortOptP
329334
localCmdP :: Parser LocalCmdArgs
330335
localCmdP = LocalCmdArgs <$> nodeTxCmdP <*> noVerifySigsP <*> shortOutputP
331336

337+
pollCmdP :: Parser PollCmdArgs
338+
pollCmdP = PollCmdArgs <$> nodeTxCmdP <*> shortOutputP
339+
332340
noVerifySigsP :: Parser Bool
333341
noVerifySigsP = flag True False $ mconcat
334342
[ long "no-verify-sigs"
@@ -455,7 +463,7 @@ data SubCommand
455463
| ListKeys (Either FilePath ChainweaverFile) (Maybe KeyIndex)
456464
| Local LocalCmdArgs
457465
| Mempool SchemeHostPort ChainId (Maybe Text) (Maybe Text)
458-
| Poll NodeTxCmdArgs
466+
| Poll PollCmdArgs
459467
| Send NodeTxCmdArgs
460468
| Sign SignArgs
461469
| Verify VerifyArgs
@@ -576,7 +584,7 @@ nodeCommands :: Mod CommandFields SubCommand
576584
nodeCommands = mconcat
577585
[ command "local" (info (Local <$> localCmdP)
578586
(progDesc "Test commands locally with a node's /local endpoint"))
579-
, command "poll" (info (Poll <$> nodeTxCmdP)
587+
, command "poll" (info (Poll <$> pollCmdP)
580588
(progDesc "Poll command results with a node's /poll endpoint"))
581589
, command "send" (info (Send <$> nodeTxCmdP)
582590
(progDesc "Send commands to a node's /send endpoint"))

0 commit comments

Comments
 (0)