Skip to content

Commit

Permalink
feat: Allow running postgrest without configuration
Browse files Browse the repository at this point in the history
Resolves #1823
  • Loading branch information
wolfgangwalther committed Jan 16, 2022
1 parent 3f0e360 commit 3b12ddf
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 23 deletions.
7 changes: 1 addition & 6 deletions main/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@

module Main (main) where

import qualified Data.Map.Strict as M

import System.IO (BufferMode (..), hSetBuffering)

import qualified PostgREST.App as App
import qualified PostgREST.CLI as CLI

import PostgREST.Config (readPGRSTEnvironment)

import Protolude

#ifndef mingw32_HOST_OS
Expand All @@ -20,8 +16,7 @@ import qualified PostgREST.Unix as Unix
main :: IO ()
main = do
setBuffering
hasPGRSTEnv <- not . M.null <$> readPGRSTEnvironment
opts <- CLI.readCLIShowHelp hasPGRSTEnv
opts <- CLI.readCLIShowHelp
CLI.main installSignalHandlers runAppInSocket opts

installSignalHandlers :: App.SignalHandlerInstaller
Expand Down
19 changes: 5 additions & 14 deletions src/PostgREST/CLI.hs
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ data Command
| CmdDumpSchema

-- | Read command line interface options. Also prints help.
readCLIShowHelp :: Bool -> IO CLI
readCLIShowHelp hasEnvironment =
readCLIShowHelp :: IO CLI
readCLIShowHelp =
O.customExecParser prefs opts
where
prefs = O.prefs $ O.showHelpOnError <> O.showHelpOnEmpty
opts = O.info parser $ O.fullDesc <> progDesc <> footer
opts = O.info parser $ O.fullDesc <> progDesc
parser = O.helper <*> exampleParser <*> cliParser

progDesc =
Expand All @@ -96,11 +96,6 @@ readCLIShowHelp hasEnvironment =
<> BS.unpack prettyVersion
<> " / create a REST API to an existing Postgres database"

footer =
O.footer $
"To run PostgREST, please pass the FILENAME argument"
<> " or set PGRST_ environment variables."

exampleParser =
O.infoOption exampleConfigFile $
O.long "example"
Expand All @@ -111,12 +106,12 @@ readCLIShowHelp hasEnvironment =
cliParser =
CLI
<$> (dumpConfigFlag <|> dumpSchemaFlag)
<*> optionalIf hasEnvironment configFileOption
<*> O.optional configFileOption

configFileOption =
O.strArgument $
O.metavar "FILENAME"
<> O.help "Path to configuration file (optional with PGRST_ environment variables)"
<> O.help "Path to configuration file"

dumpConfigFlag =
O.flag CmdRun CmdDumpConfig $
Expand All @@ -128,10 +123,6 @@ readCLIShowHelp hasEnvironment =
O.long "dump-schema"
<> O.help "Dump loaded schema as JSON and exit (for debugging, output structure is unstable)"

optionalIf :: Alternative f => Bool -> f a -> f (Maybe a)
optionalIf True = O.optional
optionalIf False = fmap Just

exampleConfigFile :: [Char]
exampleConfigFile =
[str|### REQUIRED:
Expand Down
4 changes: 1 addition & 3 deletions test/io/fixtures.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ cli:
args: ['-e']
- name: dump config
args: ['--dump-config']
use_defaultenv: true
- name: dump schema
args: ['--dump-schema']
use_defaultenv: true
# failures: config files
- name: no config
expect: error
# failures: config files
- name: non-existant config file
expect: error
args: ['does_not_exist.conf']
Expand Down

0 comments on commit 3b12ddf

Please sign in to comment.