module Hadolint.Rule.DL3023 (rule) where

import Hadolint.Rule
import Language.Docker.Syntax

rule :: Rule args
rule :: forall args. Rule args
rule = forall a args.
(Linenumber -> State a -> Instruction args -> State a)
-> State a -> Rule args
customRule forall {a}.
Linenumber
-> State (Maybe (Instruction a))
-> Instruction a
-> State (Maybe (Instruction a))
check (forall a. a -> State a
emptyState 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 forall a b. a -> (a -> b) -> b
|> forall a. a -> State a -> State a
replaceWith (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
_) (CopyFlags Chown
_ Chmod
_ Link
_ (CopySource Text
stageName)))
      | forall a. (Text -> Bool) -> Instruction a -> Bool
aliasMustBe (forall a. Eq a => a -> a -> Bool
/= Text
stageName) Instruction a
fromInstr = State (Maybe (Instruction a))
st
      | Bool
otherwise = State (Maybe (Instruction a))
st forall a b. a -> (a -> b) -> b
|> forall a. CheckFailure -> State a -> State a
addFail 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 #-}