Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/Language/Docker/Parser/Run.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ data RunMountArg
| MountArgReadOnly Bool
| MountArgRequired Bool
| MountArgSharing CacheSharing
| MountArgSize Text
| MountArgSource SourcePath
| MountArgTarget TargetPath
| MountArgType MountType
Expand Down Expand Up @@ -147,13 +148,15 @@ cacheMount args =

tmpfsMount :: [RunMountArg] -> Parser TmpOpts
tmpfsMount args =
case validArgs "tmpfs" required required args of
case validArgs "tmpfs" allowed required args of
Left e -> customError e
Right as -> return $ foldr tmpOpts def as
where
allowed = Set.fromList [ "target", "size" ]
required = Set.singleton "target"
tmpOpts :: RunMountArg -> TmpOpts -> TmpOpts
tmpOpts (MountArgTarget path) t = t {tTarget = path}
tmpOpts (MountArgSize size) t = t {tSize = Just size}
tmpOpts invalid _ = error $ "unhandled " <> show invalid <> " please report this bug"

secretMount :: [RunMountArg] -> Parser SecretOpts
Expand Down Expand Up @@ -209,6 +212,7 @@ mountArgs =
mountArgRelabel,
mountArgRequired,
mountArgSharing,
mountArgSize,
mountArgSource,
mountArgTarget,
mountArgType,
Expand Down Expand Up @@ -295,6 +299,9 @@ mountArgRequired = MountArgRequired <$> choice
mountArgSharing :: Parser RunMountArg
mountArgSharing = MountArgSharing <$> key "sharing" cacheSharing

mountArgSize :: (?esc :: Char) => Parser RunMountArg
mountArgSize = MountArgSize <$> key "size" stringArg

mountArgSource :: (?esc :: Char) => Parser RunMountArg
mountArgSource = do
label "source=" $ choice [string "source=", string "src="]
Expand Down Expand Up @@ -342,6 +349,7 @@ toArgName (MountArgMode _) = "mode"
toArgName (MountArgReadOnly _) = "ro"
toArgName (MountArgRequired _) = "required"
toArgName (MountArgSharing _) = "sharing"
toArgName (MountArgSize _) = "size"
toArgName (MountArgSource _) = "source"
toArgName (MountArgTarget _) = "target"
toArgName (MountArgType _) = "type"
Expand Down
6 changes: 5 additions & 1 deletion src/Language/Docker/PrettyPrint.hs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,10 @@
<> maybe mempty printUid sUid
<> maybe mempty printGid sGid
<> maybe mempty printRequired sIsRequired
TmpfsMount TmpOpts {..} -> "type=tmpfs" <> printTarget tTarget
TmpfsMount TmpOpts {..} ->
"type=tmpfs"
<> printTarget tTarget
<> maybe mempty printSize tSize
printQuotable str
| Text.any (== '"') str = doubleQoute str
| otherwise = pretty str
Expand All @@ -236,6 +239,7 @@
<> case r of
RelabelShared -> printQuotable "shared"
RelabelPrivate -> printQuotable "private"
printSize s = ",size=" <> pretty s

prettyPrintRunNetwork :: Maybe RunNetwork -> Doc ann
prettyPrintRunNetwork Nothing = mempty
Expand Down Expand Up @@ -289,7 +293,7 @@
CopyArgs {sourcePaths, targetPath}
CopyFlags {chmodFlag, chownFlag, linkFlag, parentsFlag, sourceFlag, excludeFlags} -> do
"COPY"
prettyPrintChown chownFlag

Check warning on line 296 in src/Language/Docker/PrettyPrint.hs

View workflow job for this annotation

GitHub Actions / hlint

