Commit cae7ca ecp

3 files Authored and Committed by Richard Marko 2 years ago
ecp

    
 1 @@ -6,6 +6,7 @@
 2    - esh - ESP remote shell
 3    - eshi - ESP remote interpret
 4    - eshc - Esp Shell Hardcore Cluster edition
 5 +  - ecp - SCP-like file upload
 6   
 7   Usage::
 8   
 9 @@ -14,7 +15,7 @@
10           stack build
11           stack install
12   
13 - ASCIInema:
14 + asciinema:
15   
16    - esh  https://asciinema.org/a/108450
17    - eshc https://asciinema.org/a/108677
 1 @@ -0,0 +1,75 @@
 2 + {-# LANGUAGE RecordWildCards #-}
 3 + {-# LANGUAGE OverloadedStrings #-}
 4 + import System.IO
 5 + import Control.Monad
 6 + import Control.Monad.IO.Class
 7 + import Control.Concurrent
 8 + import Control.Concurrent.Async
 9 + import Control.Concurrent.STM
10 + 
11 + import Network.ESP.Utils
12 + import Network.ESP.Client
13 + import Network.ESP.Types
14 + 
15 + import qualified Data.ByteString.Char8 as B
16 + 
17 + import Options.Applicative hiding ((<>), completer, help)
18 + import Data.Semigroup ((<>))
19 + import Data.List
20 + 
21 + data ESPCfg = ESPCfg {
22 +     eHost :: B.ByteString
23 +   , eFile :: String
24 +   , ePort :: Int
25 +   } deriving (Show)
26 + 
27 + parseCfg :: Parser ESPCfg
28 + parseCfg = ESPCfg
29 +   <$> (B.pack <$> argument str (metavar "HOST"))
30 +   <*> (argument str (metavar "FILE"))
31 +   <*> option auto
32 +         (long "port"
33 +       <> short 'p'
34 +       <> value 23)
35 + 
36 + main = do
37 +   cfg@ESPCfg{..} <- execParser opts
38 + 
39 +   esp@ESP{..} <- newESP eHost ePort
40 + 
41 +   --async $ forever $ (atomically $ readTChan $ espOutChan) >>= dump
42 +   let waitOk e@ESP{..} = do
43 +         val <- atomically $ readTChan $ espOutChan
44 +         case val of
45 +           Nothing -> return False
46 +           (Just "!ok_upload") -> return True
47 +           (Just x) -> B.putStr x >> waitOk e
48 + 
49 +   ea <- async $ runESP esp
50 +   -- also needs waitConnected
51 +   withFile eFile ReadMode $ \x -> do
52 +     result <- uploadESP esp x "test.lua"
53 +     return result
54 +   okRes <- waitOk esp
55 +   case okRes of
56 +     False -> B.putStr "Upload failed, device closed connection"
57 +     True -> B.putStr "Done"
58 +   --wait ea
59 +   where
60 +     opts = info (parseCfg <**> helper)
61 +       ( fullDesc
62 +      <> progDesc "Ecp"
63 +      <> header "ESP cp - copy file to remote device" )
64 + 
65 +     -- XXX: needs bootstrap
66 +     --wrap x = B.concat [ "safecall(\"", escape x, "\")" ]
67 +     wrap = id
68 + 
69 + dump (Just msg) = B.putStr msg
70 + dump _ = return ()
71 + 
72 + readUser h = do
73 +   eof <- hIsEOF h
74 +   case eof of
75 +     True -> return Nothing
76 +     False -> B.hGetLine h >>= return . Just
 1 @@ -72,6 +72,20 @@
 2                        , esphs
 3     default-language:    Haskell2010
 4   
 5 + executable ecp
 6 +   hs-source-dirs:      app
 7 +   main-is:             Ecp.hs
 8 +   ghc-options:         -threaded -rtsopts -with-rtsopts=-N
 9 +   build-depends:       base
10 +                      , bytestring
11 +                      , async
12 +                      , stm
13 +                      , network
14 +                      , random
15 +                      , optparse-applicative
16 +                      , esphs
17 +   default-language:    Haskell2010
18 + 
19   executable eshc
20     hs-source-dirs:      app
21     main-is:             EshiCluster.hs