module Hadolint.Rule.DL3020 (rule) where

import Data.Foldable (toList)
import qualified Data.Text as Text
import Hadolint.Rule
import Language.Docker.Syntax

rule :: Rule args
rule :: Rule args
rule = RuleCode
-> DLSeverity -> Text -> (Instruction args -> Bool) -> Rule args
forall args.
RuleCode
-> DLSeverity -> Text -> (Instruction args -> Bool) -> Rule args
simpleRule RuleCode
code DLSeverity
severity Text
message Instruction args -> Bool
forall args. Instruction args -> Bool
check
  where
    code :: RuleCode
code = RuleCode
"DL3020"
    severity :: DLSeverity
severity = DLSeverity
DLErrorC
    message :: Text
message = Text
"Use COPY instead of ADD for files and folders"

    check :: Instruction args -> Bool
check (Add (AddArgs NonEmpty SourcePath
srcs TargetPath
_ Chown
_)) =
      [Bool] -> Bool
forall (t :: * -> *). Foldable t => t Bool -> Bool
and [Text -> Bool
isArchive Text
src Bool -> Bool -> Bool
|| Text -> Bool
isUrl Text
src | SourcePath Text
src <- NonEmpty SourcePath -> [SourcePath]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList NonEmpty SourcePath
srcs]
    check Instruction args
_ = Bool
True
{-# INLINEABLE rule #-}

isArchive :: Text.Text -> Bool
isArchive :: Text -> Bool
isArchive Text
path =
  [Bool] -> Bool
forall (t :: * -> *). Foldable t => t Bool -> Bool
or
    ( [ Text
ftype Text -> Text -> Bool
`Text.isSuffixOf` Text
path
        | Text
ftype <-
            [ Text
".tar",
              Text
".gz",
              Text
".bz2",
              Text
".xz",
              Text
".zip",
              Text
".tgz",
              Text
".tb2",
              Text
".tbz",
              Text
".tbz2",
              Text
".lz",
              Text
".lzma",
              Text
".tlz",
              Text
".txz",
              Text
".Z",
              Text
".tZ"
            ]
      ]
    )

isUrl :: Text.Text -> Bool
isUrl :: Text -> Bool
isUrl Text
path = [Bool] -> Bool
forall (t :: * -> *). Foldable t => t Bool -> Bool
or ([Text
proto Text -> Text -> Bool
`Text.isPrefixOf` Text
path | Text
proto <- [Text
"https://", Text
"http://"]])