terminal: option to execute a single chat command via command line (#515)

This commit is contained in:
Evgeny Poberezkin
2022-04-10 16:30:54 +01:00
committed by GitHub
parent fd69b673d8
commit 0ac9785e4b
3 changed files with 56 additions and 17 deletions
+20 -9
View File
@@ -2,24 +2,35 @@
module Main where
import Control.Concurrent (threadDelay)
import Simplex.Chat
import Simplex.Chat.Controller (versionNumber)
import Simplex.Chat.Bot
import Simplex.Chat.Controller (ChatConfig, versionNumber)
import Simplex.Chat.Options
import Simplex.Chat.Terminal
import Simplex.Chat.View (serializeChatResponse)
import System.Directory (getAppUserDataDirectory)
import System.Terminal (withTerminal)
cfg :: ChatConfig
cfg = defaultChatConfig
main :: IO ()
main = do
opts <- welcomeGetOpts
t <- withTerminal pure
simplexChat defaultChatConfig opts t
welcomeGetOpts :: IO ChatOpts
welcomeGetOpts = do
appDir <- getAppUserDataDirectory "simplex"
opts@ChatOpts {dbFilePrefix} <- getChatOpts appDir "simplex_v1"
opts@ChatOpts {chatCmd} <- getChatOpts appDir "simplex_v1"
if null chatCmd
then do
welcome opts
t <- withTerminal pure
simplexChat cfg opts t
else simplexChatBot cfg opts $ \_ cc -> do
r <- sendCmd cc chatCmd
putStrLn $ serializeChatResponse r
threadDelay $ chatCmdDelay opts * 1000000
welcome :: ChatOpts -> IO ()
welcome ChatOpts {dbFilePrefix} = do
putStrLn $ "SimpleX Chat v" ++ versionNumber
putStrLn $ "db: " <> dbFilePrefix <> "_chat.db, " <> dbFilePrefix <> "_agent.db"
putStrLn "type \"/help\" or \"/h\" for usage info"
pure opts
+3 -1
View File
@@ -51,7 +51,9 @@ mobileChatOpts =
{ dbFilePrefix = "simplex_v1", -- two database files will be created: simplex_v1_chat.db and simplex_v1_agent.db
smpServers = [],
logConnections = False,
logAgent = False
logAgent = False,
chatCmd = "",
chatCmdDelay = 3
}
defaultMobileConfig :: ChatConfig
+33 -7
View File
@@ -1,3 +1,5 @@
{-# LANGUAGE ApplicativeDo #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
module Simplex.Chat.Options
@@ -20,13 +22,15 @@ data ChatOpts = ChatOpts
{ dbFilePrefix :: String,
smpServers :: [SMPServer],
logConnections :: Bool,
logAgent :: Bool
logAgent :: Bool,
chatCmd :: String,
chatCmdDelay :: Int
}
chatOpts :: FilePath -> FilePath -> Parser ChatOpts
chatOpts appDir defaultDbFileName =
ChatOpts
<$> strOption
chatOpts appDir defaultDbFileName = do
dbFilePrefix <-
strOption
( long "database"
<> short 'd'
<> metavar "DB_FILE"
@@ -34,7 +38,8 @@ chatOpts appDir defaultDbFileName =
<> value defaultDbFilePath
<> showDefault
)
<*> option
smpServers <-
option
parseSMPServers
( long "server"
<> short 's'
@@ -43,16 +48,37 @@ chatOpts appDir defaultDbFileName =
"Comma separated list of SMP server(s) to use"
<> value []
)
<*> switch
logConnections <-
switch
( long "connections"
<> short 'c'
<> help "Log every contact and group connection on start"
)
<*> switch
logAgent <-
switch
( long "log-agent"
<> short 'l'
<> help "Enable logs from SMP agent"
)
chatCmd <-
strOption
( long "execute"
<> short 'e'
<> metavar "COMMAND"
<> help "Execute chat command (received messages won't be logged) and exit"
<> value ""
)
chatCmdDelay <-
option
auto
( long "time"
<> short 't'
<> metavar "TIME"
<> help "Time to wait after sending chat command before exiting, seconds"
<> value 3
<> showDefault
)
pure ChatOpts {dbFilePrefix, smpServers, logConnections, logAgent, chatCmd, chatCmdDelay}
where
defaultDbFilePath = combine appDir defaultDbFileName