Suggestion in prettyPrintInstruction in module Language.Docker.PrettyPrint: Reduce duplication ▫︎ Found: "prettyPrintChown chownFlag\nprettyPrintChmod chmodFlag\npretty linkFlag\n" ▫︎ Perhaps: "Combine with src/Language/Docker/PrettyPrint.hs:332:9-34"
prettyPrintChmod chmodFlag
pretty linkFlag
pretty parentsFlag
Expand Down
9 changes: 7 additions & 2 deletions src/Language/Docker/Syntax.hs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@
if "/" `isInfixOf` img
then
let parts = endBy "/" img
in if "." `isInfixOf` head parts

Check warning on line 45 in src/Language/Docker/Syntax.hs

View workflow job for this annotation

GitHub Actions / hadolint (ubuntu-latest)

In the use of ‘head’
then
Image
(Just (Registry (Text.pack (head parts))))

Check warning on line 48 in src/Language/Docker/Syntax.hs

View workflow job for this annotation

GitHub Actions / hadolint (ubuntu-latest)

In the use of ‘head’
(Text.pack . intercalate "/" $ tail parts)

Check warning on line 49 in src/Language/Docker/Syntax.hs

View workflow job for this annotation

GitHub Actions / hadolint (ubuntu-latest)

In the use of ‘tail’
else Image Nothing (Text.pack img)
else Image Nothing (Text.pack img)

Expand Down Expand Up @@ -339,10 +339,15 @@
instance Default CacheOpts where
def = CacheOpts "" Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing

newtype TmpOpts = TmpOpts {tTarget :: TargetPath} deriving (Eq, Show, Ord)
data TmpOpts
= TmpOpts
{ tTarget :: TargetPath,
tSize :: !(Maybe Text)
}
deriving (Eq, Show, Ord)

instance Default TmpOpts where
def = TmpOpts ""
def = TmpOpts "" Nothing

data SecretOpts
= SecretOpts
Expand Down
5 changes: 5 additions & 0 deletions test/Language/Docker/ParseRunSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,11 @@ spec = do
file
[ Run $ RunArgs (ArgumentsText "echo foo") flags
]
it "--mount=type=tmpfs,size=12G" $
let file = Text.unlines ["RUN --mount=type=tmpfs,target=/foo,size=12G foo bar"]
flags = def { mount = Set.singleton $ TmpfsMount (def {tTarget = "/foo", tSize = Just "12G"}) }
in assertAst file [ Run $ RunArgs ( ArgumentsText "foo bar" ) flags ]

it "--mount=type=ssh" $
let file = Text.unlines ["RUN --mount=type=ssh echo foo"]
flags = def {mount = Set.singleton $ SshMount def}
Expand Down
24 changes: 24 additions & 0 deletions test/Language/Docker/PrettyPrintSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
module Language.Docker.PrettyPrintSpec where

import Data.Default
import qualified Data.Set as Set
import qualified Data.Text as Text
import Prettyprinter
import Prettyprinter.Render.Text
Expand Down Expand Up @@ -178,6 +179,29 @@ spec = do
( CopyFlags (Chown "root:root") NoChmod NoLink NoParents NoSource [Exclude "*.tmp"] )
in assertPretty "COPY --chown=root:root --exclude=*.tmp foo bar" copy

describe "pretty print RUN" $ do
it "just a simple RUN" $ do
let run = Run ( RunArgs ( ArgumentsText "foobar" ) def )
in assertPretty "RUN foobar" run

it "RUN in JSON format" $ do
let run = Run ( RunArgs ( ArgumentsList "foobar barfoo" ) def )
in assertPretty "RUN [\"foobar\", \"barfoo\"]" run

it "RUN with --mount=type=tmpfs" $ do
let run =
Run
( RunArgs
( ArgumentsText "foobar" )
( RunFlags
{ mount = Set.singleton ( TmpfsMount ( TmpOpts "/tgt" (Just "4G") ) ),
security = Nothing,
network = Nothing
}
)
)
in assertPretty "RUN --mount=type=tmpfs,target=/tgt,size=4G foobar" run

describe "pretty print # escape" $ do
it "# escape = \\" $ do
let esc = Pragma (Escape (EscapeChar '\\'))
Expand Down
Loading