module Hadolint.Process (run) where

import Hadolint.Config.Configuration (Configuration (..))
import Hadolint.Rule (CheckFailure (..), Failures, Rule, RuleCode)
import Language.Docker.Syntax
import qualified Control.Foldl as Foldl
import qualified Data.IntMap.Strict as SMap
import qualified Data.Sequence as Seq
import qualified Data.Set as Set
import qualified Data.Text as Text
import qualified Hadolint.Pragma
import qualified Hadolint.Rule.DL1001
import qualified Hadolint.Rule.DL3000
import qualified Hadolint.Rule.DL3001
import qualified Hadolint.Rule.DL3002
import qualified Hadolint.Rule.DL3003
import qualified Hadolint.Rule.DL3004
import qualified Hadolint.Rule.DL3005
import qualified Hadolint.Rule.DL3006
import qualified Hadolint.Rule.DL3007
import qualified Hadolint.Rule.DL3008
import qualified Hadolint.Rule.DL3009
import qualified Hadolint.Rule.DL3010
import qualified Hadolint.Rule.DL3011
import qualified Hadolint.Rule.DL3012
import qualified Hadolint.Rule.DL3013
import qualified Hadolint.Rule.DL3014
import qualified Hadolint.Rule.DL3015
import qualified Hadolint.Rule.DL3016
import qualified Hadolint.Rule.DL3018
import qualified Hadolint.Rule.DL3019
import qualified Hadolint.Rule.DL3020
import qualified Hadolint.Rule.DL3021
import qualified Hadolint.Rule.DL3022
import qualified Hadolint.Rule.DL3023
import qualified Hadolint.Rule.DL3024
import qualified Hadolint.Rule.DL3025
import qualified Hadolint.Rule.DL3026
import qualified Hadolint.Rule.DL3027
import qualified Hadolint.Rule.DL3028
import qualified Hadolint.Rule.DL3029
import qualified Hadolint.Rule.DL3030
import qualified Hadolint.Rule.DL3032
import qualified Hadolint.Rule.DL3033
import qualified Hadolint.Rule.DL3034
import qualified Hadolint.Rule.DL3035
import qualified Hadolint.Rule.DL3036
import qualified Hadolint.Rule.DL3037
import qualified Hadolint.Rule.DL3038
import qualified Hadolint.Rule.DL3040
import qualified Hadolint.Rule.DL3041
import qualified Hadolint.Rule.DL3042
import qualified Hadolint.Rule.DL3043
import qualified Hadolint.Rule.DL3044
import qualified Hadolint.Rule.DL3045
import qualified Hadolint.Rule.DL3046
import qualified Hadolint.Rule.DL3047
import qualified Hadolint.Rule.DL3048
import qualified Hadolint.Rule.DL3049
import qualified Hadolint.Rule.DL3050
import qualified Hadolint.Rule.DL3051
import qualified Hadolint.Rule.DL3052
import qualified Hadolint.Rule.DL3053
import qualified Hadolint.Rule.DL3054
import qualified Hadolint.Rule.DL3055
import qualified Hadolint.Rule.DL3056
import qualified Hadolint.Rule.DL3057
import qualified Hadolint.Rule.DL3058
import qualified Hadolint.Rule.DL3059
import qualified Hadolint.Rule.DL3060
import qualified Hadolint.Rule.DL3061
import qualified Hadolint.Rule.DL4000
import qualified Hadolint.Rule.DL4001
import qualified Hadolint.Rule.DL4003
import qualified Hadolint.Rule.DL4004
import qualified Hadolint.Rule.DL4005
import qualified Hadolint.Rule.DL4006
import qualified Hadolint.Rule.Shellcheck
import qualified Hadolint.Shell as Shell


data AnalisisResult = AnalisisResult
  { -- | The set of ignored rules per line
    AnalisisResult -> IntMap (Set RuleCode)
ignored :: SMap.IntMap (Set.Set RuleCode),
    -- | The set of globally ignored rules
    AnalisisResult -> Set RuleCode
globalIgnored :: Set.Set RuleCode,
    -- | A set of failures collected for reach rule
    AnalisisResult -> Seq CheckFailure
failed :: Failures
  }

