module Hadolint.Rule.DL3023 (rule) where

import Hadolint.Rule
import Language.Docker.Syntax

rule :: Rule args
rule :: Rule args
rule = (Linenumber
 -> State (Maybe (Instruction args))
 -> Instruction args
 -> State (Maybe (Instruction args)))
-> State (Maybe (Instruction args)) -> Rule args
forall a args.
(Linenumber -> State a -> Instruction args -> State a)
-> State a -> Rule args
customRule Linenumber
-> State (Maybe (Instruction args))
-> Instruction args
-> State (Maybe (Instruction args))
forall a.
Linenumber
-> State (Maybe (Instruction a))
-> Instruction a
-> State (Maybe (Instruction a))
check (Maybe (Instruction args) -> State (Maybe (Instruction args))
forall a. a -> State a
emptyState Maybe (Instruction args)
forall a. Maybe a
Nothing)
  where
    code :: RuleCode
code = RuleCode
"DL3023"
    severity :: DLSeverity
severity = DLSeverity
DLErrorC
    message :: Text
message = Text
"`COPY --from` cannot reference its own `FROM` alias"

    check :: Linenumber
-> State (Maybe (Instruction a))
-> Instruction a
-> State (Maybe (Instruction a))
check Linenumber
_ State (Maybe (Instruction a))
st f :: Instruction a
f@(From BaseImage
_) = State (Maybe (Instruction a))
st State (Maybe (Instruction a))
-> (State (Maybe (Instruction a)) -> State (Maybe (Instruction a)))
-> State (Maybe (Instruction a))
forall a b. a -> (a -> b) -> b
|> Maybe (Instruction a)
-> State (Maybe (Instruction a)) -> State (Maybe (Instruction a))
forall a. a -> State a -> State a
replaceWith (Instruction a -> Maybe (Instruction a)
forall a. a -> Maybe a
Just Instruction a
f) -- Remember the last FROM instruction found
    check Linenumber
line st :: State (Maybe (Instruction a))
st@(State Failures
_ (Just Instruction a
fromInstr)) (Copy (CopyArgs NonEmpty SourcePath
_ TargetPath
_ Chown
_ Chmod
_ (CopySource Text
stageName)))
      | (Text -> Bool) -> Instruction a -> Bool
forall a. (Text -> Bool) -> Instruction a -> Bool
aliasMustBe (Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
/= Text
stageName) Instruction a
fromInstr = State (Maybe (Instruction a))
st
      | Bool
otherwise = State (Maybe (Instruction a))
st State (Maybe (Instruction a))
-> (State (Maybe (Instruction a)) -> State (Maybe (Instruction a)))
-> State (Maybe (Instruction a))
forall a b. a -> (a -> b) -> b
|> CheckFailure
-> State (Maybe (Instruction a)) -> State (Maybe (Instruction a))
forall a. CheckFailure -> State a -> State a
addFail CheckFailure :: RuleCode -> DLSeverity -> Text -> Linenumber -> CheckFailure
CheckFailure {Linenumber
Text
RuleCode
DLSeverity
line :: Linenumber
message :: Text
severity :: DLSeverity
code :: RuleCode
line :: Linenumber
message :: Text
severity :: DLSeverity
code :: RuleCode
..}
    -- cannot copy from the same stage!
    check Linenumber
_ State (Maybe (Instruction a))
st Instruction a
_ = State (Maybe (Instruction a))
st
{-# INLINEABLE rule #-}