git.iko.soy/typeable/compaREST
on master help sign in
Slightly better output signalling (#58)
commit44a3c27f4c5ce15f23910d0c49576e17b1fd32f7 authoriko <ilyakooo0@gmail.com> dateFri 21 May 2021, 14:31 (5 years ago) parent0261ac1
2 files changed+56−42
src/OpenAPI/Checker/Options.hs+42−38 src/OpenAPI/Checker/Run.hs+14−4
src/OpenAPI/Checker/Options.hs+42−38
@@ -1,8 +1,9 @@
11module OpenAPI.Checker.Options
2− ( Options (..),
3− Mode (..),
4− optionsParserInfo,
5− execParser,
2+ ( Options (..)
3+ , Mode (..)
4+ , OutputMode (..)
5+ , optionsParserInfo
6+ , execParser
67 )
78where
89
@@ -10,52 +11,55 @@ import GHC.Generics (Generic)
1011import Options.Applicative
1112
1213data Options = Options
13− { clientFile :: FilePath,
14− serverFile :: FilePath,
15− mode :: Mode
14+ { clientFile :: FilePath
15+ , serverFile :: FilePath
16+ , mode :: Mode
17+ , outputMode :: OutputMode
1618 }
1719 deriving stock (Generic)
1820
1921data Mode = Silent | OnlyErrors | All
2022
23+data OutputMode = StdoutMode | FileMode FilePath
24+
2125optionsParserInfo :: ParserInfo Options
2226optionsParserInfo =
2327 info
2428 (helper <*> optionsParser)
25− ( fullDesc
26− <> header "openapi-diff"
27− <> progDesc "A tool to check compatibility between two OpenApi specifications."
28− )
29+ (fullDesc
30+ <> header "openapi-diff"
31+ <> progDesc "A tool to check compatibility between two OpenApi specifications.")
2932
3033optionsParser :: Parser Options
3134optionsParser =
3235 Options
3336 <$> strOption
34− ( short 'c'
35− <> long "client"
36− <> help "The specification that will be used for the client of the API."
37− )
37+ (short 'c'
38+ <> long "client"
39+ <> help "The specification that will be used for the client of the API.")
3840 <*> strOption
39− ( short 's'
40− <> long "server"
41− <> help "The specification that will be used for the server of the API."
42− )
43− <*> ( flag'
44− Silent
45− ( long "silent"
46− <> help "Silence all output."
47− )
48− <|> flag'
49− OnlyErrors
50− ( long "only-errors"
51− <> help "Only report incompatibility errors in the output."
52− )
53− <|> flag'
54− All
55− ( long "all"
56− <> help
57− "Report both incompatible and compatible changes. \
58− \Compatible changes will not trigger a failure exit code."
59− )
60− <|> pure All
61− )
41+ (short 's'
42+ <> long "server"
43+ <> help "The specification that will be used for the server of the API.")
44+ <*> (flag'
45+ Silent
46+ (long "silent"
47+ <> help "Silence all output.")
48+ <|> flag'
49+ OnlyErrors
50+ (long "only-errors"
51+ <> help "Only report incompatibility errors in the output.")
52+ <|> flag'
53+ All
54+ (long "all"
55+ <> help
56+ "Report both incompatible and compatible changes. \
57+ \Compatible changes will not trigger a failure exit code.")
58+ <|> pure All)
59+ <*> (pure StdoutMode
60+ <|> (FileMode
61+ <$> strOption
62+ (short 'o' <> long "output"
63+ <> help
64+ "The file path where the output should be writtrn. \
65+ \Leave blank to output result to stdout.")))
src/OpenAPI/Checker/Run.hs+14−4
@@ -5,9 +5,10 @@ import qualified Data.ByteString.Char8 as BSC
55import Data.HList
66import qualified Data.Yaml as Yaml
77import OpenAPI.Checker.Options
8−import OpenAPI.Checker.Subtree
98import OpenAPI.Checker.Paths
9+import OpenAPI.Checker.Subtree
1010import OpenAPI.Checker.Validate.OpenApi ()
11+import System.Exit
1112import Prelude hiding (id, (.))
1213
1314runChecker :: IO ()
@@ -19,12 +20,21 @@ runChecker = do
1920 Yaml.decodeFileEither path >>= \case
2021 Left yamlErr -> do
2122 putStrLn "Could not parse as json or yaml"
22− putStrLn $ show jsonErr
23− putStrLn $ show yamlErr
23+ print jsonErr
24+ print yamlErr
2425 fail "Exiting"
2526 Right s -> pure s
2627 Right s -> pure s
2728 a <- traced Root <$> parseSchema (clientFile opts)
2829 b <- traced Root <$> parseSchema (serverFile opts)
2930 let report = runCompatFormula $ checkCompatibility HNil Root (ProdCons a b)
30− BSC.putStrLn $ Yaml.encode report
31+ output = case outputMode opts of
32+ StdoutMode -> BSC.putStrLn
33+ FileMode f -> BSC.writeFile f
34+ case report of
35+ Right () -> do
36+ output "No breaking changes found ✨"
37+ exitSuccess
38+ Left e -> do
39+ output $ Yaml.encode e
40+ exitFailure