run :: Configuration -> [InstructionPos Text.Text] -> Failures
run :: Configuration -> [InstructionPos Text] -> Seq CheckFailure
run Configuration
config [InstructionPos Text]
dockerfile = (CheckFailure -> Bool) -> Seq CheckFailure -> Seq CheckFailure
forall a. (a -> Bool) -> Seq a -> Seq a
Seq.filter CheckFailure -> Bool
shouldKeep Seq CheckFailure
failed
  where
    AnalisisResult {Set RuleCode
Seq CheckFailure
IntMap (Set RuleCode)
ignored :: AnalisisResult -> IntMap (Set RuleCode)
globalIgnored :: AnalisisResult -> Set RuleCode
failed :: AnalisisResult -> Seq CheckFailure
failed :: Seq CheckFailure
ignored :: IntMap (Set RuleCode)
globalIgnored :: Set RuleCode
..} = Fold (InstructionPos Text) AnalisisResult
-> [InstructionPos Text] -> AnalisisResult
forall (f :: * -> *) a b. Foldable f => Fold a b -> f a -> b
Foldl.fold (Configuration -> Fold (InstructionPos Text) AnalisisResult
analyze Configuration
config) [InstructionPos Text]
dockerfile

    shouldKeep :: CheckFailure -> Bool
shouldKeep CheckFailure {Linenumber
line :: Linenumber
line :: CheckFailure -> Linenumber
line, RuleCode
code :: RuleCode
code :: CheckFailure -> RuleCode
code}
      | Configuration -> Bool
disableIgnorePragma Configuration
config = Bool
True
      | RuleCode
code RuleCode -> Set RuleCode -> Bool
forall a. Ord a => a -> Set a -> Bool
`Set.member` Set RuleCode
globalIgnored = Bool
False
      | Bool
otherwise = Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
True Maybe Bool -> Maybe Bool -> Bool
forall a. Eq a => a -> a -> Bool
/= do
          Set RuleCode
ignoreList <- Linenumber -> IntMap (Set RuleCode) -> Maybe (Set RuleCode)
forall a. Linenumber -> IntMap a -> Maybe a
SMap.lookup Linenumber
line IntMap (Set RuleCode)
ignored
          Bool -> Maybe Bool
forall a. a -> Maybe a
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool -> Maybe Bool) -> Bool -> Maybe Bool
forall a b. (a -> b) -> a -> b
$ RuleCode
code RuleCode -> Set RuleCode -> Bool
forall a. Ord a => a -> Set a -> Bool
`Set.member` Set RuleCode
ignoreList

analyze ::
  Configuration ->
  Foldl.Fold (InstructionPos Text.Text) AnalisisResult
analyze :: Configuration -> Fold (InstructionPos Text) AnalisisResult
analyze Configuration
config =
  IntMap (Set RuleCode)
-> Set RuleCode -> Seq CheckFailure -> AnalisisResult
AnalisisResult
    (IntMap (Set RuleCode)
 -> Set RuleCode -> Seq CheckFailure -> AnalisisResult)
-> Fold (InstructionPos Text) (IntMap (Set RuleCode))
-> Fold
     (InstructionPos Text)
     (Set RuleCode -> Seq CheckFailure -> AnalisisResult)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Fold (InstructionPos Text) (IntMap (Set RuleCode))
Hadolint.Pragma.ignored
    Fold
  (InstructionPos Text)
  (Set RuleCode -> Seq CheckFailure -> AnalisisResult)
-> Fold (InstructionPos Text) (Set RuleCode)
-> Fold (InstructionPos Text) (Seq CheckFailure -> AnalisisResult)
forall a b.
Fold (InstructionPos Text) (a -> b)
-> Fold (InstructionPos Text) a -> Fold (InstructionPos Text) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Fold (InstructionPos Text) (Set RuleCode)
Hadolint.Pragma.globalIgnored
    Fold (InstructionPos Text) (Seq CheckFailure -> AnalisisResult)
