aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Alexander Foremny <aforemny@posteo.de>2024-10-11 17:12:38 +0200
committerLibravatar Alexander Foremny <aforemny@posteo.de>2024-10-11 17:12:38 +0200
commit35eb8836f39b21e5e4f7770936d5a5073ea2b783 (patch)
tree9cbd97e91128708d35d7c861e67a839f59be9bdc
parent8d68caeb8d33ed8390911bfccdb0648e6ff80c7b (diff)
add cli command `collection schema COLLECTION`
-rw-r--r--backend/lib/ACMS/API/REST/Collection.hs9
-rw-r--r--cli/app/Main.hs29
-rw-r--r--cli/cli.cabal1
3 files changed, 29 insertions, 10 deletions
diff --git a/backend/lib/ACMS/API/REST/Collection.hs b/backend/lib/ACMS/API/REST/Collection.hs
index ed0ae9a..375b20c 100644
--- a/backend/lib/ACMS/API/REST/Collection.hs
+++ b/backend/lib/ACMS/API/REST/Collection.hs
@@ -1,10 +1,12 @@
{-# LANGUAGE OverloadedStrings #-}
+
module ACMS.API.REST.Collection where
import Data.Aeson qualified as A
import Data.Aeson.KeyMap qualified as AM
import Data.ByteString.Lazy.UTF8 qualified as LB
import Data.Function ((&))
+import Data.String (fromString)
import Data.Text qualified as T
import Network.HTTP.Simple
import Text.Printf (printf)
@@ -58,3 +60,10 @@ delete c i =
(LB.fromString (printf "DELETE FROM %s WHERE %s.$fileName == \"%s\"" c c i))
& httpLBS
>>= A.throwDecode . getResponseBody
+
+schema :: T.Text -> IO A.Value
+schema c =
+ fromString (printf "http://localhost:8081/%s.schema.json" c)
+ & setRequestMethod "POST"
+ & httpLBS
+ >>= A.throwDecode . getResponseBody
diff --git a/cli/app/Main.hs b/cli/app/Main.hs
index 1fc3119..e8d9605 100644
--- a/cli/app/Main.hs
+++ b/cli/app/Main.hs
@@ -1,16 +1,17 @@
+{-# LANGUAGE ApplicativeDo #-}
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE NoFieldSelectors #-}
-{-# LANGUAGE ApplicativeDo #-}
module Main where
import ACMS.API.REST.Collection qualified
import Control.Applicative ((<**>))
import Data.Aeson qualified as J
+import Data.Aeson.Encode.Pretty qualified as J
import Data.ByteString.Lazy qualified as LB
import Data.Text qualified as T
import Options.Applicative qualified as O
@@ -39,6 +40,8 @@ data CollectionCmd
| CollectionView CollectionItem
| CollectionEdit CollectionItem
| CollectionDelete CollectionItem
+ | --
+ CollectionSchema Collection
newtype Collection = Collection T.Text
deriving (Read)
@@ -66,13 +69,16 @@ collectionCmd :: O.Parser Cmd
collectionCmd = do
fmap CollectionCmd . O.hsubparser . mconcat $
[ O.command "add" . O.info (CollectionAdd <$> collectionNameArg) $
- O.progDesc "Add an entity"
- , O.command "view" . O.info (CollectionView <$> collectionPathArg) $
- O.progDesc "View an entity"
- , O.command "edit" . O.info (CollectionEdit <$> collectionPathArg) $
- O.progDesc "Edit an entity"
- , O.command "delete" . O.info (CollectionDelete <$> collectionPathArg) $
- O.progDesc "Delete an entity"
+ O.progDesc "Add an entity",
+ O.command "view" . O.info (CollectionView <$> collectionPathArg) $
+ O.progDesc "View an entity",
+ O.command "edit" . O.info (CollectionEdit <$> collectionPathArg) $
+ O.progDesc "Edit an entity",
+ O.command "delete" . O.info (CollectionDelete <$> collectionPathArg) $
+ O.progDesc "Delete an entity",
+ --
+ O.command "schema" . O.info (CollectionSchema <$> collectionNameArg) $
+ O.progDesc "Show the collection's schema"
]
collectionPathArg :: O.Parser CollectionItem
@@ -97,11 +103,14 @@ main =
CollectionView CollectionItem {collectionName = Collection cn, fileName} ->
print
=<< ACMS.API.REST.Collection.read cn fileName
- CollectionDelete CollectionItem {collectionName = Collection cn, fileName}->
+ CollectionDelete CollectionItem {collectionName = Collection cn, fileName} ->
print
=<< ACMS.API.REST.Collection.delete cn fileName
- CollectionEdit CollectionItem {collectionName = Collection cn, fileName}->
+ CollectionEdit CollectionItem {collectionName = Collection cn, fileName} ->
print
=<< ACMS.API.REST.Collection.update cn fileName
=<< J.throwDecode
=<< LB.getContents
+ CollectionSchema (Collection cn) ->
+ LB.putStr . J.encodePretty
+ =<< ACMS.API.REST.Collection.schema cn
diff --git a/cli/cli.cabal b/cli/cli.cabal
index d0ad909..bb98d8b 100644
--- a/cli/cli.cabal
+++ b/cli/cli.cabal
@@ -16,6 +16,7 @@ executable cli
ghc-options: -Wall
build-depends:
aeson,
+ aeson-pretty,
backend,
base,
bytestring,