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.yamlindex d9f6879..f157e71 100644--- a/.stylish-haskell.yaml+++ b/.stylish-haskell.yaml@@ -203,3 +203,4 @@ language_extensions:- TupleSections- TypeFamilies- TypeOperators+ - TypeApplicationsdiff --git a/exe/Rad.hs b/exe/Rad.hsnew file mode 100644index 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 =<< getArgsdiff --git a/package.yaml b/package.yamlindex b836819..206fcc7 100644--- a/package.yaml+++ b/package.yaml@@ -100,6 +100,16 @@ tests:- tasty-hunitexecutables:+ 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.hssource-dirs: exe