Skip to content

A peer-to-peer stack for code collaboration

Implement basic rad top-level command (#398)

Alexis Sellier7731e8d5b7e2
diff --git a/.stylish-haskell.yaml b/.stylish-haskell.yaml
index d9f6879..f157e71 100644
--- a/.stylish-haskell.yaml
+++ b/.stylish-haskell.yaml
@@ -203,3 +203,4 @@ language_extensions:
- TupleSections
- TypeFamilies
- TypeOperators
+ - TypeApplications
diff --git a/exe/Rad.hs b/exe/Rad.hs
new file mode 100644
index 0000000..012cb89
--- /dev/null
+++ b/exe/Rad.hs
@@ -0,0 +1,67 @@
+{-# LANGUAGE TypeApplications #-}
+-- | Top-level radicle CLI. Calls sub-commands.
+module Rad
+ ( main
+ ) where
+
+import Protolude
+
+import Data.List (isPrefixOf)
+import Data.String (String)
+import qualified Data.Text as T
+import Data.Version (showVersion)
+import System.Directory
+import qualified System.Posix.Process as Posix
+
+import Paths_radicle (getBinDir, version)
+
+-- | All command filenames are prefixed with this.
+radPrefix :: String
+radPrefix = "rad-"
+
+runCommand :: [String] -> IO ()
+runCommand [] = do
+ bin <- getBinDir
+ cmds <- availableCommands [bin]
+ putStr @Text "usage: rad <command> [<args>]\n\n"
+ putStrLn @Text "Available rad commands:"
+ putStr $ T.unlines $ [" " <> T.pack c | c <- cmds]
+runCommand ("version":_) =
+ putStrLn ("rad version " <> toS (showVersion version) :: Text)
+runCommand ("help":_) =
+ runCommand []
+runCommand ("--help":_) =
+ runCommand []
+runCommand ("-h":_) =
+ runCommand []
+runCommand (name:args) = do
+ result <- findExecutable (radPrefix <> name)
+ case result of
+ Just exe ->
+ Posix.executeFile exe False args Nothing
+ Nothing -> do
+ die $ T.unwords
+ [ "rad:", "'" <> toS name <> "'"
+ , "is not a rad command. See 'rad help' for a list of available commands."
+ ]
+
+-- | Built-in commands.
+builtinCommands :: [String]
+builtinCommands =
+ ["version", "help"]
+
+-- | Lists commands available for running.
+availableCommands
+ :: [FilePath] -- ^ Directories to search
+ -> IO [String] -- ^ Available commands
+availableCommands dirs =
+ concat <$> forM dirs (\dir -> do
+ result <- try $ do
+ xs <- listDirectory dir
+ pure $ [drop (length radPrefix) x | x <- xs, toS radPrefix `isPrefixOf` x]
+ pure $ case result of
+ Left (_ :: IOException) -> builtinCommands
+ Right xs -> xs ++ builtinCommands)
+
+main :: IO ()
+main = runCommand =<< getArgs
diff --git a/package.yaml b/package.yaml
index b836819..206fcc7 100644
--- a/package.yaml
+++ b/package.yaml
@@ -100,6 +100,16 @@ tests:
- tasty-hunit
executables:
+ rad:
+ main: Rad.hs
+ source-dirs: exe
+ other-modules: [Paths_radicle]
+ dependencies:
+ - directory
+ - unix
+ - text
+ ghc-options: -main-is Rad -O3 -threaded
+
radicle:
main: RadicleExe.hs
source-dirs: exe