Files
acms/cli/app/Main.hs

108 lines
3.3 KiB
Haskell
Raw Normal View History

2024-10-11 14:17:33 +02:00
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE NoFieldSelectors #-}
{-# LANGUAGE ApplicativeDo #-}
2024-10-11 14:17:33 +02:00
module Main where
2024-10-11 15:44:54 +02:00
import ACMS.API.REST.Collection qualified
2024-10-11 14:17:33 +02:00
import Control.Applicative ((<**>))
import Data.Aeson qualified as J
import Data.ByteString.Lazy qualified as LB
import Data.Text qualified as T
import Options.Applicative qualified as O
import Text.ParserCombinators.ReadP qualified as R
import Text.ParserCombinators.ReadPrec qualified as R
import Text.Read (Read (..))
data Args = Args
{ cmd :: Cmd
}
args :: O.Parser Args
args = Args <$> cmd_
2024-10-11 16:53:42 +02:00
data Cmd = Collection CollectionCmd
2024-10-11 14:17:33 +02:00
cmd_ :: O.Parser Cmd
cmd_ =
O.hsubparser . mconcat $
[ O.command "collection" . O.info collectionCmd $
O.progDesc "Manage content collections"
]
2024-10-11 16:53:42 +02:00
data CollectionCmd
= CollectionAdd CollectionName
| CollectionView CollectionPath
| CollectionEdit CollectionPath
| CollectionDelete CollectionPath
newtype CollectionName = CollectionName T.Text
deriving (Read)
2024-10-11 14:17:33 +02:00
data CollectionPath = CollectionPath
2024-10-11 16:53:42 +02:00
{ collectionName :: CollectionName,
2024-10-11 14:17:33 +02:00
fileName :: T.Text
}
instance Read CollectionPath where
readPrec = R.lift do
2024-10-11 16:53:42 +02:00
(CollectionName . T.pack -> collectionName) <- R.munch (/= '/')
2024-10-11 14:17:33 +02:00
_ <- R.string "/"
(T.pack -> fileName) <- do
fileName <- R.munch (liftA2 (&&) (/= '.') (/= '/'))
fileExt <- R.string ".json"
pure (fileName <> fileExt)
pure CollectionPath {..}
instance Show CollectionPath where
2024-10-11 16:53:42 +02:00
show (CollectionPath {collectionName = CollectionName cn, fileName}) =
show (cn <> "/" <> fileName)
2024-10-11 14:17:33 +02:00
collectionCmd :: O.Parser Cmd
collectionCmd = do
2024-10-11 16:53:42 +02:00
fmap Collection . O.hsubparser . mconcat $
[ O.command "add" . O.info (CollectionAdd <$> collectionNameArg) $
2024-10-11 15:45:24 +02:00
O.progDesc "Add an entity"
2024-10-11 16:53:42 +02:00
, O.command "view" . O.info (CollectionView <$> collectionPathArg) $
O.progDesc "View an entity"
2024-10-11 16:53:42 +02:00
, O.command "edit" . O.info (CollectionEdit <$> collectionPathArg) $
O.progDesc "Edit an entity"
2024-10-11 16:53:42 +02:00
, O.command "delete" . O.info (CollectionDelete <$> collectionPathArg) $
O.progDesc "Delete an entity"
2024-10-11 14:17:33 +02:00
]
collectionPathArg :: O.Parser CollectionPath
collectionPathArg =
O.argument O.auto (O.metavar "COLLECTION_PATH")
2024-10-11 16:53:42 +02:00
collectionNameArg :: O.Parser CollectionName
collectionNameArg =
CollectionName . T.pack <$> O.strArgument (O.metavar "COLLECTION_NAME")
2024-10-11 14:17:33 +02:00
main :: IO ()
main =
2024-10-11 14:17:33 +02:00
O.execParser (O.info (args <**> O.helper) O.idm) >>= \case
Args
2024-10-11 16:53:42 +02:00
{ cmd = Collection cmd
} -> case cmd of
CollectionAdd (CollectionName cn) -> do
print
2024-10-11 16:53:42 +02:00
=<< ACMS.API.REST.Collection.create cn
=<< J.throwDecode
=<< LB.getContents
2024-10-11 16:53:42 +02:00
CollectionView CollectionPath {collectionName = CollectionName cn, fileName} ->
print
2024-10-11 16:53:42 +02:00
=<< ACMS.API.REST.Collection.read cn fileName
CollectionDelete CollectionPath {collectionName = CollectionName cn, fileName}->
print
2024-10-11 16:53:42 +02:00
=<< ACMS.API.REST.Collection.delete cn fileName
CollectionEdit CollectionPath {collectionName = CollectionName cn, fileName}->
print
2024-10-11 16:53:42 +02:00
=<< ACMS.API.REST.Collection.update cn fileName
=<< J.throwDecode
=<< LB.getContents