Skip to content

Commit ce2b882

Browse files
committed
Move Options to own module in source tree
Also add some tests checking parsing of IP
1 parent 557ecea commit ce2b882

File tree

3 files changed

+123
-16
lines changed

3 files changed

+123
-16
lines changed

db-server.cabal

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ library
5454
hs-source-dirs: src
5555
exposed-modules:
5656
Cardano.Tools.DBServer
57+
Cardano.Tools.DBServer.Options
5758

5859
build-depends:
5960
aeson,
@@ -92,6 +93,7 @@ library
9293
network,
9394
network-mux,
9495
nothunks,
96+
optparse-applicative,
9597
ouroboros-consensus ^>= 0.21,
9698
ouroboros-consensus-cardano,
9799
ouroboros-consensus-diffusion,
@@ -128,8 +130,7 @@ executable db-server
128130
optparse-applicative,
129131
db-server,
130132
with-utf8,
131-
other-modules:
132-
DBServer.Options
133+
133134

134135
test-suite db-server-test
135136
import: common-test
@@ -138,21 +139,23 @@ test-suite db-server-test
138139
main-is: Main.hs
139140
other-modules:
140141
Cardano.Tools.DBServerSpec
142+
Cardano.Tools.DBServer.OptionsSpec
141143
Cardano.Tools.TestHelper
142144
build-depends:
143-
QuickCheck,
144-
aeson,
145-
base,
146-
db-server,
147-
directory,
148-
filepath,
149-
hspec,
150-
http-types,
151-
mtl,
152-
temporary,
153-
text,
154-
unix,
155-
wai,
156-
wai-extra
145+
QuickCheck,
146+
aeson,
147+
base,
148+
db-server,
149+
directory,
150+
filepath,
151+
hspec,
152+
http-types,
153+
mtl,
154+
temporary,
155+
text,
156+
unix,
157+
wai,
158+
wai-extra,
159+
warp
157160
build-tool-depends:
158161
hspec-discover:hspec-discover
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
{-# LANGUAGE ApplicativeDo #-}
2+
{-# LANGUAGE DerivingStrategies #-}
3+
{-# LANGUAGE NamedFieldPuns #-}
4+
5+
module Cardano.Tools.DBServer.Options (Options (..), optsParser, parseArgs) where
6+
7+
import qualified Network.Socket as Socket
8+
import Options.Applicative (
9+
Parser,
10+
ParserInfo,
11+
ParserResult (..),
12+
auto,
13+
defaultPrefs,
14+
execParserPure,
15+
fullDesc,
16+
help,
17+
helper,
18+
info,
19+
long,
20+
metavar,
21+
option,
22+
progDesc,
23+
showDefault,
24+
strOption,
25+
value,
26+
)
27+
28+
-- * Options
29+
data Options = Options
30+
{ databaseDirectory :: FilePath
31+
, port :: Socket.PortNumber
32+
, host :: String
33+
, configurationFile :: FilePath
34+
}
35+
deriving stock (Eq, Show)
36+
37+
optsParser :: ParserInfo Options
38+
optsParser =
39+
info (helper <*> parse) $ fullDesc <> progDesc desc
40+
where
41+
desc = "Serve a ChainDB directory over HTTP"
42+
43+
parse :: Parser Options
44+
parse = do
45+
databaseDirectory <-
46+
strOption $
47+
mconcat
48+
[ long "db"
49+
, help "Path to the chain DB"
50+
, value "db"
51+
, metavar "PATH"
52+
]
53+
port <-
54+
option auto $
55+
mconcat
56+
[ long "port"
57+
, help "Port to listen on"
58+
, value 9003
59+
, showDefault
60+
]
61+
host <-
62+
strOption $
63+
mconcat
64+
[ long "host"
65+
, help "host to listen on"
66+
, value "127.0.0.1"
67+
, showDefault
68+
]
69+
configurationFile <-
70+
strOption $
71+
mconcat
72+
[ long "config"
73+
, help "Path to cardano-node config file. Note this file should reference existing genesis files."
74+
, metavar "PATH"
75+
, value "config.json"
76+
, showDefault
77+
]
78+
pure Options{databaseDirectory, port, host, configurationFile}
79+
80+
parseArgs :: [String] -> Either String Options
81+
parseArgs args =
82+
case execParserPure defaultPrefs optsParser args of
83+
Success opts -> Right opts
84+
Failure err -> Left (show err)
85+
CompletionInvoked _ -> Left "Unexpected completion invocation"
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module Cardano.Tools.DBServer.OptionsSpec where
2+
3+
import Cardano.Tools.DBServer.Options (Options (..), parseArgs)
4+
import Data.String (IsString (..))
5+
import Network.Wai.Handler.Warp (HostPreference)
6+
import Test.Hspec (Spec, describe, it, shouldBe)
7+
8+
spec :: Spec
9+
spec = do
10+
describe "optsParser" $ do
11+
it "can parse --host options" $ do
12+
parseArgs ["--host", "0.0.0.0"] `shouldBe` Right (Options "db" 9003 "0.0.0.0" "config.json")
13+
14+
it "parses host catch-all expressions" $ do
15+
-- NOTE: for some reason the constructors of HostPreference are not
16+
-- exported so we need to show them to be able to parse them 🤡
17+
show (fromString "!4" :: HostPreference) `shouldBe` "HostIPv4Only"
18+
show (fromString "*" :: HostPreference) `shouldBe` "HostAny"
19+
show (fromString "0.0.0.0" :: HostPreference) `shouldBe` "Host \"0.0.0.0\""

0 commit comments

Comments
 (0)