@@ -7,6 +7,7 @@ module BotPlutusInterface.Effects (
77 ShellArgs (.. ),
88 handlePABEffect ,
99 createDirectoryIfMissing ,
10+ createDirectoryIfMissingCLI ,
1011 queryChainIndex ,
1112 listDirectory ,
1213 threadDelay ,
@@ -37,6 +38,8 @@ import Data.Aeson (ToJSON)
3738import Data.Aeson qualified as JSON
3839import Data.Bifunctor (second )
3940import Data.Kind (Type )
41+ import Data.Maybe (catMaybes )
42+ import Data.String (IsString , fromString )
4043import Data.Text (Text )
4144import Data.Text qualified as Text
4245import Plutus.Contract.Effects (ChainIndexQuery , ChainIndexResponse )
@@ -58,6 +61,8 @@ instance Show (ShellArgs a) where
5861data PABEffect (w :: Type ) (r :: Type ) where
5962 CallCommand :: ShellArgs a -> PABEffect w (Either Text a )
6063 CreateDirectoryIfMissing :: Bool -> FilePath -> PABEffect w ()
64+ -- Same as above but creates folder on the CLI machine, be that local or remote.
65+ CreateDirectoryIfMissingCLI :: Bool -> FilePath -> PABEffect w ()
6166 PrintLog :: LogLevel -> String -> PABEffect w ()
6267 UpdateInstanceState :: Activity -> PABEffect w ()
6368 LogToContract :: (ToJSON w , Monoid w ) => w -> PABEffect w ()
@@ -93,6 +98,10 @@ handlePABEffect contractEnv =
9398 Remote ipAddr -> callRemoteCommand ipAddr shellArgs
9499 CreateDirectoryIfMissing createParents filePath ->
95100 Directory. createDirectoryIfMissing createParents filePath
101+ CreateDirectoryIfMissingCLI createParents filePath ->
102+ case contractEnv. cePABConfig. pcCliLocation of
103+ Local -> Directory. createDirectoryIfMissing createParents filePath
104+ Remote ipAddr -> createDirectoryIfMissingRemote ipAddr createParents filePath
96105 PrintLog logLevel txt -> printLog' contractEnv. cePABConfig. pcLogLevel logLevel txt
97106 UpdateInstanceState s -> do
98107 atomically $
@@ -132,8 +141,15 @@ callRemoteCommand ipAddr ShellArgs {cmdName, cmdArgs, cmdOutParser} =
132141 " ssh"
133142 (map Text. unpack [ipAddr, Text. unwords $ " source ~/.bash_profile;" : cmdName : map quotes cmdArgs])
134143
135- quotes :: Text -> Text
136- quotes str = " \" " <> str <> " \" "
144+ createDirectoryIfMissingRemote :: Text -> Bool -> FilePath -> IO ()
145+ createDirectoryIfMissingRemote ipAddr createParents path =
146+ void $ readProcessEither " ssh" $ catMaybes [Just $ Text. unpack ipAddr, Just " mkdir" , pFlag, Just $ quotes path]
147+ where
148+ pFlag :: Maybe String
149+ pFlag = if createParents then Just " -p" else Nothing
150+
151+ quotes :: forall (a :: Type ). (IsString a , Semigroup a ) => a -> a
152+ quotes str = fromString " \" " <> str <> fromString " \" "
137153
138154readProcessEither :: FilePath -> [String ] -> IO (Either Text String )
139155readProcessEither path args =
@@ -162,6 +178,14 @@ createDirectoryIfMissing ::
162178 Eff effs ()
163179createDirectoryIfMissing createParents path = send @ (PABEffect w ) $ CreateDirectoryIfMissing createParents path
164180
181+ createDirectoryIfMissingCLI ::
182+ forall (w :: Type ) (effs :: [Type -> Type ]).
183+ Member (PABEffect w ) effs =>
184+ Bool ->
185+ FilePath ->
186+ Eff effs ()
187+ createDirectoryIfMissingCLI createParents path = send @ (PABEffect w ) $ CreateDirectoryIfMissingCLI createParents path
188+
165189printLog ::
166190 forall (w :: Type ) (effs :: [Type -> Type ]).
167191 Member (PABEffect w ) effs =>
0 commit comments