git.iko.soy/typeable/compaREST
on master help sign in
70 lines 2.4 kB history raw
1module Main (main) where
3import Control.Monad
4import Control.Monad.Except
5import Control.Monad.Trans (lift)
6import Data.Aeson
7import qualified Data.ByteString.Lazy as BSL
8import Data.Default
9import Data.Maybe
10import Data.OpenApi.Compare.Options
11import Data.OpenApi.Compare.Run
12import qualified Data.Text.IO as T
13import qualified Data.Yaml as Yaml
14import FormatHeuristic
15import System.Exit
16import System.IO
17import Text.Pandoc hiding (report)
18import Text.Pandoc.Builder
20main :: IO ()
21main = do
22 opts <- parseOptions
23 let parseSchema path =
24 eitherDecodeFileStrict path >>= \case
25 Left jsonErr -> do
26 Yaml.decodeFileEither path >>= \case
27 Left yamlErr -> do
28 putStrLn "Could not parse as json or yaml"
29 print jsonErr
30 print yamlErr
31 fail "Exiting"
32 Right s -> pure s
33 Right s -> pure s
34 a <- parseSchema (clientFile opts)
35 b <- parseSchema (serverFile opts)
36 let runPandocIO :: PandocIO a -> ExceptT Errors IO a
37 runPandocIO x = lift (runIO x) >>= either (throwError . DocumentError) pure
38 options = def {writerExtensions = githubMarkdownExtensions}
39 write :: Pandoc -> ExceptT Errors IO ()
40 write = case outputMode opts of
41 StdoutMode -> lift . T.putStrLn <=< runPandocIO . writeMarkdown options
42 FileMode f -> case formatFromFilePath f of
43 Nothing -> \_ -> throwError UnknownOutputFormat
44 Just (writer, f') -> lift . BSL.writeFile f' <=< runPandocIO . writer
45 reportConfig =
46 ReportConfig
47 { treeStyle = reportTreeStyle opts
48 , reportMode = fromMaybe All $ mode opts
49 }
50 (report, status) = runReport reportConfig (a, b)
51 case mode opts of
52 Just _ -> either handler pure <=< runExceptT $ write $ doc report
53 Nothing -> pure ()
54 when (signalExitCode opts) $
55 case status of
56 NoBreakingChanges -> exitSuccess
57 BreakingChanges -> exitWith $ ExitFailure 1
58 OnlyUnsupportedChanges -> exitWith $ ExitFailure 2
60data Errors
61 = DocumentError PandocError
62 | UnknownOutputFormat
64handler :: Errors -> IO a
65handler (DocumentError err) = do
66 T.hPutStrLn stderr (renderError err)
67 exitWith $ ExitFailure 100
68handler UnknownOutputFormat = do
69 T.hPutStrLn stderr "Could not determine output format from file extension."
70 exitWith $ ExitFailure 101