Skip to content

Commit e463bee

Browse files
authored
Merge pull request #110 from m-ildefons/wip/tmpfs-size-flag
Tmpfs mount: Size option
2 parents f999974 + 85e78c7 commit e463bee

5 files changed

Lines changed: 50 additions & 4 deletions

File tree

src/Language/Docker/Parser/Run.hs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ data RunMountArg
2626
| MountArgReadOnly Bool
2727
| MountArgRequired Bool
2828
| MountArgSharing CacheSharing
29+
| MountArgSize Text
2930
| MountArgSource SourcePath
3031
| MountArgTarget TargetPath
3132
| MountArgType MountType
@@ -147,13 +148,15 @@ cacheMount args =
147148

148149
tmpfsMount :: [RunMountArg] -> Parser TmpOpts
149150
tmpfsMount args =
150-
case validArgs "tmpfs" required required args of
151+
case validArgs "tmpfs" allowed required args of
151152
Left e -> customError e
152153
Right as -> return $ foldr tmpOpts def as
153154
where
155+
allowed = Set.fromList [ "target", "size" ]
154156
required = Set.singleton "target"
155157
tmpOpts :: RunMountArg -> TmpOpts -> TmpOpts
156158
tmpOpts (MountArgTarget path) t = t {tTarget = path}
159+
tmpOpts (MountArgSize size) t = t {tSize = Just size}
157160
tmpOpts invalid _ = error $ "unhandled " <> show invalid <> " please report this bug"
158161

159162
secretMount :: [RunMountArg] -> Parser SecretOpts
@@ -209,6 +212,7 @@ mountArgs =
209212
mountArgRelabel,
210213
mountArgRequired,
211214
mountArgSharing,
215+
mountArgSize,
212216
mountArgSource,
213217
mountArgTarget,
214218
mountArgType,
@@ -295,6 +299,9 @@ mountArgRequired = MountArgRequired <$> choice
295299
mountArgSharing :: Parser RunMountArg
296300
mountArgSharing = MountArgSharing <$> key "sharing" cacheSharing
297301

302+
mountArgSize :: (?esc :: Char) => Parser RunMountArg
303+
mountArgSize = MountArgSize <$> key "size" stringArg
304+
298305
mountArgSource :: (?esc :: Char) => Parser RunMountArg
299306
mountArgSource = do
300307
label "source=" $ choice [string "source=", string "src="]
@@ -342,6 +349,7 @@ toArgName (MountArgMode _) = "mode"
342349
toArgName (MountArgReadOnly _) = "ro"
343350
toArgName (MountArgRequired _) = "required"
344351
toArgName (MountArgSharing _) = "sharing"
352+
toArgName (MountArgSize _) = "size"
345353
toArgName (MountArgSource _) = "source"
346354
toArgName (MountArgTarget _) = "target"
347355
toArgName (MountArgType _) = "type"

src/Language/Docker/PrettyPrint.hs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,10 @@ prettyPrintRunMount set =
212212
<> maybe mempty printUid sUid
213213
<> maybe mempty printGid sGid
214214
<> maybe mempty printRequired sIsRequired
215-
TmpfsMount TmpOpts {..} -> "type=tmpfs" <> printTarget tTarget
215+
TmpfsMount TmpOpts {..} ->
216+
"type=tmpfs"
217+
<> printTarget tTarget
218+
<> maybe mempty printSize tSize
216219
printQuotable str
217220
| Text.any (== '"') str = doubleQoute str
218221
| otherwise = pretty str
@@ -236,6 +239,7 @@ prettyPrintRunMount set =
236239
<> case r of
237240
RelabelShared -> printQuotable "shared"
238241
RelabelPrivate -> printQuotable "private"
242+
printSize s = ",size=" <> pretty s
239243

240244
prettyPrintRunNetwork :: Maybe RunNetwork -> Doc ann
241245
prettyPrintRunNetwork Nothing = mempty

src/Language/Docker/Syntax.hs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,10 +339,15 @@ data CacheOpts
339339
instance Default CacheOpts where
340340
def = CacheOpts "" Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing
341341

342-
newtype TmpOpts = TmpOpts {tTarget :: TargetPath} deriving (Eq, Show, Ord)
342+
data TmpOpts
343+
= TmpOpts
344+
{ tTarget :: TargetPath,
345+
tSize :: !(Maybe Text)
346+
}
347+
deriving (Eq, Show, Ord)
343348

344349
instance Default TmpOpts where
345-
def = TmpOpts ""
350+
def = TmpOpts "" Nothing
346351

347352
data SecretOpts
348353
= SecretOpts

test/Language/Docker/ParseRunSpec.hs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,11 @@ spec = do
168168
file
169169
[ Run $ RunArgs (ArgumentsText "echo foo") flags
170170
]
171+
it "--mount=type=tmpfs,size=12G" $
172+
let file = Text.unlines ["RUN --mount=type=tmpfs,target=/foo,size=12G foo bar"]
173+
flags = def { mount = Set.singleton $ TmpfsMount (def {tTarget = "/foo", tSize = Just "12G"}) }
174+
in assertAst file [ Run $ RunArgs ( ArgumentsText "foo bar" ) flags ]
175+
171176
it "--mount=type=ssh" $
172177
let file = Text.unlines ["RUN --mount=type=ssh echo foo"]
173178
flags = def {mount = Set.singleton $ SshMount def}

test/Language/Docker/PrettyPrintSpec.hs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
module Language.Docker.PrettyPrintSpec where
55

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

182+
describe "pretty print RUN" $ do
183+
it "just a simple RUN" $ do
184+
let run = Run ( RunArgs ( ArgumentsText "foobar" ) def )
185+
in assertPretty "RUN foobar" run
186+
187+
it "RUN in JSON format" $ do
188+
let run = Run ( RunArgs ( ArgumentsList "foobar barfoo" ) def )
189+
in assertPretty "RUN [\"foobar\", \"barfoo\"]" run
190+
191+
it "RUN with --mount=type=tmpfs" $ do
192+
let run =
193+
Run
194+
( RunArgs
195+
( ArgumentsText "foobar" )
196+
( RunFlags
197+
{ mount = Set.singleton ( TmpfsMount ( TmpOpts "/tgt" (Just "4G") ) ),
198+
security = Nothing,
199+
network = Nothing
200+
}
201+
)
202+
)
203+
in assertPretty "RUN --mount=type=tmpfs,target=/tgt,size=4G foobar" run
204+
181205
describe "pretty print # escape" $ do
182206
it "# escape = \\" $ do
183207
let esc = Pragma (Escape (EscapeChar '\\'))

0 commit comments

Comments
 (0)