-> Fold (InstructionPos Text) (Seq CheckFailure)
-> Fold (InstructionPos Text) AnalisisResult
forall a b.
Fold (InstructionPos Text) (a -> b)
-> Fold (InstructionPos Text) a -> Fold (InstructionPos Text) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (InstructionPos Text -> InstructionPos ParsedShell)
-> Rule ParsedShell
-> Fold (InstructionPos Text) (Seq CheckFailure)
forall a b r. (a -> b) -> Fold b r -> Fold a r
Foldl.premap InstructionPos Text -> InstructionPos ParsedShell
parseShell (Configuration -> Rule ParsedShell
failures Configuration
config)

parseShell :: InstructionPos Text.Text -> InstructionPos Shell.ParsedShell
parseShell :: InstructionPos Text -> InstructionPos ParsedShell
parseShell = (Text -> ParsedShell)
-> InstructionPos Text -> InstructionPos ParsedShell
forall a b. (a -> b) -> InstructionPos a -> InstructionPos b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Text -> ParsedShell
Shell.parseShell

failures :: Configuration -> Rule Shell.ParsedShell
failures :: Configuration -> Rule ParsedShell
failures Configuration {Set Registry
allowedRegistries :: Set Registry
allowedRegistries :: Configuration -> Set Registry
allowedRegistries, LabelSchema
labelSchema :: LabelSchema
labelSchema :: Configuration -> LabelSchema
labelSchema, Bool
strictLabels :: Bool
strictLabels :: Configuration -> Bool
strictLabels} =
  Rule ParsedShell
Hadolint.Rule.DL1001.rule
    Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> Rule ParsedShell
forall args. Rule args
Hadolint.Rule.DL3000.rule
    Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> Rule ParsedShell
Hadolint.Rule.DL3001.rule
    Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> Rule ParsedShell
forall args. Rule args
Hadolint.Rule.DL3002.rule
    Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> Rule ParsedShell
Hadolint.Rule.DL3003.rule
    Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> Rule ParsedShell
Hadolint.Rule.DL3004.rule
    Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> Rule ParsedShell
Hadolint.Rule.DL3005.rule
    Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> Rule ParsedShell
forall args. Rule args
Hadolint.Rule.DL3006.rule
    Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> Rule ParsedShell
forall args. Rule args
Hadolint.Rule.DL3007.rule
    Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> Rule ParsedShell
Hadolint.Rule.DL3008.rule
    Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> Rule ParsedShell
Hadolint.Rule.DL3009.rule
    Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> Rule ParsedShell
Hadolint.Rule.DL3010.rule
    Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> Rule ParsedShell
forall args. Rule args
Hadolint.Rule.DL3011.rule
    Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> Rule ParsedShell
forall args. Rule args
Hadolint.Rule.DL3012.rule
    Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> Rule ParsedShell
Hadolint.Rule.DL3013.rule
    Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> Rule ParsedShell
Hadolint.Rule.DL3014.rule
    Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> Rule ParsedShell
Hadolint.Rule.DL3015.rule
    Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> Rule ParsedShell
Hadolint.Rule.DL3016.rule
    Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> Rule ParsedShell
Hadolint.Rule.DL3018.rule
    Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> Rule ParsedShell
Hadolint.Rule.DL3019.rule
    Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> Rule ParsedShell
forall args. Rule args
Hadolint.Rule.DL3020.rule
    Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> Rule ParsedShell
forall args. Rule args
Hadolint.Rule.DL3021.rule
    Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> Rule ParsedShell
forall args. Rule args
Hadolint.Rule.DL3022.rule
    Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> Rule ParsedShell
forall args. Rule args
Hadolint.Rule.DL3023.rule
    Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> Rule ParsedShell
forall args. Rule args
Hadolint.Rule.DL3024.rule
    Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> Rule ParsedShell
forall args. Rule args
Hadolint.Rule.DL3025.rule
    Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> Set Registry -> Rule ParsedShell
forall args. Set Registry -> Rule args
Hadolint.Rule.DL3026.rule Set Registry
allowedRegistries
    Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> Rule ParsedShell
Hadolint.Rule.DL3027.rule
    Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> Rule ParsedShell
Hadolint.Rule.DL3028.rule
    Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> Rule ParsedShell
