From 08d3a9f867bd92dafa3dc5ccc61254d25993130e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kier=C3=A1n=20Meinhardt?= Date: Fri, 11 Oct 2024 16:53:42 +0200 Subject: autogenerate collection item IDs --- cli/app/Main.hs | 62 +++++++++++++++++++++++++++++---------------------------- 1 file changed, 32 insertions(+), 30 deletions(-) (limited to 'cli') diff --git a/cli/app/Main.hs b/cli/app/Main.hs index b832f14..fb35b42 100644 --- a/cli/app/Main.hs +++ b/cli/app/Main.hs @@ -25,10 +25,7 @@ data Args = Args args :: O.Parser Args args = Args <$> cmd_ -data Cmd = Collection - { operation :: CollectionCmd - , filePath :: CollectionPath - } +data Cmd = Collection CollectionCmd cmd_ :: O.Parser Cmd cmd_ = @@ -37,16 +34,23 @@ cmd_ = O.progDesc "Manage content collections" ] -data CollectionCmd = CollectionAdd | CollectionView | CollectionEdit | CollectionDelete +data CollectionCmd + = CollectionAdd CollectionName + | CollectionView CollectionPath + | CollectionEdit CollectionPath + | CollectionDelete CollectionPath + +newtype CollectionName = CollectionName T.Text + deriving (Read) data CollectionPath = CollectionPath - { collectionName :: T.Text, + { collectionName :: CollectionName, fileName :: T.Text } instance Read CollectionPath where readPrec = R.lift do - (T.pack -> collectionName) <- R.munch (/= '/') + (CollectionName . T.pack -> collectionName) <- R.munch (/= '/') _ <- R.string "/" (T.pack -> fileName) <- do fileName <- R.munch (liftA2 (&&) (/= '.') (/= '/')) @@ -55,51 +59,49 @@ instance Read CollectionPath where pure CollectionPath {..} instance Show CollectionPath where - show (CollectionPath {collectionName, fileName}) = - show (collectionName <> "/" <> fileName) + show (CollectionPath {collectionName = CollectionName cn, fileName}) = + show (cn <> "/" <> fileName) collectionCmd :: O.Parser Cmd collectionCmd = do - operation <- O.hsubparser $ mconcat $ - [ O.command "add" . O.info (pure CollectionAdd) $ + fmap Collection . O.hsubparser . mconcat $ + [ O.command "add" . O.info (CollectionAdd <$> collectionNameArg) $ O.progDesc "Add an entity" - , O.command "view" . O.info (pure CollectionView) $ + , O.command "view" . O.info (CollectionView <$> collectionPathArg) $ O.progDesc "View an entity" - , O.command "edit" . O.info (pure CollectionEdit) $ + , O.command "edit" . O.info (CollectionEdit <$> collectionPathArg) $ O.progDesc "Edit an entity" - , O.command "delete" . O.info (pure CollectionDelete) $ + , O.command "delete" . O.info (CollectionDelete <$> collectionPathArg) $ O.progDesc "Delete an entity" ] - filePath <- collectionPathArg - pure $ Collection {..} collectionPathArg :: O.Parser CollectionPath collectionPathArg = O.argument O.auto (O.metavar "COLLECTION_PATH") +collectionNameArg :: O.Parser CollectionName +collectionNameArg = + CollectionName . T.pack <$> O.strArgument (O.metavar "COLLECTION_NAME") + main :: IO () main = O.execParser (O.info (args <**> O.helper) O.idm) >>= \case Args - { cmd = - Collection - { operation = operation - , filePath = CollectionPath {collectionName, fileName} - } - } -> case operation of - CollectionAdd -> + { cmd = Collection cmd + } -> case cmd of + CollectionAdd (CollectionName cn) -> do print - =<< ACMS.API.REST.Collection.create collectionName fileName + =<< ACMS.API.REST.Collection.create cn =<< J.throwDecode =<< LB.getContents - CollectionView -> + CollectionView CollectionPath {collectionName = CollectionName cn, fileName} -> print - =<< ACMS.API.REST.Collection.read collectionName fileName - CollectionDelete -> + =<< ACMS.API.REST.Collection.read cn fileName + CollectionDelete CollectionPath {collectionName = CollectionName cn, fileName}-> print - =<< ACMS.API.REST.Collection.delete collectionName fileName - CollectionEdit -> + =<< ACMS.API.REST.Collection.delete cn fileName + CollectionEdit CollectionPath {collectionName = CollectionName cn, fileName}-> print - =<< ACMS.API.REST.Collection.update collectionName fileName + =<< ACMS.API.REST.Collection.update cn fileName =<< J.throwDecode =<< LB.getContents -- cgit v1.2.3