module Hadolint.Rule.DL3062 (rule) where
import Data.Text qualified as Text
import Hadolint.Rule
import Hadolint.Shell qualified as Shell
import Language.Docker.Syntax
rule :: Rule Shell.ParsedShell
rule :: Rule ParsedShell
rule = Rule ParsedShell
dl3062 Rule ParsedShell -> Rule ParsedShell -> Rule ParsedShell
forall a. Semigroup a => a -> a -> a
<> Rule ParsedShell -> Rule ParsedShell
forall args. Rule args -> Rule args
onbuild Rule ParsedShell
dl3062
{-# INLINEABLE rule #-}
dl3062 :: Rule Shell.ParsedShell
dl3062 :: Rule ParsedShell
dl3062 = RuleCode
-> DLSeverity
-> Text
-> (Instruction ParsedShell -> Bool)
-> Rule ParsedShell
forall args.
RuleCode
-> DLSeverity -> Text -> (Instruction args -> Bool) -> Rule args
simpleRule RuleCode
code DLSeverity
severity Text
message Instruction ParsedShell -> Bool
check
where
code :: RuleCode
code = RuleCode
"DL3062"
severity :: DLSeverity
severity = DLSeverity
DLWarningC
message :: Text
message = Text
"Pin versions in go. Instead of `go install <package>` use `go install <package>@<version>`"
check :: Instruction ParsedShell -> Bool
check (Run (RunArgs Arguments ParsedShell
args RunFlags
_)) = (ParsedShell -> Bool) -> Arguments ParsedShell -> Bool
forall a b. (a -> b) -> Arguments a -> b
foldArguments ((Text -> Bool) -> [Text] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Text -> Bool
checkGoPackageVersion ([Text] -> Bool) -> (ParsedShell -> [Text]) -> ParsedShell -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ParsedShell -> [Text]
goPackageCommand) Arguments ParsedShell
args
check Instruction ParsedShell
_ = Bool
True
{-# INLINEABLE dl3062 #-}
goPackageCommand :: Shell.ParsedShell -> [Text.Text]
goPackageCommand :: ParsedShell -> [Text]
goPackageCommand ParsedShell
args
| Bool -> Bool
not (Bool -> Bool) -> ([Text] -> Bool) -> [Text] -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Text] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null ([Text] -> Bool) -> [Text] -> Bool
forall a b. (a -> b) -> a -> b
$ ParsedShell -> [Text]
runGoPackage ParsedShell
args = ParsedShell -> [Text]
runGoPackage ParsedShell
args
| Bool -> Bool
not (Bool -> Bool) -> ([Text] -> Bool) -> [Text] -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Text] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null ([Text] -> Bool) -> [Text] -> Bool
forall a b. (a -> b) -> a -> b
$ ParsedShell -> [Text]
getGoPackage ParsedShell
args = ParsedShell -> [Text]
getGoPackage ParsedShell
args
| Bool
otherwise = []
runGoPackage :: Shell.ParsedShell -> [Text.Text]
runGoPackage :: ParsedShell -> [Text]
runGoPackage ParsedShell
args =
[ Text
arg
| Command
cmd <- ParsedShell -> [Command]
Shell.presentCommands ParsedShell
args,
[Text] -> [Text] -> Command -> Bool
Shell.cmdsHaveArgs [Text
"go"] [Text
"run"] Command
cmd,
(Integer
idx, Text
arg) <- [Text] -> [(Integer, Text)]
enum ([Text] -> [(Integer, Text)]) -> [Text] -> [(Integer, Text)]
forall a b. (a -> b) -> a -> b
$ Command -> [Text]
Shell.getArgsNoFlags Command
cmd,
Text
arg Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
/= Text
"run" Bool -> Bool -> Bool
&& Integer
idx Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
<= Integer
1
]
where
enum :: [Text.Text] -> [(Integer, Text.Text)]
enum :: [Text] -> [(Integer, Text)]
enum = [Integer] -> [Text] -> [(Integer, Text)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Integer
0..]
getGoPackage :: Shell.ParsedShell -> [Text.Text]
getGoPackage :: ParsedShell -> [Text]
getGoPackage ParsedShell
args =
[ Text
arg
| Command
cmd <- ParsedShell -> [Command]
Shell.presentCommands ParsedShell
args,
[Text] -> [Text] -> Command -> Bool
Shell.cmdsHaveArgs [Text
"go"] [Text
"install", Text
"get"] Command
cmd,
Text
arg <- Command -> [Text]
Shell.getArgsNoFlags Command
cmd,
Text
arg Text -> [Text] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` [Text
"install", Text
"get", Text
"tool"]
]
hasVersionSymbol :: Text.Text -> Bool
hasVersionSymbol :: Text -> Bool
hasVersionSymbol Text
package = Text
"@" Text -> Text -> Bool
`Text.isInfixOf` Text
package
isTagsVersion :: Text.Text -> Bool
isTagsVersion :: Text -> Bool
isTagsVersion Text
package =
[Bool] -> Bool
forall (t :: * -> *). Foldable t => t Bool -> Bool
or [(Text
"@" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
tag) Text -> Text -> Bool
`Text.isSuffixOf` Text
package | Text
tag <- [Text
"latest", Text
"none"]]
isLocalPath :: Text.Text -> Bool
isLocalPath :: Text -> Bool
isLocalPath Text
p = Text
p Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"."
Bool -> Bool -> Bool
|| Text
"/" Text -> Text -> Bool
`Text.isPrefixOf` Text
p
Bool -> Bool -> Bool
|| Text
"." Text -> Text -> Bool
`Text.isPrefixOf` Text
p
checkGoPackageVersion :: Text.Text -> Bool
checkGoPackageVersion :: Text -> Bool
checkGoPackageVersion Text
package =
Text -> Bool
isLocalPath Text
package
Bool -> Bool -> Bool
|| Text -> Bool
hasVersionSymbol Text
package
Bool -> Bool -> Bool
&& Bool -> Bool
not (Text -> Bool
isTagsVersion Text
package)