Skip to content

Commit

Permalink
Merge pull request #92 from ppettina/add-checksum
Browse files Browse the repository at this point in the history
Add 'ADD --checksum' support
  • Loading branch information
lorenzo authored Apr 24, 2024
2 parents a62ce75 + 325be8a commit 52ca2e5
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 28 deletions.
1 change: 1 addition & 0 deletions integration-tests/parse_files.sh
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ function clone_repos() {
git_clone https://github.com/ArchiveTeam/warrior-dockerfile.git &
git_clone https://github.com/wckr/wocker-dockerfile.git &
git_clone https://github.com/kartoza/docker-postgis.git &
git_clone https://github.com/andrericardo/docker-subtitleedit &

# colliding names
git_named_clone https://github.com/yaronr/dockerfile.git yaronr-dockerfile &
Expand Down
6 changes: 3 additions & 3 deletions language-docker.cabal
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
cabal-version: 1.12

-- This file has been generated from package.yaml by hpack version 0.35.0.
-- This file has been generated from package.yaml by hpack version 0.36.0.
--
-- see: https://github.com/sol/hpack
--
-- hash: 37c989b46891ef750c66b64a1dcbdfc30c1ee22a0ed4821aeb27c7c63c02d2a9
-- hash: 528820a99687ed9372038487df42e487d63b867ed312d39e8e51b6d1bf4cf7d9

name: language-docker
version: 12.1.0
version: 12.2.0
synopsis: Dockerfile parser, pretty-printer and embedded DSL
description: All functions for parsing and pretty-printing Dockerfiles are exported through @Language.Docker@. For more fine-grained operations look for specific modules that implement a certain functionality.
See the <https://github.com/hadolint/language-docker GitHub project> for the source-code and examples.
Expand Down
2 changes: 1 addition & 1 deletion package.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
name: language-docker
version: '12.1.0'
version: '12.2.0'
synopsis: Dockerfile parser, pretty-printer and embedded DSL
description: 'All functions for parsing and pretty-printing Dockerfiles are
exported through @Language.Docker@. For more fine-grained operations look for
Expand Down
31 changes: 22 additions & 9 deletions src/Language/Docker/Parser/Copy.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import Language.Docker.Parser.Prelude
import Language.Docker.Syntax

data Flag
= FlagChown Chown
= FlagChecksum Checksum
| FlagChown Chown
| FlagChmod Chmod
| FlagLink Link
| FlagSource CopySource
Expand Down Expand Up @@ -56,18 +57,23 @@ parseAdd :: (?esc :: Char) => Parser (Instruction Text)
parseAdd = do
reserved "ADD"
flags <- addFlag `sepEndBy` requiredWhitespace
let checksumFlags = [c | FlagChecksum c <- flags]
let chownFlags = [c | FlagChown c <- flags]
let chmodFlags = [c | FlagChmod c <- flags]
let linkFlags = [l | FlagLink l <- flags]
let invalidFlags = [i | FlagInvalid i <- flags]
notFollowedBy (string "--") <?>
"only the --chown flag, the --chmod flag or the src and dest paths"
case (invalidFlags, chownFlags, linkFlags, chmodFlags) of
((k, v) : _, _, _, _) -> unexpectedFlag k v
(_, _ : _ : _, _, _) -> customError $ DuplicateFlagError "--chown"
(_, _, _ : _ : _, _) -> customError $ DuplicateFlagError "--chmod"
(_, _, _, _ : _ : _) -> customError $ DuplicateFlagError "--link"
"only the --checksum, --chown, --chmod, --link flags or the src and dest paths"
case (invalidFlags, checksumFlags, chownFlags, linkFlags, chmodFlags) of
((k, v) : _, _, _, _, _) -> unexpectedFlag k v
(_, _ : _ : _, _, _, _) -> customError $ DuplicateFlagError "--checksum"
(_, _, _ : _ : _, _, _) -> customError $ DuplicateFlagError "--chown"
(_, _, _, _ : _ : _, _) -> customError $ DuplicateFlagError "--chmod"
(_, _, _, _, _ : _ : _) -> customError $ DuplicateFlagError "--link"
_ -> do
let chk = case checksumFlags of
[] -> NoChecksum
c : _ -> c
let cho = case chownFlags of
[] -> NoChown
c : _ -> c
Expand All @@ -78,7 +84,7 @@ parseAdd = do
case linkFlags of
[] -> NoLink
l : _ -> l
fileList "ADD" (\src dest -> Add (AddArgs src dest) (AddFlags cho chm lnk))
fileList "ADD" (\src dest -> Add (AddArgs src dest) (AddFlags chk cho chm lnk))

heredocList :: (?esc :: Char) =>
(NonEmpty SourcePath -> TargetPath -> Instruction Text) ->
Expand Down Expand Up @@ -114,11 +120,18 @@ copyFlag :: (?esc :: Char) => Parser Flag
copyFlag = (FlagSource <$> try copySource <?> "only one --from") <|> addFlag

addFlag :: (?esc :: Char) => Parser Flag
addFlag = (FlagChown <$> try chown <?> "--chown")
addFlag = (FlagChecksum <$> try checksum <?> "--checksum")
<|> (FlagChown <$> try chown <?> "--chown")
<|> (FlagChmod <$> try chmod <?> "--chmod")
<|> (FlagLink <$> try link <?> "--link")
<|> (FlagInvalid <$> try anyFlag <?> "other flag")

checksum :: (?esc :: Char) => Parser Checksum
checksum = do
void $ string "--checksum="
chk <- someUnless "the remote file checksum" (== ' ')
return $ Checksum chk

chown :: (?esc :: Char) => Parser Chown
chown = do
void $ string "--chown="
Expand Down
9 changes: 8 additions & 1 deletion src/Language/Docker/PrettyPrint.hs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ prettyPrintFileList sources (TargetPath dest) =
_ -> ""
in hsep $ [pretty s | SourcePath s <- toList sources] ++ [pretty dest <> ending]

prettyPrintChecksum :: Checksum -> Doc ann
prettyPrintChecksum checksum =
case checksum of
Checksum c -> "--checksum=" <> pretty c
NoChecksum -> mempty

prettyPrintChown :: Chown -> Doc ann
prettyPrintChown chown =
case chown of
Expand Down Expand Up @@ -315,8 +321,9 @@ prettyPrintInstruction i =
prettyPrintBaseImage b
Add
AddArgs {sourcePaths, targetPath}
AddFlags {chownFlag, chmodFlag, linkFlag} -> do
AddFlags {checksumFlag, chownFlag, chmodFlag, linkFlag} -> do
"ADD"
prettyPrintChecksum checksumFlag
prettyPrintChown chownFlag
prettyPrintChmod chmodFlag
prettyPrintLink linkFlag
Expand Down
16 changes: 14 additions & 2 deletions src/Language/Docker/Syntax.hs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,17 @@ newtype TargetPath
}
deriving (Show, Eq, Ord, IsString)

