module Hadolint.Formatter
  ( OutputFormat (..),
    Result (..),
    write,
    readMaybeOutputFormat,
  )
where

import Data.Text (Text)
import Hadolint.Formatter.Format
import Language.Docker.Parser (DockerfileError)
import qualified Hadolint.Formatter.Checkstyle as FormatCheckstyle
import qualified Hadolint.Formatter.Codacy as FormatCodacy
import qualified Hadolint.Formatter.Codeclimate as FormatCodeclimate
import qualified Hadolint.Formatter.Gnu as FormatGnu
import qualified Hadolint.Formatter.JUnit as FormatJUnit
import qualified Hadolint.Formatter.Json as FormatJson
import qualified Hadolint.Formatter.Sarif as FormatSarif
import qualified Hadolint.Formatter.SonarQube as FormatSonarQube
import qualified Hadolint.Formatter.TTY as FormatTTY
import System.IO


hWrite :: Foldable f =>
  Handle ->
  OutputFormat ->
  Bool ->
  Maybe FilePath ->
  f (Result Text DockerfileError) ->
  IO ()
hWrite :: forall (f :: * -> *).
Foldable f =>
Handle
-> OutputFormat
-> Bool
-> Maybe FilePath
-> f (Result Text DockerfileError)
-> IO ()
hWrite Handle
handle OutputFormat
format Bool
nocolor Maybe FilePath
filePathInReport f (Result Text DockerfileError)
allResults =
  case OutputFormat
format of
    OutputFormat
Checkstyle -> Handle
-> f (Result Text DockerfileError) -> Maybe FilePath -> IO ()
forall (f :: * -> *) s e.
(Foldable f, VisualStream s, TraversableStream s,
 ShowErrorComponent e) =>
Handle -> f (Result s e) -> Maybe FilePath -> IO ()
FormatCheckstyle.hWrite Handle
handle f (Result Text DockerfileError)
allResults Maybe FilePath
filePathInReport
    OutputFormat
Codacy -> Handle -> f (Result Text DockerfileError) -> IO ()
forall (f :: * -> *) s e.
(Foldable f, VisualStream s, TraversableStream s,
 ShowErrorComponent e) =>
Handle -> f (Result s e) -> IO ()
FormatCodacy.hWrite Handle
handle f (Result Text DockerfileError)
allResults
    OutputFormat
CodeclimateJson -> Handle
-> f (Result Text DockerfileError) -> Maybe FilePath -> IO ()
forall s e (f :: * -> *).
(VisualStream s, TraversableStream s, ShowErrorComponent e,
 Foldable f) =>
Handle -> f (Result s e) -> Maybe FilePath -> IO ()
FormatCodeclimate.hWrite Handle
handle f (Result Text DockerfileError)
allResults Maybe FilePath
filePathInReport
    OutputFormat
GitLabCodeclimateJson -> Handle
-> f (Result Text DockerfileError) -> Maybe FilePath -> IO ()
forall (f :: * -> *) s e.
(Foldable f, VisualStream s, TraversableStream s,
 ShowErrorComponent e) =>
Handle -> f (Result s e) -> Maybe FilePath -> IO ()
FormatCodeclimate.hWriteGitLab Handle
handle f (Result Text DockerfileError)
allResults Maybe FilePath
filePathInReport
    OutputFormat
Gnu -> Handle -> f (Result Text DockerfileError) -> IO ()
forall s e (f :: * -> *).
(VisualStream s, TraversableStream s, ShowErrorComponent e,
 Foldable f) =>
Handle -> f (Result s e) -> IO ()
FormatGnu.hWrite Handle
handle f (Result Text DockerfileError)
allResults
    OutputFormat
JUnit -> Handle
-> f (Result Text DockerfileError) -> Maybe FilePath -> IO ()
forall (f :: * -> *) s e.
(Foldable f, VisualStream s, TraversableStream s,
 ShowErrorComponent e) =>
Handle -> f (Result s e) -> Maybe FilePath -> IO ()
FormatJUnit.hWrite Handle
handle f (Result Text DockerfileError)
allResults Maybe FilePath
filePathInReport
    OutputFormat
Json -> Handle -> f (Result Text DockerfileError) -> IO ()
forall s e (f :: * -> *).
(VisualStream s, TraversableStream s, ShowErrorComponent e,
 Foldable f) =>
Handle -> f (Result s e) -> IO ()
FormatJson.hWrite Handle
handle f (Result Text DockerfileError)
allResults
    OutputFormat
Sarif -> Handle -> f (Result Text DockerfileError) -> IO ()
forall s e (f :: * -> *).
(VisualStream s, TraversableStream s, ShowErrorComponent e,
 Foldable f) =>
Handle -> f (Result s e) -> IO ()
FormatSarif.hWrite Handle
handle f (Result Text DockerfileError)
allResults
    OutputFormat
SonarQube -> Handle
-> f (Result Text DockerfileError) -> Maybe FilePath -> IO ()
forall s e (f :: * -> *).
(VisualStream s, TraversableStream s, ShowErrorComponent e,
 Foldable f) =>
Handle -> f (Result s e) -> Maybe FilePath -> IO ()
FormatSonarQube.hWrite Handle
handle f (Result Text DockerfileError)
allResults Maybe FilePath
filePathInReport
    OutputFormat
