-- | Hadolint GNU Format Output
--
-- See https://www.gnu.org/prep/standards/html_node/Errors.html for reference.

module Hadolint.Formatter.Gnu
  ( printResults,
  )
where


import Data.Text (Text, pack)
import Hadolint.Formatter.Format
import Hadolint.Rule (CheckFailure (..), RuleCode (..))
import Text.Megaparsec (TraversableStream)
import Text.Megaparsec.Error
import Text.Megaparsec.Stream (VisualStream)
import qualified Data.List.NonEmpty as NonEmpty
import qualified Data.Text.IO as TextIO


printResults ::
  (VisualStream s, TraversableStream s, ShowErrorComponent e, Foldable f) =>
  f (Result s e) ->
  IO ()
printResults :: forall s e (f :: * -> *).
(VisualStream s, TraversableStream s, ShowErrorComponent e,
 Foldable f) =>
f (Result s e) -> IO ()
printResults = forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ forall s e.
(VisualStream s, TraversableStream s, ShowErrorComponent e) =>
Result s e -> IO ()
printResult


printResult ::
  (VisualStream s, TraversableStream s, ShowErrorComponent e) =>
  Result s e ->
  IO ()
printResult :: forall s e.
(VisualStream s, TraversableStream s, ShowErrorComponent e) =>
Result s e -> IO ()
printResult (Result Text
filename Seq (ParseErrorBundle s e)
errors Failures
checks) =
  forall s e (f :: * -> *).
(VisualStream s, TraversableStream s, ShowErrorComponent e,
 Foldable f) =>
f (ParseErrorBundle s e) -> IO ()
printErrors Seq (ParseErrorBundle s e)
errors forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (f :: * -> *). Foldable f => Text -> f CheckFailure -> IO ()
printChecks Text
filename Failures
checks


printErrors ::
  (VisualStream s, TraversableStream s, ShowErrorComponent e, Foldable f) =>
  f (ParseErrorBundle s e) ->
  IO ()
printErrors :: forall s e (f :: * -> *).
(VisualStream s, TraversableStream s, ShowErrorComponent e,
 Foldable f) =>
f (ParseErrorBundle s e) -> IO ()
printErrors = forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (Text -> IO ()
TextIO.putStrLn forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s e.
(VisualStream s, TraversableStream s, ShowErrorComponent e) =>
ParseErrorBundle s e -> Text
formatError)


printChecks :: (Foldable f) => Text -> f CheckFailure -> IO ()
printChecks :: forall (f :: * -> *). Foldable f => Text -> f CheckFailure -> IO ()
printChecks Text
filename = forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (Text -> IO ()
TextIO.putStrLn forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> CheckFailure -> Text
formatCheck Text
filename)


formatError ::
  (VisualStream s, TraversableStream s, ShowErrorComponent e) =>
  ParseErrorBundle s e ->
  Text
formatError :: forall s e.
(VisualStream s, TraversableStream s, ShowErrorComponent e) =>
ParseErrorBundle s e -> Text
formatError err :: ParseErrorBundle s e
err@(ParseErrorBundle NonEmpty (ParseError s e)
e PosState s
_) =
  String -> Text
pack forall a b. (a -> b) -> a -> b
$
    String
"hadolint:"
      forall a. Semigroup a => a -> a -> a
<> String -> String
stripNewlines
          ( forall s e. TraversableStream s => ParseErrorBundle s e -> String
errorPositionPretty ParseErrorBundle s e
err
              forall a. Semigroup a => a -> a -> a
<> String
": "
              forall a. Semigroup a => a -> a -> a
<> forall s e.
(VisualStream s, ShowErrorComponent e) =>
ParseError s e -> String
parseErrorTextPretty (forall a. NonEmpty a -> a
NonEmpty.head NonEmpty (ParseError s e)
e)
          )


formatCheck :: Text -> CheckFailure -> Text
formatCheck :: Text -> CheckFailure -> Text
formatCheck Text
source (CheckFailure RuleCode
code DLSeverity
severity Text
message Linenumber
line) =
  Text
"hadolint:"
    forall a. Semigroup a => a -> a -> a
<> Text
source
    forall a. Semigroup a => a -> a -> a
<> Text
":"
    forall a. Semigroup a => a -> a -> a
<> String -> Text
pack (forall a. Show a => a -> String
show Linenumber
line)
    forall a. Semigroup a => a -> a -> a
<> Text
": "
    forall a. Semigroup a => a -> a -> a
<> RuleCode -> Text
unRuleCode RuleCode
code
    forall a. Semigroup a => a -> a -> a
<> Text
" "
    forall a. Semigroup a => a -> a -> a
<> DLSeverity -> Text
severityText DLSeverity
severity
    forall a. Semigroup a => a -> a -> a
<> Text
": "
    forall a. Semigroup a => a -> a -> a
<> Text
message