data Checksum
= Checksum !Text
| NoChecksum
deriving (Show, Eq, Ord)

instance IsString Checksum where
fromString ch =
case ch of
"" -> NoChecksum
_ -> Checksum (Text.pack ch)

data Chown
= Chown !Text
| NoChown
Expand Down Expand Up @@ -197,14 +208,15 @@ data AddArgs

data AddFlags
= AddFlags
{ chownFlag :: !Chown,
{ checksumFlag :: !Checksum,
chownFlag :: !Chown,
chmodFlag :: !Chmod,
linkFlag :: !Link
}
deriving (Show, Eq, Ord)

instance Default AddFlags where
def = AddFlags NoChown NoChmod NoLink
def = AddFlags NoChecksum NoChown NoChmod NoLink

data Check args
= Check !(CheckArgs args)
Expand Down
24 changes: 16 additions & 8 deletions test/Language/Docker/ParseAddSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,54 +41,62 @@ spec = do
)
def
]
it "with checksum flag" $
let file = Text.unlines ["ADD --checksum=sha256:24454f830cdd http://www.example.com/foo bar"]
in assertAst
file
[ Add
( AddArgs (fmap SourcePath ["http://www.example.com/foo"]) (TargetPath "bar") )
( AddFlags (Checksum "sha256:24454f830cdd") NoChown NoChmod NoLink )
]
it "with chown flag" $
let file = Text.unlines ["ADD --chown=root:root foo bar"]
in assertAst
file
[ Add
( AddArgs (fmap SourcePath ["foo"]) (TargetPath "bar") )
( AddFlags (Chown "root:root") NoChmod NoLink )
( AddFlags NoChecksum (Chown "root:root") NoChmod NoLink )
]
it "with chmod flag" $
let file = Text.unlines ["ADD --chmod=640 foo bar"]
in assertAst
file
[ Add
( AddArgs (fmap SourcePath ["foo"]) (TargetPath "bar") )
( AddFlags NoChown (Chmod "640") NoLink )
( AddFlags NoChecksum NoChown (Chmod "640") NoLink )
]
it "with link flag" $
let file = Text.unlines ["ADD --link foo bar"]
in assertAst
file
[ Add
( AddArgs (fmap SourcePath ["foo"]) (TargetPath "bar") )
( AddFlags NoChown NoChmod Link )
( AddFlags NoChecksum NoChown NoChmod Link )
]
it "with chown and chmod flag" $
let file = Text.unlines ["ADD --chown=root:root --chmod=640 foo bar"]
in assertAst
file
[ Add
( AddArgs (fmap SourcePath ["foo"]) (TargetPath "bar") )
( AddFlags (Chown "root:root") (Chmod "640") NoLink )
( AddFlags NoChecksum (Chown "root:root") (Chmod "640") NoLink )
]
it "with chown and chmod flag other order" $
let file = Text.unlines ["ADD --chmod=640 --chown=root:root foo bar"]
in assertAst
file
[ Add
( AddArgs (fmap SourcePath ["foo"]) (TargetPath "bar") )
( AddFlags (Chown "root:root") (Chmod "640") NoLink )
( AddFlags NoChecksum (Chown "root:root") (Chmod "640") NoLink )
]
it "with all flags" $
let file =
Text.unlines ["ADD --chmod=640 --chown=root:root --link foo bar"]
Text.unlines ["ADD --chmod=640 --chown=root:root --checksum=sha256:24454f830cdd --link foo bar"]
in assertAst
file
[ Add
( AddArgs (fmap SourcePath ["foo"]) (TargetPath "bar") )
( AddFlags (Chown "root:root") (Chmod "640") Link )
( AddFlags (Checksum "sha256:24454f830cdd") (Chown "root:root") (Chmod "640") Link )
]
it "list of quoted files and chown" $
let file =
Expand All @@ -101,5 +109,5 @@ spec = do
(fmap SourcePath ["foo", "bar", "baz"])
(TargetPath "/app")
)
( AddFlags (Chown "user:group") NoChmod NoLink )
( AddFlags NoChecksum (Chown "user:group") NoChmod NoLink )
]
13 changes: 9 additions & 4 deletions test/Language/Docker/PrettyPrintSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,30 @@ spec = do
( AddArgs [SourcePath "foo"] (TargetPath "bar") )
( def :: AddFlags )
in assertPretty "ADD foo bar" add
it "with just checksum" $ do
let add = Add
( AddArgs [SourcePath "http://www.example.com/foo"] (TargetPath "bar") )
( AddFlags ( Checksum "sha256:24454f830cdd" ) NoChown NoChmod NoLink )
in assertPretty "ADD --checksum=sha256:24454f830cdd http://www.example.com/foo bar" add
it "with just chown" $ do
let add = Add
( AddArgs [SourcePath "foo"] (TargetPath "bar") )
( AddFlags ( Chown "root:root" ) NoChmod NoLink )
( AddFlags NoChecksum ( Chown "root:root" ) NoChmod NoLink )
in assertPretty "ADD --chown=root:root foo bar" add
it "with just chmod" $ do
let add = Add
( AddArgs [SourcePath "foo"] (TargetPath "bar") )
( AddFlags NoChown ( Chmod "751" ) NoLink )
( AddFlags NoChecksum NoChown ( Chmod "751" ) NoLink )
in assertPretty "ADD --chmod=751 foo bar" add
it "with just link" $ do
let add = Add
( AddArgs [SourcePath "foo"] (TargetPath "bar") )
( AddFlags NoChown NoChmod Link )
( AddFlags NoChecksum NoChown NoChmod Link )
in assertPretty "ADD --link foo bar" add
it "with chown, chmod and link" $ do
let add = Add
( AddArgs [SourcePath "foo"] (TargetPath "bar") )
( AddFlags ( Chown "root:root" ) ( Chmod "751" ) Link )
( AddFlags NoChecksum ( Chown "root:root" ) ( Chmod "751" ) Link )
in assertPretty "ADD --chown=root:root --chmod=751 --link foo bar" add

describe "pretty print COPY" $ do
Expand Down

0 comments on commit 52ca2e5

Please sign in to comment.