Take config path as first param, not flag

Include example config in usage message
This commit is contained in:
Joe Nelson
2016-11-17 22:45:59 -08:00
committed by GitHub
parent 41c1cf6e01
commit 4df853eff9
2 changed files with 47 additions and 48 deletions
+1
View File
@@ -47,6 +47,7 @@ library
default-language: Haskell2010 default-language: Haskell2010
default-extensions: OverloadedStrings, QuasiQuotes, NoImplicitPrelude default-extensions: OverloadedStrings, QuasiQuotes, NoImplicitPrelude
build-depends: aeson build-depends: aeson
, ansi-wl-pprint
, base >= 4.8 && < 6 , base >= 4.8 && < 6
, bytestring , bytestring
, case-insensitive , case-insensitive
+46 -48
View File
@@ -28,7 +28,7 @@ import qualified Data.CaseInsensitive as CI
import qualified Data.Configurator as C import qualified Data.Configurator as C
import qualified Data.Configurator.Types as C import qualified Data.Configurator.Types as C
import Data.List (lookup) import Data.List (lookup)
import Data.Text (strip, intercalate) import Data.Text (strip, intercalate, lines)
import Data.Text.IO (hPutStrLn) import Data.Text.IO (hPutStrLn)
import Data.Version (versionBranch) import Data.Version (versionBranch)
import Network.Wai import Network.Wai
@@ -36,6 +36,7 @@ import Network.Wai.Middleware.Cors (CorsResourcePolicy (..))
import Options.Applicative hiding (str) import Options.Applicative hiding (str)
import Paths_postgrest (version) import Paths_postgrest (version)
import Text.Heredoc import Text.Heredoc
import Text.PrettyPrint.ANSI.Leijen hiding ((<>))
import Protolude hiding (intercalate import Protolude hiding (intercalate
, (<>)) , (<>))
@@ -85,35 +86,11 @@ prettyVersion = intercalate "." $ map show $ versionBranch version
-- | Function to read and parse options from the command line -- | Function to read and parse options from the command line
readOptions :: IO AppConfig readOptions :: IO AppConfig
readOptions = do readOptions = do
args <- customExecParser parserPrefs opts -- First read the config file path from command line
cfgPath <- customExecParser parserPrefs opts
when (caExample args) $ do -- Now read the actual config file
putStrLn (
[str|db-uri = "postgres://user:pass@localhost:5432/dbname"
|db-schema = "public"
|db-anon-role = "postgres"
|db-pool = 10
|
|server-host = "*4"
|server-port = 3000
|
|## base url for swagger output
|# server-proxy-uri = ""
|
|## choose a secret to enable JWT auth
|## (use "@filename" to load from separate file)
|# jwt-secret = "foo"
|
|## limit rows in response
|# max-rows = 1000
|
|## stored proc to exec immediately after auth
|# pre-request = "stored_proc_name"
|]::Text)
exitSuccess
conf <- catch conf <- catch
(C.load [C.Required $ caConfig args]) (C.load [C.Required cfgPath])
configNotfoundHint configNotfoundHint
handle missingKeyHint $ do handle missingKeyHint $ do
@@ -136,21 +113,24 @@ readOptions = do
cJwtSec cPool cMaxRows cReqCheck False cJwtSec cPool cMaxRows cReqCheck False
where where
opts = info (helper <*> argParser) $ opts = info (helper <*> pathParser) $
fullDesc fullDesc
<> progDesc ( <> progDesc (
"PostgREST " "PostgREST "
<> toS prettyVersion <> toS prettyVersion
<> " / create a REST API to an existing Postgres database" <> " / create a REST API to an existing Postgres database"
) )
<> footerDoc (Just $
text "Example Config File:"
<> nest 2 (hardline <> exampleCfg)
)
parserPrefs = prefs showHelpOnError parserPrefs = prefs showHelpOnError
configNotfoundHint :: IOError -> IO a configNotfoundHint :: IOError -> IO a
configNotfoundHint e = do configNotfoundHint e = do
hPutStrLn stderr $ intercalate "\n" [ hPutStrLn stderr $
"Cannot open config file:", "Cannot open config file:\n\t" <> show e
"\t" <> show e,
"\nUse the --help flag to learn how to fix this."]
exitFailure exitFailure
missingKeyHint :: C.KeyError -> IO a missingKeyHint :: C.KeyError -> IO a
@@ -160,18 +140,36 @@ readOptions = do
"Try the --example-config option to see how to configure PostgREST." "Try the --example-config option to see how to configure PostgREST."
exitFailure exitFailure
data CmdArgs = CmdArgs { exampleCfg :: Doc
caConfig :: FilePath exampleCfg = vsep . map (text . toS) . lines $
, caExample :: Bool [str|db-uri = "postgres://user:pass@localhost:5432/dbname"
} |db-schema = "public"
|db-anon-role = "postgres"
|db-pool = 10
|
|server-host = "*4"
|server-port = 3000
|
|## base url for swagger output
|# server-proxy-uri = ""
|
|## choose a secret to enable JWT auth
|## (use "@filename" to load from separate file)
|# jwt-secret = "foo"
|
|## limit rows in response
|# max-rows = 1000
|
|## stored proc to exec immediately after auth
|# pre-request = "stored_proc_name"
|]
argParser :: Parser CmdArgs
argParser = CmdArgs <$>
(toS <$> strOption
(short 'c' <> metavar "filename" <>
help "Path to configuration file")) <*>
switch (long "example-config" <> help "output an example config file")
pathParser :: Parser FilePath
pathParser =
strArgument $
metavar "FILENAME" <>
help "Path to configuration file"
data PgVersion = PgVersion { data PgVersion = PgVersion {
pgvNum :: Int32 pgvNum :: Int32