module Hadolint.Utils
  ( hasCacheOrTmpfsMount,
    hasCacheOrTmpfsMountWith
  )
where

import qualified Data.Text as Text
import qualified Data.Set as Set
import Language.Docker.Syntax


-- Returns true if the given RunFlags contain a mount of type cache or tmpfs at
-- a location that contains the path fragment `frag`.
hasCacheOrTmpfsMountWith :: Text.Text -> RunFlags -> Bool
hasCacheOrTmpfsMountWith :: Text -> RunFlags -> Bool
hasCacheOrTmpfsMountWith Text
frag RunFlags { Set RunMount
mount :: Set RunMount
mount :: RunFlags -> Set RunMount
mount } =
  Bool -> Bool
not ( Set RunMount -> Bool
forall a. Set a -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null (Set RunMount -> Bool) -> Set RunMount -> Bool
forall a b. (a -> b) -> a -> b
$ (RunMount -> Bool) -> Set RunMount -> Set RunMount
forall a. (a -> Bool) -> Set a -> Set a
Set.filter (Text -> RunMount -> Bool
isCacheOrTmpfsMountWith Text
frag) Set RunMount
mount)

isCacheOrTmpfsMountWith :: Text.Text -> RunMount -> Bool
isCacheOrTmpfsMountWith :: Text -> RunMount -> Bool
isCacheOrTmpfsMountWith Text
frag (CacheMount CacheOpts {cTarget :: CacheOpts -> TargetPath
cTarget = TargetPath {unTargetPath :: TargetPath -> Text
unTargetPath = Text
t}}) = Text
frag Text -> Text -> Bool
`Text.isInfixOf` Text
t
isCacheOrTmpfsMountWith Text
frag (TmpfsMount TmpOpts {tTarget :: TmpOpts -> TargetPath
tTarget = TargetPath {unTargetPath :: TargetPath -> Text
unTargetPath = Text
t}}) = Text
frag Text -> Text -> Bool
`Text.isInfixOf` Text
t
isCacheOrTmpfsMountWith Text
_ RunMount
_ = Bool
False

-- Returns true if the given RunFlags contain a mount of type cache or tmpfs
hasCacheOrTmpfsMount :: RunFlags -> Bool
hasCacheOrTmpfsMount :: RunFlags -> Bool
hasCacheOrTmpfsMount RunFlags { Set RunMount
mount :: RunFlags -> Set RunMount
mount :: Set RunMount
mount } =
  Bool -> Bool
not ( Set RunMount -> Bool
forall a. Set a -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null (Set RunMount -> Bool) -> Set RunMount -> Bool
forall a b. (a -> b) -> a -> b
$ (RunMount -> Bool) -> Set RunMount -> Set RunMount
forall a. (a -> Bool) -> Set a -> Set a
Set.filter RunMount -> Bool
isCacheOrTmpfsMount Set RunMount
mount)

isCacheOrTmpfsMount :: RunMount -> Bool
isCacheOrTmpfsMount :: RunMount -> Bool
isCacheOrTmpfsMount (CacheMount CacheOpts
_) = Bool
True
isCacheOrTmpfsMount (TmpfsMount TmpOpts
_) = Bool
True
isCacheOrTmpfsMount RunMount
_ = Bool
False