Skip to content

Commit 36e2c83

Browse files
committed
[cli] Simplify agent cli module
1 parent 09cccbf commit 36e2c83

File tree

2 files changed

+39
-51
lines changed

2 files changed

+39
-51
lines changed

cli/src/Oracle/Validate/Requests/TestRun/Update.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ data UpdateTestRunFailure
4141
| UpdateTestRunAgentRejection AgentRejection
4242
| UpdateTestRunRequestNotFromAgent Owner
4343
| UpdateTestRunConfigNotAvailable
44+
| UpdateTestRunWrongPreviousState
4445
deriving (Show, Eq)
4546

4647
instance Monad m => ToJSON m UpdateTestRunFailure where
@@ -53,6 +54,8 @@ instance Monad m => ToJSON m UpdateTestRunFailure where
5354
object ["updateTestRunRequestNotFromAgent" .= show owner]
5455
UpdateTestRunConfigNotAvailable ->
5556
toJSON ("Token configuration is not available yet" :: String)
57+
UpdateTestRunWrongPreviousState ->
58+
toJSON ("Wrong previous state for test run" :: String)
5659

5760
checkingOwner
5861
:: Monad m

cli/src/User/Agent/Cli.hs

Lines changed: 36 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ module User.Agent.Cli
88
, TestRunId (..)
99
, IsReady (..)
1010
, agentCmd
11-
, agentCmdCore
1211
)
1312
where
1413

@@ -60,7 +59,7 @@ import Oracle.Validate.Requests.TestRun.Update
6059
, validateToRunningUpdate
6160
)
6261
import Oracle.Validate.Types
63-
( AValidationResult
62+
( AValidationResult (..)
6463
, Validate
6564
, Validated
6665
, liftMaybe
@@ -87,16 +86,6 @@ import User.Types
8786
)
8887
import Validation (Validation)
8988

90-
agentCmd
91-
:: MonadIO m
92-
=> AgentCommand NotReady a
93-
-> WithContext m a
94-
agentCmd cmdNotReady = do
95-
mCmdReady <- resolveOldState cmdNotReady
96-
case mCmdReady of
97-
Nothing -> error "No previous state found for the command"
98-
Just cmdReady -> agentCmdCore cmdReady
99-
10089
withExpectedState
10190
:: (FromJSON Maybe (TestRunState phase), Monad m)
10291
=> TokenId
@@ -139,30 +128,42 @@ withResolvedTestRun tk (TestRunId testRunId) cont = do
139128
Nothing -> pure Nothing
140129
Just testRun -> cont testRun
141130

142-
resolveOldState
143-
:: Monad m
144-
=> AgentCommand NotReady result
145-
-> WithContext m (Maybe (AgentCommand Ready result))
146-
resolveOldState cmd = case cmd of
147-
Accept tokenId wallet key () ->
148-
withResolvedTestRun tokenId key $ \testRun ->
149-
withExpectedState tokenId testRun
150-
$ pure . Accept tokenId wallet testRun
151-
Reject tokenId wallet key () reason ->
152-
withResolvedTestRun tokenId key $ \testRun ->
153-
withExpectedState tokenId testRun
154-
$ \pending -> pure $ Reject tokenId wallet testRun pending reason
155-
Report tokenId wallet key () duration url ->
156-
withResolvedTestRun tokenId key $ \testRun ->
157-
withExpectedState tokenId testRun $ \runningState ->
158-
pure $ Report tokenId wallet testRun runningState duration url
159-
Query tokenId -> pure $ Just $ Query tokenId
160-
WhiteList tokenId m platform repo ->
161-
pure $ Just $ WhiteList tokenId m platform repo
131+
updateTestRunState
132+
:: (Monad m, FromJSON Maybe (TestRunState phase))
133+
=> TokenId
134+
-> TestRunId
135+
-> ( TestRun
136+
-> TestRunState phase
137+
-> WithContext m (AValidationResult UpdateTestRunFailure a)
138+
)
139+
-> WithContext m (AValidationResult UpdateTestRunFailure a)
140+
updateTestRunState tokenId key f = do
141+
mResult <- withResolvedTestRun tokenId key $ \testRun ->
142+
withExpectedState tokenId testRun $ f testRun
143+
pure $ case mResult of
144+
Nothing -> ValidationFailure UpdateTestRunWrongPreviousState
145+
Just result -> result
146+
147+
agentCmd
148+
:: MonadIO m
149+
=> AgentCommand NotReady a
150+
-> WithContext m a
151+
agentCmd = \case
152+
Query tokenId -> queryCommand tokenId
153+
WhiteList tokenId wallet platform repo ->
154+
whiteList tokenId wallet platform repo
162155
BlackList tokenId wallet platform repo ->
163-
pure $ Just $ BlackList tokenId wallet platform repo
164-
DownloadAssets tokenId key dir ->
165-
pure $ Just $ DownloadAssets tokenId key dir
156+
blackList tokenId wallet platform repo
157+
DownloadAssets tokenId key dir -> downloadAssets tokenId key dir
158+
Accept tokenId wallet key () -> do
159+
updateTestRunState tokenId key $ \testRun testRunState ->
160+
acceptCommand tokenId wallet testRun testRunState
161+
Reject tokenId wallet key () reason -> do
162+
updateTestRunState tokenId key $ \testRun testRunState ->
163+
rejectCommand tokenId wallet testRun testRunState reason
164+
Report tokenId wallet key () duration url -> do
165+
updateTestRunState tokenId key $ \testRun testRunState ->
166+
reportCommand tokenId wallet testRun testRunState duration url
166167

167168
data IsReady = NotReady | Ready
168169
deriving (Show, Eq)
@@ -245,22 +246,6 @@ deriving instance Eq (AgentCommand NotReady result)
245246
deriving instance Show (AgentCommand Ready result)
246247
deriving instance Eq (AgentCommand Ready result)
247248

248-
agentCmdCore
249-
:: MonadIO m
250-
=> AgentCommand Ready result
251-
-> WithContext m result
252-
agentCmdCore cmd = case cmd of
253-
Accept tokenId wallet key pending ->
254-
acceptCommand tokenId wallet key pending
255-
Reject tokenId wallet key pending reason ->
256-
rejectCommand tokenId wallet key pending reason
257-
Report tokenId wallet key running duration url ->
258-
reportCommand tokenId wallet key running duration url
259-
Query tokenId -> queryCommand tokenId
260-
WhiteList tokenId wallet platform repo -> whiteList tokenId wallet platform repo
261-
BlackList tokenId wallet platform repo -> blackList tokenId wallet platform repo
262-
DownloadAssets tokenId key dir -> downloadAssets tokenId key dir
263-
264249
whiteList
265250
:: Monad m
266251
=> TokenId

0 commit comments

Comments
 (0)