cli: add CRUD operations for collections

This commit is contained in:
2024-10-11 16:04:57 +02:00
parent 33ecbd5734
commit e6023a1137

View File

@@ -4,6 +4,7 @@
{-# LANGUAGE RecordWildCards #-} {-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ViewPatterns #-} {-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE NoFieldSelectors #-} {-# LANGUAGE NoFieldSelectors #-}
{-# LANGUAGE ApplicativeDo #-}
module Main where module Main where
@@ -24,7 +25,10 @@ data Args = Args
args :: O.Parser Args args :: O.Parser Args
args = Args <$> cmd_ args = Args <$> cmd_
data Cmd = Collection CollectionCmd data Cmd = Collection
{ operation :: CollectionCmd
, filePath :: CollectionPath
}
cmd_ :: O.Parser Cmd cmd_ :: O.Parser Cmd
cmd_ = cmd_ =
@@ -33,9 +37,7 @@ cmd_ =
O.progDesc "Manage content collections" O.progDesc "Manage content collections"
] ]
data CollectionCmd = CollectionAdd data CollectionCmd = CollectionAdd | CollectionView | CollectionEdit | CollectionDelete
{ filePath :: CollectionPath
}
data CollectionPath = CollectionPath data CollectionPath = CollectionPath
{ collectionName :: T.Text, { collectionName :: T.Text,
@@ -57,32 +59,47 @@ instance Show CollectionPath where
show (collectionName <> "/" <> fileName) show (collectionName <> "/" <> fileName)
collectionCmd :: O.Parser Cmd collectionCmd :: O.Parser Cmd
collectionCmd = collectionCmd = do
fmap Collection . O.hsubparser . mconcat $ operation <- O.hsubparser $ mconcat $
[ O.command "add" . O.info collectionAddCmd $ [ O.command "add" . O.info (pure CollectionAdd) $
O.progDesc "Add an entity" O.progDesc "Add an entity"
, O.command "view" . O.info (pure CollectionView) $
O.progDesc "View an entity"
, O.command "edit" . O.info (pure CollectionEdit) $
O.progDesc "Edit an entity"
, O.command "delete" . O.info (pure CollectionDelete) $
O.progDesc "Delete an entity"
] ]
filePath <- collectionPathArg
collectionAddCmd :: O.Parser CollectionCmd pure $ Collection {..}
collectionAddCmd =
CollectionAdd
<$> collectionPathArg
collectionPathArg :: O.Parser CollectionPath collectionPathArg :: O.Parser CollectionPath
collectionPathArg = collectionPathArg =
O.argument O.auto (O.metavar "COLLECTION_PATH") O.argument O.auto (O.metavar "COLLECTION_PATH")
main :: IO () main :: IO ()
main = do main =
O.execParser (O.info (args <**> O.helper) O.idm) >>= \case O.execParser (O.info (args <**> O.helper) O.idm) >>= \case
Args Args
{ cmd = { cmd =
Collection Collection
CollectionAdd { operation = operation
{ filePath = CollectionPath {collectionName, fileName} , filePath = CollectionPath {collectionName, fileName}
} }
} -> } -> case operation of
print CollectionAdd ->
=<< ACMS.API.REST.Collection.create collectionName fileName print
=<< J.throwDecode =<< ACMS.API.REST.Collection.create collectionName fileName
=<< LB.getContents =<< J.throwDecode
=<< LB.getContents
CollectionView ->
print
=<< ACMS.API.REST.Collection.read collectionName fileName
CollectionDelete ->
print
=<< ACMS.API.REST.Collection.delete collectionName fileName
CollectionEdit ->
print
=<< ACMS.API.REST.Collection.update collectionName fileName
=<< J.throwDecode
=<< LB.getContents