TTY -> Handle -> f (Result Text DockerfileError) -> Bool -> IO ()
forall s e (f :: * -> *).
(VisualStream s, TraversableStream s, ShowErrorComponent e,
 Foldable f) =>
Handle -> f (Result s e) -> Bool -> IO ()
FormatTTY.hWrite Handle
handle f (Result Text DockerfileError)
allResults Bool
nocolor


write :: Foldable f =>
  [FilePath] ->
  [OutputFormat] ->
  Bool ->
  Maybe FilePath ->
  f (Result Text DockerfileError) ->
  IO ()
write :: forall (f :: * -> *).
Foldable f =>
[FilePath]
-> [OutputFormat]
-> Bool
-> Maybe FilePath
-> f (Result Text DockerfileError)
-> IO ()
write [FilePath]
paths [OutputFormat]
formats Bool
nocolor Maybe FilePath
filePathInReport f (Result Text DockerfileError)
allResults =
  ((FilePath, OutputFormat) -> IO ())
-> [(FilePath, OutputFormat)] -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (FilePath, OutputFormat) -> IO ()
writePath [(FilePath, OutputFormat)]
ziplist
  where
    ziplist :: [(FilePath, OutputFormat)]
ziplist = FilePath
-> OutputFormat
-> [FilePath]
-> [OutputFormat]
-> [(FilePath, OutputFormat)]
zipWithDef FilePath
"-" OutputFormat
TTY [FilePath]
paths [OutputFormat]
formats
    writePath :: (FilePath, OutputFormat) -> IO ()
writePath ( FilePath
p, OutputFormat
f ) =
      if FilePath
p FilePath -> FilePath -> Bool
forall a. Eq a => a -> a -> Bool
== FilePath
"-" then Handle
-> OutputFormat
-> Bool
-> Maybe FilePath
-> f (Result Text DockerfileError)
-> IO ()
forall (f :: * -> *).
Foldable f =>
Handle
-> OutputFormat
-> Bool
-> Maybe FilePath
-> f (Result Text DockerfileError)
-> IO ()
hWrite Handle
stdout OutputFormat
f Bool
nocolor Maybe FilePath
filePathInReport f (Result Text DockerfileError)
allResults
      else FilePath -> IOMode -> (Handle -> IO ()) -> IO ()
forall r. FilePath -> IOMode -> (Handle -> IO r) -> IO r
withFile FilePath
p IOMode
WriteMode (\Handle
h -> Handle
-> OutputFormat
-> Bool
-> Maybe FilePath
-> f (Result Text DockerfileError)
-> IO ()
forall (f :: * -> *).
Foldable f =>
Handle
-> OutputFormat
-> Bool
-> Maybe FilePath
-> f (Result Text DockerfileError)
-> IO ()
hWrite Handle
h OutputFormat
f Bool
nocolor Maybe FilePath
filePathInReport f (Result Text DockerfileError)
allResults)


zipWithDef :: FilePath -> OutputFormat -> [FilePath] -> [OutputFormat] -> [(FilePath, OutputFormat)]
zipWithDef :: FilePath
-> OutputFormat
-> [FilePath]
-> [OutputFormat]
-> [(FilePath, OutputFormat)]
zipWithDef FilePath
defP OutputFormat
defF []     []     = [(FilePath
defP, OutputFormat
defF)]
zipWithDef FilePath
defP OutputFormat
_    []     [OutputFormat
f]    = [(FilePath
defP, OutputFormat
f)]
zipWithDef FilePath
_    OutputFormat
defF [FilePath
p]    []     = [(FilePath
p, OutputFormat
defF)]
zipWithDef FilePath
_    OutputFormat
_    [FilePath
p]    [OutputFormat
f]    = [(FilePath
p, OutputFormat
f)]
zipWithDef FilePath
defP OutputFormat
defF []     (OutputFormat
f:[OutputFormat]
fs) = (FilePath
defP, OutputFormat
f)(FilePath, OutputFormat)
-> [(FilePath, OutputFormat)] -> [(FilePath, OutputFormat)]
forall a. a -> [a] -> [a]
:FilePath
-> OutputFormat
-> [FilePath]
-> [OutputFormat]
-> [(FilePath, OutputFormat)]
zipWithDef FilePath
defP OutputFormat
defF [] [OutputFormat]
fs
zipWithDef FilePath
defP OutputFormat
defF (FilePath
p:[FilePath]
ps) []     = (FilePath
p, OutputFormat
defF)(FilePath, OutputFormat)
-> [(FilePath, OutputFormat)] -> [(FilePath, OutputFormat)]
forall a. a -> [a] -> [a]
:FilePath
-> OutputFormat
-> [FilePath]
-> [OutputFormat]
-> [(FilePath, OutputFormat)]
zipWithDef FilePath
defP OutputFormat
defF [FilePath]
ps []
zipWithDef FilePath
defP OutputFormat
defF (FilePath
p:[FilePath]
ps) (OutputFormat
f:[OutputFormat]
fs) = (FilePath
p, OutputFormat
f)(FilePath, OutputFormat)
-> [(FilePath, OutputFormat)] -> [(FilePath, OutputFormat)]
forall a. a -> [a] -> [a]
:FilePath
-> OutputFormat
-> [FilePath]
-> [OutputFormat]
-> [(FilePath, OutputFormat)]
zipWithDef FilePath
defP OutputFormat
defF [FilePath]
ps [OutputFormat]
fs