git.iko.soy/typeable/compaREST
on master help sign in
Refined CLI and added options (#114)
23bb0f7iko
README.md+8−1
@@ -12,6 +12,7 @@ Compatibility checker for OpenAPI
12Usage: openapi-diff (-c|--client ARG) (-s|--server ARG)
12Usage: openapi-diff (-c|--client ARG) (-s|--server ARG)
13 [--silent | --only-breaking | --all] [-o|--output ARG]
13 [--silent | --only-breaking | --all] [-o|--output ARG]
14 [--folding-block-quotes-style | --header-style]
14 [--folding-block-quotes-style | --header-style]
15+ [--signal-exit-code]
15 A tool to check compatibility between two OpenApi specifications.
16 A tool to check compatibility between two OpenApi specifications.
16
17
17 Usage examples
18 Usage examples
@@ -43,7 +44,8 @@ Available options:
43 -s,--server ARG A path to the file containing the specification that
44 -s,--server ARG A path to the file containing the specification that
44 will be used for the server of the API. Can be either
45 will be used for the server of the API. Can be either
45 a YAML or JSON file.
46 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.
47 --only-breaking Only report breaking changes in the output.
49 --only-breaking Only report breaking changes in the output.
48 --all Report both incompatible and compatible changes.
50 --all Report both incompatible and compatible changes.
49 Compatible changes will not trigger a failure exit
51 Compatible changes will not trigger a failure exit
@@ -67,4 +69,9 @@ Available options:
67 format. Markdown has rendering bugs on GitHub.
69 format. Markdown has rendering bugs on GitHub.
68 --header-style The report tree is structured using increasing levels
70 --header-style The report tree is structured using increasing levels
69 of headers.
71 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.
70```
77```
app/Main.hs+6−5
@@ -17,7 +17,7 @@ import Text.Pandoc hiding (report)
17
17
18main :: IO ()
18main :: IO ()
19main = do
19main = do
20− opts <- execParser optionsParserInfo
20+ opts <- parseOptions
21 let parseSchema path =
21 let parseSchema path =
22 eitherDecodeFileStrict path >>= \case
22 eitherDecodeFileStrict path >>= \case
23 Left jsonErr -> do
23 Left jsonErr -> do
@@ -49,10 +49,11 @@ main = do
49 case mode opts of
49 case mode opts of
50 Just _ -> either handler pure <=< runExceptT $ write report
50 Just _ -> either handler pure <=< runExceptT $ write report
51 Nothing -> pure ()
51 Nothing -> pure ()
52+ when (signalExitCode opts) $
52− case status of
53+ case status of
53− NoBreakingChanges -> exitSuccess
54+ NoBreakingChanges -> exitSuccess
54− BreakingChanges -> exitWith $ ExitFailure 1
55+ BreakingChanges -> exitWith $ ExitFailure 1
55− OnlyUnsupportedChanges -> exitWith $ ExitFailure 2
56+ OnlyUnsupportedChanges -> exitWith $ ExitFailure 2
56
57
57data Errors
58data Errors
58 = DocumentError PandocError
59 = DocumentError PandocError
app/OpenAPI/Checker/Options.hs+13−3
@@ -1,8 +1,7 @@
1module OpenAPI.Checker.Options
1module OpenAPI.Checker.Options
2 ( Options (..)
2 ( Options (..)
3 , OutputMode (..)
3 , OutputMode (..)
4− , optionsParserInfo
4+ , parseOptions
5− , execParser
6 )
5 )
7where
6where
8
7
@@ -11,6 +10,9 @@ import OpenAPI.Checker.Report
11import Options.Applicative
10import Options.Applicative
12import Options.Applicative.Help hiding (fullDesc)
11import Options.Applicative.Help hiding (fullDesc)
13
12
13+parseOptions :: IO Options
14+parseOptions = customExecParser (prefs $ showHelpOnError) optionsParserInfo
15+
14data Options = Options
16data Options = Options
15 { clientFile :: FilePath
17 { clientFile :: FilePath
16 , serverFile :: FilePath
18 , serverFile :: FilePath
@@ -18,6 +20,7 @@ data Options = Options
18 mode :: Maybe ReportMode
20 mode :: Maybe ReportMode
19 , outputMode :: OutputMode
21 , outputMode :: OutputMode
20 , reportTreeStyle :: ReportTreeStyle
22 , reportTreeStyle :: ReportTreeStyle
23+ , signalExitCode :: Bool
21 }
24 }
22 deriving stock (Generic)
25 deriving stock (Generic)
23
26
@@ -62,7 +65,7 @@ optionsParser =
62 <*> (flag'
65 <*> (flag'
63 Nothing
66 Nothing
64 (long "silent"
67 (long "silent"
65− <> help "Silence all output.")
68+ <> help "Silence all output. Only makes sense in combination with --signal-exit-code.")
66 <|> flag'
69 <|> flag'
67 (Just OnlyErrors)
70 (Just OnlyErrors)
68 (long "only-breaking"
71 (long "only-breaking"
@@ -100,6 +103,13 @@ optionsParser =
100 "The report tree is structured using \
103 "The report tree is structured using \
101 \increasing levels of headers.")
104 \increasing levels of headers.")
102 <|> pure HeadersTreeStyle)
105 <|> 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."))
103
113
104par :: String -> Doc
114par :: String -> Doc
105par = foldr1 (</>) . fmap string . words
115par = foldr1 (</>) . fmap string . words