Skip to content

Commit e5ac91c

Browse files
committed
[cli] refactor: implement generate assets via github directory downloading
1 parent 9825d8a commit e5ac91c

File tree

2 files changed

+40
-39
lines changed

2 files changed

+40
-39
lines changed

cli/src/Lib/GitHub.hs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,30 @@ data GetGithubFileFailure
223223
| GithubPathParsingError String
224224
deriving (Eq, Show)
225225

226+
instance Monad m => ToJSON m GetGithubFileFailure where
227+
toJSON GetGithubFileDirectoryNotFound =
228+
object ["error" .= ("directory not found" :: String)]
229+
toJSON GetGithubFileNotAFile =
230+
object ["error" .= ("not a file" :: String)]
231+
toJSON (GetGithubFileUnsupportedEncoding enc) =
232+
object
233+
[ "error"
234+
.= ("unsupported encoding: " ++ enc)
235+
]
236+
toJSON (GetGithubFileOtherFailure filename err) =
237+
object
238+
[ "error"
239+
.= ( "error fetching file "
240+
++ filename
241+
++ ": "
242+
++ err
243+
)
244+
]
245+
toJSON (GetGithubFileCodeError err) =
246+
object ["error" .= ("GitHub response code error: " ++ show err)]
247+
toJSON (GithubPathParsingError err) =
248+
object ["error" .= ("path parsing error: " ++ err)]
249+
226250
instance Exception GetGithubFileFailure
227251

228252
githubGetFile

cli/src/User/Requester/Cli.hs

Lines changed: 16 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module User.Requester.Cli
77
, NewTestRunCreated (..)
88
) where
99

10-
import Control.Monad (forM, forM_, void)
10+
import Control.Monad (void)
1111
import Control.Monad.Trans.Class (lift)
1212
import Core.Context
1313
( WithContext
@@ -19,7 +19,6 @@ import Core.Context
1919
import Core.Types.Basic
2020
( Directory (..)
2121
, Duration
22-
, FileName (..)
2322
, Repository (..)
2423
, Success (..)
2524
, TokenId
@@ -30,9 +29,9 @@ import Core.Types.Operation (Operation (..))
3029
import Core.Types.Tx (TxHash, WithTxHash (..))
3130
import Core.Types.Wallet (Wallet)
3231
import Crypto.PubKey.Ed25519 (Signature)
33-
import Data.Bifunctor (Bifunctor (..))
3432
import Data.ByteString.Lazy qualified as BL
3533
import Data.Functor (($>))
34+
import Lib.GitHub (GetGithubFileFailure)
3635
import Lib.JSON.Canonical.Extra (object, (.=))
3736
import Lib.SSH.Private
3837
( KeyPair (..)
@@ -66,9 +65,7 @@ import Oracle.Validate.Types
6665
( AValidationResult
6766
, ForRole (..)
6867
, liftMaybe
69-
, mapFailure
7068
, runValidate
71-
, sequenceValidate
7269
, throwLeft
7370
)
7471
import Submitting (Submission (..))
@@ -82,21 +79,6 @@ import User.Types
8279
, TestRunState (..)
8380
)
8481
import Validation (Validation (..), hoistValidation)
85-
import Validation.DownloadFile (DownloadedFileFailure)
86-
87-
newtype GenerateAssetsFailure
88-
= GenerateAssetsFailure [(FileName, DownloadedFileFailure)]
89-
90-
instance Monad m => ToJSON m GenerateAssetsFailure where
91-
toJSON (GenerateAssetsFailure failures) =
92-
object
93-
[
94-
( "generateAssetsFailure"
95-
, mapM
96-
(\(FileName fn, err) -> object ["file" .= fn, "error" .= err])
97-
failures
98-
)
99-
]
10082

10183
data RequesterCommand a where
10284
RegisterUser
@@ -134,7 +116,7 @@ data RequesterCommand a where
134116
:: Directory
135117
-> RequesterCommand
136118
( AValidationResult
137-
GenerateAssetsFailure
119+
GetGithubFileFailure
138120
Success
139121
)
140122

@@ -167,25 +149,20 @@ requesterCmd command = do
167149
generateAssets
168150
:: Monad m
169151
=> Directory
170-
-> WithContext m (AValidationResult GenerateAssetsFailure Success)
152+
-> WithContext m (AValidationResult GetGithubFileFailure Success)
171153
generateAssets (Directory targetDirectory) = do
172-
Validation{githubGetFile, writeTextFile} <- askValidation Nothing
173-
lift $ runValidate $ do
174-
downloads <- forM ["docker-compose.yaml", "README.md", "testnet.yaml"] $ \filename -> do
175-
fmap (bimap (FileName filename,) (filename,))
176-
$ lift
177-
$ githubGetFile
178-
(Repository "cardano-foundation" "antithesis")
179-
Nothing
180-
(FileName $ "compose/testnets/cardano_node_master/" <> filename)
181-
contents <-
182-
mapFailure GenerateAssetsFailure
183-
$ sequenceValidate
184-
$ fmap (throwLeft id) downloads
185-
forM_ contents $ \(filename, content) -> do
186-
let filePath = targetDirectory <> "/" <> filename
187-
lift $ writeTextFile filePath content
188-
pure Success
154+
Validation{githubDownloadDirectory} <- askValidation Nothing
155+
lift
156+
$ runValidate
157+
$ do
158+
r <-
159+
lift
160+
$ githubDownloadDirectory
161+
(Repository "cardano-foundation" "antithesis")
162+
Nothing
163+
(Directory "compose/testnet/cardano_node_master")
164+
(Directory targetDirectory)
165+
throwLeft id r $> Success
189166

190167
signKey
191168
:: (ToJSON m key, Monad m) => KeyPair -> key -> m (JSValue, Signature)

0 commit comments

Comments
 (0)