2024-10-11 14:17:33 +02:00
|
|
|
{-# LANGUAGE BlockArguments #-}
|
|
|
|
|
{-# LANGUAGE LambdaCase #-}
|
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
{-# LANGUAGE RecordWildCards #-}
|
|
|
|
|
{-# LANGUAGE ViewPatterns #-}
|
|
|
|
|
{-# LANGUAGE NoFieldSelectors #-}
|
2024-10-11 16:04:57 +02:00
|
|
|
{-# 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:04:57 +02:00
|
|
|
data Cmd = Collection
|
|
|
|
|
{ operation :: CollectionCmd
|
|
|
|
|
, filePath :: CollectionPath
|
|
|
|
|
}
|
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:04:57 +02:00
|
|
|
data CollectionCmd = CollectionAdd | CollectionView | CollectionEdit | CollectionDelete
|
2024-10-11 14:17:33 +02:00
|
|
|
|
|
|
|
|
data CollectionPath = CollectionPath
|
|
|
|
|
{ collectionName :: T.Text,
|
|
|
|
|
fileName :: T.Text
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
instance Read CollectionPath where
|
|
|
|
|
readPrec = R.lift do
|
|
|
|
|
(T.pack -> collectionName) <- R.munch (/= '/')
|
|
|
|
|
_ <- R.string "/"
|
|
|
|
|
(T.pack -> fileName) <- do
|
|
|
|
|
fileName <- R.munch (liftA2 (&&) (/= '.') (/= '/'))
|
|
|
|
|
fileExt <- R.string ".json"
|
|
|
|
|
pure (fileName <> fileExt)
|
|
|
|
|
pure CollectionPath {..}
|
|
|
|
|
|
|
|
|
|
instance Show CollectionPath where
|
|
|
|
|
show (CollectionPath {collectionName, fileName}) =
|
|
|
|
|
show (collectionName <> "/" <> fileName)
|
|
|
|
|
|
|
|
|
|
collectionCmd :: O.Parser Cmd
|
2024-10-11 16:04:57 +02:00
|
|
|
collectionCmd = do
|
|
|
|
|
operation <- O.hsubparser $ mconcat $
|
|
|
|
|
[ O.command "add" . O.info (pure CollectionAdd) $
|
2024-10-11 15:45:24 +02:00
|
|
|
O.progDesc "Add an entity"
|
2024-10-11 16:04:57 +02:00
|
|
|
, 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"
|
2024-10-11 14:17:33 +02:00
|
|
|
]
|
2024-10-11 16:04:57 +02:00
|
|
|
filePath <- collectionPathArg
|
|
|
|
|
pure $ Collection {..}
|
2024-10-11 14:17:33 +02:00
|
|
|
|
|
|
|
|
collectionPathArg :: O.Parser CollectionPath
|
|
|
|
|
collectionPathArg =
|
|
|
|
|
O.argument O.auto (O.metavar "COLLECTION_PATH")
|
|
|
|
|
|
|
|
|
|
main :: IO ()
|
2024-10-11 16:04:57 +02:00
|
|
|
main =
|
2024-10-11 14:17:33 +02:00
|
|
|
O.execParser (O.info (args <**> O.helper) O.idm) >>= \case
|
|
|
|
|
Args
|
|
|
|
|
{ cmd =
|
|
|
|
|
Collection
|
2024-10-11 16:04:57 +02:00
|
|
|
{ operation = operation
|
|
|
|
|
, filePath = CollectionPath {collectionName, fileName}
|
|
|
|
|
}
|
|
|
|
|
} -> case operation of
|
|
|
|
|
CollectionAdd ->
|
|
|
|
|
print
|
|
|
|
|
=<< ACMS.API.REST.Collection.create collectionName fileName
|
|
|
|
|
=<< 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
|