packages feed

handsy 0.0.2 → 0.0.3

raw patch · 3 files changed

+42/−28 lines, 3 filesdep ~basePVP: major bump suggested

API removals or changes: PVP suggests a major version bump

Dependency ranges changed: base

API changes (from Hackage documentation)

+ System.Handsy: AppendFile :: FilePath -> ByteString -> (() -> k) -> HandsyF k
+ System.Handsy: appendFile :: MonadFree HandsyF m_a7wC => FilePath -> ByteString -> m_a7wC ()
+ System.Handsy: shell :: String -> Handsy (ExitCode, ByteString, ByteString)
- System.Handsy: Command :: String -> [String] -> ByteString -> ((ExitCode, ByteString, ByteString) -> k) -> HandsyF k
+ System.Handsy: Command :: FilePath -> [String] -> ByteString -> ((ExitCode, ByteString, ByteString) -> k) -> HandsyF k
- System.Handsy: ReadFile :: String -> (ByteString -> k) -> HandsyF k
+ System.Handsy: ReadFile :: FilePath -> (ByteString -> k) -> HandsyF k
- System.Handsy: WriteFile :: String -> ByteString -> (() -> k) -> HandsyF k
+ System.Handsy: WriteFile :: FilePath -> ByteString -> (() -> k) -> HandsyF k
- System.Handsy: command :: MonadFree HandsyF m_a7tf => String -> [String] -> ByteString -> m_a7tf (ExitCode, ByteString, ByteString)
+ System.Handsy: command :: MonadFree HandsyF m_a7vR => FilePath -> [String] -> ByteString -> m_a7vR (ExitCode, ByteString, ByteString)
- System.Handsy: readFile :: MonadFree HandsyF m_a7tR => String -> m_a7tR ByteString
+ System.Handsy: readFile :: MonadFree HandsyF m_a7wt => FilePath -> m_a7wt ByteString
- System.Handsy: writeFile :: MonadFree HandsyF m_a7tV => String -> ByteString -> m_a7tV ()
+ System.Handsy: writeFile :: MonadFree HandsyF m_a7wx => FilePath -> ByteString -> m_a7wx ()
- System.Handsy.Remote: RemoteOptions :: (String, [String]) -> RemoteOptions
+ System.Handsy.Remote: RemoteOptions :: (FilePath, [String]) -> RemoteOptions
- System.Handsy.Remote: sshCommand :: RemoteOptions -> (String, [String])
+ System.Handsy.Remote: sshCommand :: RemoteOptions -> (FilePath, [String])

Files

handsy.cabal view
@@ -1,5 +1,5 @@ name:          handsy-version:       0.0.2+version:       0.0.3 synopsis:      A DSL to describe common shell operations and interpeters for running them locally and remotely. -- description:    Homepage:      https://github.com/utdemir/handsy@@ -18,7 +18,7 @@ library   exposed-modules:  System.Handsy                     System.Handsy.Remote-  build-depends:    base >=4.7 && <4.8+  build-depends:    base >=4.6 && <4.8                   , bytestring                   , process                   , transformers
src/System/Handsy.hs view
@@ -5,26 +5,34 @@  module System.Handsy where -import           Prelude                   hiding (readFile, writeFile)- import           Control.Monad.IO.Class-import qualified Data.ByteString.Char8     as B+import qualified Data.ByteString.Lazy           as B import           System.Exit  import           Control.Monad.Free.TH import           Control.Monad.Trans.Free-import           System.Process.ByteString+import           System.Process.ByteString.Lazy --- | Base functor for our dsl-data HandsyF k = Command      String [String] B.ByteString ((ExitCode, B.ByteString, B.ByteString) -> k)-               | ReadFile     String                       (B.ByteString -> k)-               | WriteFile    String B.ByteString          (() -> k)-             deriving (Functor)+-- * Types+data HandsyF k =+    Command      FilePath [String] B.ByteString ((ExitCode, B.ByteString, B.ByteString) -> k)+  | ReadFile     FilePath                       (B.ByteString -> k)+  | WriteFile    FilePath B.ByteString          (() -> k)+  | AppendFile   FilePath B.ByteString          (() -> k)+  deriving (Functor)  type Handsy = FreeT HandsyF IO +-- * TH generated actions makeFree ''HandsyF +-- * Helpers+shell :: String -> Handsy (ExitCode, B.ByteString, B.ByteString)+shell cmd = command "/usr/bin/env" ["sh", "-c", cmd] ""++-- * Interpreter++-- | Executes the actions locally run :: Handsy a -> IO a run h = do   x <- runFreeT h@@ -34,5 +42,8 @@       -> B.readFile fp >>= run . next     Free (WriteFile fp str next)       -> B.writeFile fp str >>= run . next+    Free (AppendFile fp str next)+      -> B.appendFile fp str >>= run . next     Free (Command prg args stdin next)       -> readProcessWithExitCode prg args stdin >>= run . next+
src/System/Handsy/Remote.hs view
@@ -1,40 +1,43 @@ {-# LANGUAGE OverloadedStrings #-}- module System.Handsy.Remote where -import           Prelude                  hiding (readFile, writeFile)--import           System.Handsy+import           System.Handsy            as H -import qualified Data.ByteString.Char8    as B+import qualified Data.ByteString.Lazy     as B  import           Control.Monad.IO.Class import           Control.Monad.Trans.Free  data RemoteOptions =   RemoteOptions {-    sshCommand :: (String, [String])+    sshCommand :: (FilePath, [String])   } +-- | Executes the actions at a remote host runRemote :: RemoteOptions -> Handsy a -> IO a runRemote opts h = do   x <- runFreeT h   case x of     Pure r -> return r-    Free (ReadFile fp next) -> do-      (_, stdin,  _) <- runSsh "cat" [fp] ""-      runRemote opts (next stdin)-    Free (WriteFile fp str next) -> do-      _ <- runSsh "dd" ["of=" ++ fp] str -- TODO: Escape necessary?-      runRemote opts (next ())-    Free (Command prg args stdin next) -> runSsh prg args stdin >>= run . next+    Free (ReadFile fp next)+      -> runSsh "cat" [fp] "" >>= \(_, stdin, _) -> runRemote opts (next stdin)+    Free (WriteFile fp str next)+      -> runSsh "dd" ["of=" ++ fp] str >> runRemote opts (next ())+    Free (AppendFile fp str next)+      -> runSsh "dd" ["of=" ++ fp, "conv=notrunc", "oflag=append"] str >> runRemote opts (next ())+    Free (Command prg args stdin next)+      -> runSsh prg args stdin >>= runRemote opts . next    where     (ssh, sshOpts) = sshCommand opts     runSsh prg args stdin = run (command ssh (sshOpts ++ prg : args) stdin) -pushFile :: FilePath -> FilePath -> Handsy ()-pushFile local remote = liftIO (B.readFile local) >>= writeFile remote+pushFile :: FilePath -- ^ Local path of source+         -> FilePath -- ^ Remote path of destination+         -> Handsy ()+pushFile local remote = liftIO (B.readFile local) >>= H.writeFile remote -pullFile :: FilePath -> FilePath -> Handsy ()-pullFile remote local = readFile remote >>= liftIO . B.writeFile local+pullFile :: FilePath -- ^ Remote path of source+         -> FilePath -- ^ Local path of destination+         -> Handsy ()+pullFile remote local = H.readFile remote >>= liftIO . B.writeFile local