Skip to content

A peer-to-peer stack for code collaboration

daemon: Extract CLI code

Thomas Scholtes9641d4e172d8
diff --git a/exe/Daemon.hs b/exe/Daemon.hs
index b548dc5..a4f0a31 100644
--- a/exe/Daemon.hs
+++ b/exe/Daemon.hs
@@ -23,11 +23,11 @@ import qualified Data.Map.Strict as Map
import qualified Data.Text as T
import qualified Data.Time.Clock.System as Time
import Network.Wai.Handler.Warp (run)
-import Options.Applicative
import Servant
import System.IO (BufferMode(..), hSetBuffering)
import UnliftIO.Async
+import qualified Radicle.Daemon.Cli as Cli
import Radicle.Daemon.Error
import qualified Radicle.Daemon.HttpApi as Api
import Radicle.Daemon.Ipfs
@@ -39,53 +39,9 @@ import Radicle.Daemon.Monad
import qualified Radicle.Ipfs as Ipfs
import Radicle hiding (DaemonError, Env)
-import qualified Radicle.Internal.CLI as Local
import qualified Radicle.Internal.ConcurrentMap as CMap
import qualified Radicle.Internal.UUID as UUID
--- * Main
-
-data Opts = Opts
- { port :: Int
- , machineConfigFile :: FilePath
- , debug :: Bool
- }
-
-cliInfo :: IO (ParserInfo Opts)
-cliInfo = do
- defaultMachineConfigFile <- Local.getRadicleFile "machines.json"
- pure $
- info (opts defaultMachineConfigFile <**> helper)
- ( fullDesc
- <> progDesc "Run the radicle daemon"
- <> header "rad-daemon-radicle"
- )
-
-opts :: FilePath -> Parser Opts
-opts defaultMachineConfigFile = do
- port <-
- option auto
- ( long "port"
- <> help "daemon port"
- <> metavar "PORT"
- <> showDefault
- <> value 8909
- )
- machineConfigFile <-
- strOption
- ( long "machine-config"
- <> help "Where to store configuration for known machines"
- <> metavar "FILE"
- <> showDefault
- <> value defaultMachineConfigFile
- )
- debug <-
- switch
- ( long "debug"
- <> help "enable debug logging"
- <> showDefault
- )
- pure $ Opts{..}
-- | Repeatedly tries to connect to the Radicle IPFS daemon API until
-- succesfull. Times out after five seconds and throws 'IpfsDaemonNotReachable'.
@@ -123,7 +79,7 @@ waitForIpfsDaemon = do
main :: IO Void
main = do
hSetBuffering stdout LineBuffering
- Opts{..} <- execParser =<< cliInfo
+ Cli.Opts{..} <- Cli.parse
machineConfigFileLock <- newMVar ()
machines <- CachedMachines <$> CMap.empty
let env = Env{ logLevel = if debug then LogDebug else LogInfo, ..}
diff --git a/package.yaml b/package.yaml
index efdda42..01a2ae9 100644
--- a/package.yaml
+++ b/package.yaml
@@ -53,6 +53,7 @@ library:
- microlens
- mtl
- multibase
+ - optparse-applicative
- pointed
- prettyprinter
- prettyprinter-ansi-terminal
@@ -158,7 +159,6 @@ executables:
dependencies:
- containers
- mtl
- - optparse-applicative
- radicle
- safe-exceptions
- servant-server
diff --git a/src/Radicle/Daemon/Cli.hs b/src/Radicle/Daemon/Cli.hs
new file mode 100644
index 0000000..fc4707a
--- /dev/null
+++ b/src/Radicle/Daemon/Cli.hs
@@ -0,0 +1,55 @@
+module Radicle.Daemon.Cli
+ ( parse
+ , Opts(..)
+ ) where
+
+import Protolude hiding (option)
+
+import Options.Applicative
+
+import qualified Radicle.Internal.CLI as Local
+
+data Opts = Opts
+ { port :: Int
+ , machineConfigFile :: FilePath
+ , debug :: Bool
+ }
+
+parse :: IO Opts
+parse = execParser =<< parserInfo
+
+parserInfo :: IO (ParserInfo Opts)
+parserInfo = do
+ defaultMachineConfigFile <- Local.getRadicleFile "machines.json"
+ pure $
+ info (opts defaultMachineConfigFile <**> helper)
+ ( fullDesc
+ <> progDesc "Run the radicle daemon"
+ <> header "rad-daemon-radicle"
+ )
+
+opts :: FilePath -> Parser Opts
+opts defaultMachineConfigFile = do
+ port <-
+ option auto
+ ( long "port"
+ <> help "daemon port"
+ <> metavar "PORT"
+ <> showDefault
+ <> value 8909
+ )
+ machineConfigFile <-
+ strOption
+ ( long "machine-config"
+ <> help "Where to store configuration for known machines"
+ <> metavar "FILE"
+ <> showDefault
+ <> value defaultMachineConfigFile
+ )
+ debug <-
+ switch
+ ( long "debug"
+ <> help "enable debug logging"
+ <> showDefault
+ )
+ pure Opts{..}