forall args. Rule args
Hadolint.Rule.DL3029.rule
    Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> Rule ParsedShell
Hadolint.Rule.DL3030.rule
    Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> Rule ParsedShell
Hadolint.Rule.DL3032.rule
    Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> Rule ParsedShell
Hadolint.Rule.DL3033.rule
    Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> Rule ParsedShell
Hadolint.Rule.DL3034.rule
    Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> Rule ParsedShell
Hadolint.Rule.DL3035.rule
    Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> Rule ParsedShell
Hadolint.Rule.DL3036.rule
    Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> Rule ParsedShell
Hadolint.Rule.DL3037.rule
    Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> Rule ParsedShell
Hadolint.Rule.DL3038.rule
    Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> Rule ParsedShell
Hadolint.Rule.DL3040.rule
    Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> Rule ParsedShell
Hadolint.Rule.DL3041.rule
    Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> Rule ParsedShell
Hadolint.Rule.DL3042.rule
    Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> Rule ParsedShell
forall args. Rule args
Hadolint.Rule.DL3043.rule
    Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> Rule ParsedShell
forall args. Rule args
Hadolint.Rule.DL3044.rule
    Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> Rule ParsedShell
forall args. Rule args
Hadolint.Rule.DL3045.rule
    Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> Rule ParsedShell
Hadolint.Rule.DL3046.rule
    Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> Rule ParsedShell
Hadolint.Rule.DL3047.rule
    Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> Rule ParsedShell
forall args. Rule args
Hadolint.Rule.DL3048.rule
    Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> LabelSchema -> Rule ParsedShell
forall args. LabelSchema -> Rule args
Hadolint.Rule.DL3049.rule LabelSchema
labelSchema
    Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> LabelSchema -> Bool -> Rule ParsedShell
forall args. LabelSchema -> Bool -> Rule args
Hadolint.Rule.DL3050.rule LabelSchema
labelSchema Bool
strictLabels
    Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> LabelSchema -> Rule ParsedShell
forall args. LabelSchema -> Rule args
Hadolint.Rule.DL3051.rule LabelSchema
labelSchema
    Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> LabelSchema -> Rule ParsedShell
forall args. LabelSchema -> Rule args
Hadolint.Rule.DL3052.rule LabelSchema
labelSchema
    Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> LabelSchema -> Rule ParsedShell
forall args. LabelSchema -> Rule args
Hadolint.Rule.DL3053.rule LabelSchema
labelSchema
    Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> LabelSchema -> Rule ParsedShell
forall args. LabelSchema -> Rule args
Hadolint.Rule.DL3054.rule LabelSchema
labelSchema
    Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> LabelSchema -> Rule ParsedShell
forall args. LabelSchema -> Rule args
Hadolint.Rule.DL3055.rule LabelSchema
labelSchema
    Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> LabelSchema -> Rule ParsedShell
forall args. LabelSchema -> Rule args
Hadolint.Rule.DL3056.rule LabelSchema
labelSchema
    Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> Rule ParsedShell
forall args. Rule args
Hadolint.Rule.DL3057.rule
    Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> LabelSchema -> Rule ParsedShell
forall args. LabelSchema -> Rule args
Hadolint.Rule.DL3058.rule LabelSchema
labelSchema
    Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> Rule ParsedShell
Hadolint.Rule.DL3059.rule
    Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> Rule ParsedShell
Hadolint.Rule.DL3060.rule
    Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> Rule ParsedShell
forall args. Rule args
Hadolint.Rule.DL3061.rule
    Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> Rule ParsedShell
forall args. Rule args
Hadolint.Rule.DL4000.rule
    Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> Rule ParsedShell
Hadolint.Rule.DL4001.rule
    Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> Rule ParsedShell
forall args. Rule args
Hadolint.Rule.DL4003.rule
    Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> Rule ParsedShell
forall args. Rule args
Hadolint.Rule.DL4004.rule
    Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> Rule ParsedShell
Hadolint.Rule.DL4005.rule
    Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> Rule ParsedShell
Hadolint.Rule.DL4006.rule
    Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> Rule ParsedShell
Hadolint.Rule.Shellcheck.rule