Skip to content

Commit 539d9e4

Browse files
committed
[cli] Add anti facts subcommands to select facts by type
1 parent 7557433 commit 539d9e4

File tree

5 files changed

+101
-7
lines changed

5 files changed

+101
-7
lines changed

cli/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
dist-newstyle
2+
.direnv
3+
tmp
4+
result

cli/anti.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ library
103103
Core.Types.Operation
104104
Core.Types.Tx
105105
Core.Types.Wallet
106+
Facts
106107
Lib.Box
107108
Lib.GitHub
108109
Lib.JSON.Canonical.Aeson

cli/src/Cli.hs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ import Core.Types.MPFS (MPFSClient (..))
1111
import Core.Types.Tx (TxHash, WithTxHash (..))
1212
import Core.Types.Wallet (Wallet)
1313
import Data.Functor.Identity (Identity (..))
14+
import Facts (FactsSelection, factsCmd)
1415
import Lib.GitHub (getOAUth)
1516
import Lib.JSON.Canonical.Extra
1617
import MPFS.API
1718
( MPFS (..)
18-
, getTokenFacts
1919
, mpfsClient
2020
, retractChange
2121
)
@@ -34,7 +34,7 @@ import Oracle.Validate.Types
3434
, runValidate
3535
)
3636
import Submitting (Submission (..))
37-
import Text.JSON.Canonical (FromJSON (..), JSValue, ToJSON (..))
37+
import Text.JSON.Canonical (FromJSON (..), ToJSON (..))
3838
import User.Agent.Cli
3939
( AgentCommand (..)
4040
, IsReady (NotReady)
@@ -53,7 +53,7 @@ data Command a where
5353
AgentCommand :: MPFSClient -> AgentCommand NotReady a -> Command a
5454
RetractRequest
5555
:: MPFSClient -> Wallet -> RequestRefId -> Command TxHash
56-
GetFacts :: MPFSClient -> TokenId -> Command JSValue
56+
GetFacts :: MPFSClient -> TokenId -> FactsSelection a -> Command a
5757
Wallet :: WalletCommand a -> Command a
5858
GetToken
5959
:: MPFSClient
@@ -106,8 +106,8 @@ cmd = \case
106106
$ submit
107107
$ \address ->
108108
retractChange address refId
109-
GetFacts MPFSClient{runMPFS} tokenId -> do
110-
runMPFS $ getTokenFacts tokenId
109+
GetFacts MPFSClient{runMPFS} tokenId factsCommand ->
110+
runMPFS $ factsCmd mpfsClient tokenId factsCommand
111111
Wallet walletCommand -> liftIO $ walletCmd walletCommand
112112
GetToken
113113
MPFSClient{runMPFS, submitTx}

cli/src/Facts.hs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
module Facts
2+
( FactsSelection (..)
3+
, TestRunSelection (..)
4+
, factsCmd
5+
)
6+
where
7+
8+
import Core.Types.Basic (TokenId)
9+
import Core.Types.Fact (Fact (..), parseFacts)
10+
import MPFS.API (MPFS, mpfsGetTokenFacts)
11+
import Text.JSON.Canonical
12+
import User.Types
13+
( Phase (..)
14+
, RegisterUserKey
15+
, TestRun
16+
, TestRunState (..)
17+
)
18+
19+
data TestRunSelection a where
20+
TestRunPending
21+
:: TestRunSelection [Fact TestRun (TestRunState 'PendingT)]
22+
TestRunRunning
23+
:: TestRunSelection [Fact TestRun (TestRunState 'RunningT)]
24+
TestRunDone :: TestRunSelection [Fact TestRun (TestRunState 'DoneT)]
25+
TestRunRejected
26+
:: TestRunSelection [Fact TestRun (TestRunState 'DoneT)]
27+
data FactsSelection a where
28+
UserFacts :: FactsSelection [Fact RegisterUserKey ()]
29+
RoleFacts :: FactsSelection [Fact RegisterUserKey ()]
30+
TestRunFacts :: TestRunSelection a -> FactsSelection a
31+
AllFacts :: FactsSelection [Fact JSValue JSValue]
32+
retrieveAnyFacts
33+
:: (FromJSON Maybe k, FromJSON Maybe v, Functor m)
34+
=> MPFS m
35+
-> TokenId
36+
-> m [Fact k v]
37+
retrieveAnyFacts mpfs tokenId = parseFacts <$> mpfsGetTokenFacts mpfs tokenId
38+
39+
factsCmd :: Monad m => MPFS m -> TokenId -> FactsSelection a -> m a
40+
factsCmd mpfs tokenId UserFacts = retrieveAnyFacts mpfs tokenId
41+
factsCmd mpfs tokenId RoleFacts = retrieveAnyFacts mpfs tokenId
42+
factsCmd mpfs tokenId (TestRunFacts TestRunPending) = retrieveAnyFacts mpfs tokenId
43+
factsCmd mpfs tokenId (TestRunFacts TestRunRunning) = retrieveAnyFacts mpfs tokenId
44+
factsCmd mpfs tokenId (TestRunFacts TestRunDone) = do
45+
facts <- retrieveAnyFacts mpfs tokenId
46+
pure
47+
$ filter
48+
( \v -> case factValue v of
49+
Finished{} -> True
50+
_ -> False
51+
)
52+
facts
53+
factsCmd mpfs tokenId (TestRunFacts TestRunRejected) = do
54+
facts <- retrieveAnyFacts mpfs tokenId
55+
pure
56+
$ filter
57+
( \v -> case factValue v of
58+
Rejected{} -> True
59+
_ -> False
60+
)
61+
facts
62+
factsCmd mpfs tokenId AllFacts = retrieveAnyFacts mpfs tokenId

cli/src/Options.hs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,20 @@ import Core.Options
2020
)
2121
import Core.Types.MPFS (mpfsClientOption)
2222
import Data.Version (Version)
23+
import Facts (FactsSelection (..), TestRunSelection (..))
2324
import Lib.Box (Box (..), fmapBox)
2425
import MPFS.API (mpfsClient)
2526
import OptEnvConf
2627
( Parser
2728
, command
2829
, commands
29-
, runParser
30+
, runParser, (<|>)
3031
)
3132
import Oracle.Options (oracleCommandParser)
3233
import User.Agent.Options (agentCommandParser)
3334
import User.Requester.Options (requesterCommandParser)
3435
import Wallet.Options (walletCommandParser)
36+
import Control.Applicative (optional)
3537

3638
newtype Options a = Options
3739
{ optionsCommand :: Command a
@@ -59,11 +61,37 @@ commandParser =
5961
"Retract a request"
6062
retractRequestOptions
6163
, command "facts" "Get token facts"
62-
$ fmap Box . GetFacts <$> mpfsClientOption <*> tokenIdOption
64+
$ (\c tk -> fmapBox (GetFacts c tk))
65+
<$> mpfsClientOption
66+
<*> tokenIdOption
67+
<*> factsSelectionParser
6368
, command "token" "Get the token content"
6469
$ fmap Box . GetToken <$> mpfsClientOption <*> tokenIdOption
6570
]
6671

72+
factsSelectionParser :: Parser (Box FactsSelection)
73+
factsSelectionParser =
74+
commands
75+
[ command "user" "Get registered users" (pure $ Box UserFacts)
76+
, command "role" "Get registered roles" (pure $ Box RoleFacts)
77+
, command
78+
"test-run"
79+
"Get test runs"
80+
(fmapBox TestRunFacts <$> testRunSelectionParser)
81+
] <|> pure (Box AllFacts)
82+
83+
testRunSelectionParser :: Parser (Box TestRunSelection)
84+
testRunSelectionParser =
85+
commands
86+
[ command "pending" "Get pending test runs" (pure $ Box TestRunPending)
87+
, command "running" "Get running test runs" (pure $ Box TestRunRunning)
88+
, command "done" "Get done test runs" (pure $ Box TestRunDone)
89+
, command
90+
"rejected"
91+
"Get rejected test runs"
92+
(pure $ Box TestRunRejected)
93+
]
94+
6795
optionsParser :: Parser (Box Options)
6896
optionsParser = fmapBox Options <$> commandParser
6997

0 commit comments

Comments
 (0)