2021-02-24 09:44:45 +03:00
|
|
|
module Main (main) where
|
|
|
|
|
2021-06-05 19:15:04 +03:00
|
|
|
import Control.Monad
|
|
|
|
import Control.Monad.Except
|
|
|
|
import Data.Aeson
|
|
|
|
import qualified Data.ByteString.Lazy as BSL
|
|
|
|
import Data.Default
|
2021-07-02 16:17:53 +03:00
|
|
|
import Data.Maybe
|
2021-08-16 13:41:22 +03:00
|
|
|
import Data.OpenApi.Compare.Options
|
|
|
|
import Data.OpenApi.Compare.Run
|
2021-06-05 19:15:04 +03:00
|
|
|
import qualified Data.Text.IO as T
|
|
|
|
import qualified Data.Yaml as Yaml
|
|
|
|
import FormatHeuristic
|
|
|
|
import System.Exit
|
|
|
|
import System.IO
|
2021-06-21 20:52:48 +03:00
|
|
|
import Text.Pandoc hiding (report)
|
2021-08-06 11:49:00 +03:00
|
|
|
import Text.Pandoc.Builder
|
2021-02-24 09:44:45 +03:00
|
|
|
|
|
|
|
main :: IO ()
|
2021-06-05 19:15:04 +03:00
|
|
|
main = do
|
2021-07-23 12:49:52 +03:00
|
|
|
opts <- parseOptions
|
2021-06-05 19:15:04 +03:00
|
|
|
let parseSchema path =
|
|
|
|
eitherDecodeFileStrict path >>= \case
|
|
|
|
Left jsonErr -> do
|
|
|
|
Yaml.decodeFileEither path >>= \case
|
|
|
|
Left yamlErr -> do
|
|
|
|
putStrLn "Could not parse as json or yaml"
|
|
|
|
print jsonErr
|
|
|
|
print yamlErr
|
|
|
|
fail "Exiting"
|
|
|
|
Right s -> pure s
|
|
|
|
Right s -> pure s
|
2021-06-21 20:52:48 +03:00
|
|
|
a <- parseSchema (clientFile opts)
|
|
|
|
b <- parseSchema (serverFile opts)
|
|
|
|
let runPandocIO :: PandocIO a -> ExceptT Errors IO a
|
2021-06-05 19:15:04 +03:00
|
|
|
runPandocIO x = lift (runIO x) >>= either (throwError . DocumentError) pure
|
2021-06-15 18:57:35 +03:00
|
|
|
options = def {writerExtensions = githubMarkdownExtensions}
|
2021-06-21 20:52:48 +03:00
|
|
|
write :: Pandoc -> ExceptT Errors IO ()
|
|
|
|
write = case outputMode opts of
|
|
|
|
StdoutMode -> lift . T.putStrLn <=< runPandocIO . writeMarkdown options
|
2021-06-05 19:15:04 +03:00
|
|
|
FileMode f -> case formatFromFilePath f of
|
|
|
|
Nothing -> \_ -> throwError UnknownOutputFormat
|
2021-06-28 19:44:47 +03:00
|
|
|
Just (writer, f') -> lift . BSL.writeFile f' <=< runPandocIO . writer
|
2021-07-02 16:17:53 +03:00
|
|
|
reportConfig =
|
|
|
|
ReportConfig
|
|
|
|
{ treeStyle = reportTreeStyle opts
|
|
|
|
, reportMode = fromMaybe All $ mode opts
|
|
|
|
}
|
|
|
|
(report, status) = runReport reportConfig (a, b)
|
|
|
|
case mode opts of
|
2021-08-06 11:49:00 +03:00
|
|
|
Just _ -> either handler pure <=< runExceptT $ write $ doc report
|
2021-07-02 16:17:53 +03:00
|
|
|
Nothing -> pure ()
|
2021-07-23 12:49:52 +03:00
|
|
|
when (signalExitCode opts) $
|
|
|
|
case status of
|
|
|
|
NoBreakingChanges -> exitSuccess
|
|
|
|
BreakingChanges -> exitWith $ ExitFailure 1
|
|
|
|
OnlyUnsupportedChanges -> exitWith $ ExitFailure 2
|
2021-06-05 19:15:04 +03:00
|
|
|
|
|
|
|
data Errors
|
|
|
|
= DocumentError PandocError
|
|
|
|
| UnknownOutputFormat
|
|
|
|
|
|
|
|
handler :: Errors -> IO a
|
|
|
|
handler (DocumentError err) = do
|
|
|
|
T.hPutStrLn stderr (renderError err)
|
|
|
|
exitWith $ ExitFailure 100
|
|
|
|
handler UnknownOutputFormat = do
|
|
|
|
T.hPutStrLn stderr "Could not determine output format from file extension."
|
|
|
|
exitWith $ ExitFailure 101
|