Skip to content

Commit 7e09588

Browse files
committed
[cli] Add anti requester generate-assets command
1 parent 2299f0f commit 7e09588

File tree

3 files changed

+76
-2
lines changed

3 files changed

+76
-2
lines changed

cli/src/User/Requester/Cli.hs

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module User.Requester.Cli
55
, RequesterCommand (..)
66
) where
77

8-
import Control.Monad (void)
8+
import Control.Monad (forM, forM_, void)
99
import Control.Monad.Catch (MonadMask)
1010
import Control.Monad.IO.Class (MonadIO (..))
1111
import Control.Monad.Trans.Class (lift)
@@ -16,13 +16,22 @@ import Core.Context
1616
, askSubmit
1717
, askValidation
1818
)
19-
import Core.Types.Basic (Duration, TokenId)
19+
import Core.Types.Basic
20+
( Directory (..)
21+
, Duration
22+
, FileName (..)
23+
, Repository (..)
24+
, TokenId
25+
)
2026
import Core.Types.Change (Change (..), Key (..), deleteKey, insertKey)
2127
import Core.Types.Operation (Operation (..))
2228
import Core.Types.Tx (TxHash, WithTxHash (..))
2329
import Core.Types.Wallet (Wallet)
30+
import Data.Bifunctor (Bifunctor (..))
2431
import Data.ByteString.Lazy qualified as BL
2532
import Data.Functor (($>))
33+
import Data.Text.IO qualified as T
34+
import Lib.JSON.Canonical.Extra (object, (.=))
2635
import Lib.SSH.Private
2736
( KeyAPI (..)
2837
, SSHClient (..)
@@ -53,7 +62,10 @@ import Oracle.Validate.Requests.TestRun.Create
5362
import Oracle.Validate.Types
5463
( AValidationResult
5564
, liftMaybe
65+
, mapFailure
5666
, runValidate
67+
, sequenceValidate
68+
, throwLeft
5769
)
5870
import Submitting (Submission (..))
5971
import Text.JSON.Canonical (ToJSON (..), renderCanonicalJSON)
@@ -64,6 +76,22 @@ import User.Types
6476
, TestRun (..)
6577
, TestRunState (..)
6678
)
79+
import Validation (Validation (..))
80+
import Validation.DownloadFile (DownloadedFileFailure)
81+
82+
newtype GenerateAssetsFailure
83+
= GenerateAssetsFailure [(FileName, DownloadedFileFailure)]
84+
85+
instance Monad m => ToJSON m GenerateAssetsFailure where
86+
toJSON (GenerateAssetsFailure failures) =
87+
object
88+
[
89+
( "generateAssetsFailure"
90+
, mapM
91+
(\(FileName fn, err) -> object ["file" .= fn, "error" .= err])
92+
failures
93+
)
94+
]
6795

6896
data RequesterCommand a where
6997
RegisterUser
@@ -97,6 +125,13 @@ data RequesterCommand a where
97125
CreateTestRunFailure
98126
(WithTxHash (TestRunState PendingT))
99127
)
128+
GenerateAssets
129+
:: Directory
130+
-> RequesterCommand
131+
( AValidationResult
132+
GenerateAssetsFailure
133+
()
134+
)
100135

101136
deriving instance Show (RequesterCommand a)
102137
deriving instance Eq (RequesterCommand a)
@@ -122,6 +157,29 @@ requesterCmd command = do
122157
sshClient
123158
testRun
124159
duration
160+
GenerateAssets directory -> generateAssets directory
161+
162+
generateAssets
163+
:: MonadIO m
164+
=> Directory
165+
-> WithContext m (AValidationResult GenerateAssetsFailure ())
166+
generateAssets (Directory targetDirectory) = do
167+
Validation{githubGetFile} <- askValidation Nothing
168+
lift $ runValidate $ do
169+
downloads <- forM ["docker-compose.yaml", "README.md", "testnet.yaml"] $ \filename -> do
170+
fmap (bimap (FileName filename,) (filename,))
171+
$ lift
172+
$ githubGetFile
173+
(Repository "cardano-foundation" "antithesis")
174+
Nothing
175+
(FileName $ "compose/testnets/cardano_node_master/" <> filename)
176+
contents <-
177+
mapFailure GenerateAssetsFailure
178+
$ sequenceValidate
179+
$ fmap (throwLeft id) downloads
180+
forM_ contents $ \(filename, content) -> do
181+
let filePath = targetDirectory <> "/" <> filename
182+
liftIO $ T.writeFile filePath content
125183

126184
createCommand
127185
:: (MonadIO m, MonadMask m)

cli/src/User/Requester/Options.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module User.Requester.Options
88

99
import Core.Options
1010
( commitOption
11+
, downloadAssetsDirectoryOption
1112
, durationOption
1213
, platformOption
1314
, pubkeyhashOption
@@ -117,6 +118,8 @@ requesterCommandParser =
117118
$ Box <$> addRoleOptions
118119
, command "unregister-role" "Remove a user from a repository"
119120
$ Box <$> removeRoleOptions
121+
, command "generate-asssets" "Generate assets for a test run"
122+
$ Box . GenerateAssets <$> downloadAssetsDirectoryOption
120123
]
121124

122125
sshClientOption

cli/src/Validation/DownloadFile.hs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import Data.Text.Encoding qualified as T
1717
import Data.Yaml qualified as Yaml
1818
import GitHub (Auth)
1919
import Lib.GitHub (GetGithubFileFailure, githubGetFile)
20+
import Lib.JSON.Canonical.Extra (object, (.=))
21+
import Text.JSON.Canonical (ToJSON (..))
2022
import Text.MMark qualified as MMark
2123

2224
data DownloadedFileFailure
@@ -25,6 +27,17 @@ data DownloadedFileFailure
2527
| DownloadedFileNotSupported
2628
deriving (Eq, Show)
2729

30+
instance Monad m => ToJSON m DownloadedFileFailure where
31+
toJSON (GithubGetFileError failure) =
32+
object ["githubGetFileError" .= show failure]
33+
toJSON (DownloadedFileParseError failure) =
34+
object ["downloadedFileParseError" .= failure]
35+
toJSON DownloadedFileNotSupported =
36+
object
37+
[ "downloadedFileNotSupported"
38+
.= ("Only `md` and `yaml` files are supported" :: Text)
39+
]
40+
2841
renderDownloadedFileFailure :: DownloadedFileFailure -> String
2942
renderDownloadedFileFailure = \case
3043
GithubGetFileError failure ->

0 commit comments

Comments
 (0)