on master help sign in
Refined CLI and added options (#114)

* show full help on error * Added signal-exit-code flag

commit23bb0f78cc27a54bb269325270a68aa587ece35a authoriko <ilyakooo0@gmail.com> dateFri 23 Jul 2021, 12:49 (5 years ago) parentd50b2ba
3 files changed+27−9
README.md+8−1 app/Main.hs+6−5 app/OpenAPI/Checker/Options.hs+13−3
README.md+8−1
@@ -12,6 +12,7 @@ Compatibility checker for OpenAPI
1212Usage: openapi-diff (-c|--client ARG) (-s|--server ARG)
1313 [--silent | --only-breaking | --all] [-o|--output ARG]
1414 [--folding-block-quotes-style | --header-style]
15+ [--signal-exit-code]
1516 A tool to check compatibility between two OpenApi specifications.
1617
1718 Usage examples
@@ -43,7 +44,8 @@ Available options:
4344 -s,--server ARG A path to the file containing the specification that
4445 will be used for the server of the API. Can be either
4546 a YAML or JSON file.
46− --silent Silence all output.
47+ --silent Silence all output. Only makes sense in combination
48+ with --signal-exit-code.
4749 --only-breaking Only report breaking changes in the output.
4850 --all Report both incompatible and compatible changes.
4951 Compatible changes will not trigger a failure exit
@@ -67,4 +69,9 @@ Available options:
6769 format. Markdown has rendering bugs on GitHub.
6870 --header-style The report tree is structured using increasing levels
6971 of headers.
72+ --signal-exit-code Signal API compatibility with the exit code.
73+
74+ Exit with 0 if there are no breaking changes.
75+ Exit with 1 if there are breaking changes.
76+ Exit with 2 if could not determine compatibility.
7077```
app/Main.hs+6−5
@@ -17,7 +17,7 @@ import Text.Pandoc hiding (report)
1717
1818main :: IO ()
1919main = do
20− opts <- execParser optionsParserInfo
20+ opts <- parseOptions
2121 let parseSchema path =
2222 eitherDecodeFileStrict path >>= \case
2323 Left jsonErr -> do
@@ -49,10 +49,11 @@ main = do
4949 case mode opts of
5050 Just _ -> either handler pure <=< runExceptT $ write report
5151 Nothing -> pure ()
52− case status of
53− NoBreakingChanges -> exitSuccess
54− BreakingChanges -> exitWith $ ExitFailure 1
55− OnlyUnsupportedChanges -> exitWith $ ExitFailure 2
52+ when (signalExitCode opts) $
53+ case status of
54+ NoBreakingChanges -> exitSuccess
55+ BreakingChanges -> exitWith $ ExitFailure 1
56+ OnlyUnsupportedChanges -> exitWith $ ExitFailure 2
5657
5758data Errors
5859 = DocumentError PandocError
app/OpenAPI/Checker/Options.hs+13−3
@@ -1,8 +1,7 @@
11module OpenAPI.Checker.Options
22 ( Options (..)
33 , OutputMode (..)
4− , optionsParserInfo
5− , execParser
4+ , parseOptions
65 )
76where
87
@@ -11,6 +10,9 @@ import OpenAPI.Checker.Report
1110import Options.Applicative
1211import Options.Applicative.Help hiding (fullDesc)
1312
13+parseOptions :: IO Options
14+parseOptions = customExecParser (prefs $ showHelpOnError) optionsParserInfo
15+
1416data Options = Options
1517 { clientFile :: FilePath
1618 , serverFile :: FilePath
@@ -18,6 +20,7 @@ data Options = Options
1820 mode :: Maybe ReportMode
1921 , outputMode :: OutputMode
2022 , reportTreeStyle :: ReportTreeStyle
23+ , signalExitCode :: Bool
2124 }
2225 deriving stock (Generic)
2326
@@ -62,7 +65,7 @@ optionsParser =
6265 <*> (flag'
6366 Nothing
6467 (long "silent"
65− <> help "Silence all output.")
68+ <> help "Silence all output. Only makes sense in combination with --signal-exit-code.")
6669 <|> flag'
6770 (Just OnlyErrors)
6871 (long "only-breaking"
@@ -100,6 +103,13 @@ optionsParser =
100103 "The report tree is structured using \
101104 \increasing levels of headers.")
102105 <|> pure HeadersTreeStyle)
106+ <*> switch
107+ (long "signal-exit-code"
108+ <> helpDoc (Just $
109+ par "Signal API compatibility with the exit code."
110+ <$$> hardline <> par "Exit with 0 if there are no breaking changes."
111+ <$$> par "Exit with 1 if there are breaking changes."
112+ <$$> par "Exit with 2 if could not determine compatibility."))
103113
104114par :: String -> Doc
105115par = foldr1 (</>) . fmap string . words