Skip to content

Commit 8459193

Browse files
committed
chore: introduce newtypes for volume/container/path
1 parent ecbf556 commit 8459193

File tree

2 files changed

+35
-22
lines changed

2 files changed

+35
-22
lines changed

src/Restyler/CodeVolume.hs

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,42 +7,56 @@
77
-- Stability : experimental
88
-- Portability : POSIX
99
module Restyler.CodeVolume
10-
( withCodeVolume
10+
( VolumeName (..)
11+
, withCodeVolume
1112
) where
1213

1314
import Restyler.Prelude
1415

1516
import Restyler.Monad.Docker
1617
import UnliftIO.Exception (bracket)
1718

18-
withCodeVolume :: (MonadDocker m, MonadUnliftIO m) => (String -> m a) -> m a
19-
withCodeVolume = bracket acquire dockerVolumeRm
19+
newtype VolumeName = VolumeName
20+
{ unwrap :: String
21+
}
22+
23+
withCodeVolume :: (MonadDocker m, MonadUnliftIO m) => (VolumeName -> m a) -> m a
24+
withCodeVolume = bracket acquire (dockerVolumeRm . (.unwrap))
2025
where
21-
acquire :: (MonadDocker m, MonadUnliftIO m) => m String
26+
acquire :: (MonadDocker m, MonadUnliftIO m) => m VolumeName
2227
acquire = withVolumeInContainer tmpVolumeName $ \cName cPath -> do
23-
tmpVolumeName <$ dockerCp "." (cName <> ":" <> cPath)
28+
tmpVolumeName <$ dockerCp "." (cName.unwrap <> ":" <> cPath.unwrap)
29+
30+
newtype ContainerName = ContainerName
31+
{ unwrap :: String
32+
}
33+
34+
newtype ContainerPath = ContainerPath
35+
{ unwrap :: FilePath
36+
}
2437

2538
withVolumeInContainer
2639
:: (MonadDocker m, MonadUnliftIO m)
27-
=> String
28-
-> (String -> FilePath -> m a)
40+
=> VolumeName
41+
-> (ContainerName -> ContainerPath -> m a)
2942
-> m a
30-
withVolumeInContainer name = bracket acquire (dockerRm . fst) . uncurry
43+
withVolumeInContainer name =
44+
bracket acquire (dockerRm . (.unwrap) . fst) . uncurry
3145
where
32-
acquire :: MonadDocker m => m (String, FilePath)
46+
acquire :: MonadDocker m => m (ContainerName, ContainerPath)
3347
acquire = do
3448
dockerCreate
35-
$ ["--name", tmpContainerName]
36-
<> ["--volume", name <> ":" <> tmpContainerPath]
49+
$ ["--name", tmpContainerName.unwrap]
50+
<> ["--volume", name.unwrap <> ":" <> tmpContainerPath.unwrap]
3751
<> ["busybox"]
3852

3953
pure (tmpContainerName, tmpContainerPath)
4054

41-
tmpVolumeName :: String
42-
tmpVolumeName = "restyler-code-volume"
55+
tmpVolumeName :: VolumeName
56+
tmpVolumeName = VolumeName "restyler-code-volume"
4357

44-
tmpContainerName :: String
45-
tmpContainerName = "restyler-tmp-container"
58+
tmpContainerName :: ContainerName
59+
tmpContainerName = ContainerName "restyler-tmp-container"
4660

47-
tmpContainerPath :: FilePath
48-
tmpContainerPath = "/data"
61+
tmpContainerPath :: ContainerPath
62+
tmpContainerPath = ContainerPath "/data"

src/Restyler/Restyler/Run.hs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,7 @@ runRestyler
247247
, MonadUnliftIO m
248248
, MonadWriteFile m
249249
)
250-
=> String
251-
-- ^ Code volume name
250+
=> VolumeName
252251
-> Restyler
253252
-> [FilePath]
254253
-> m (Maybe RestylerResult)
@@ -279,7 +278,7 @@ runRestyler_
279278
, MonadUnliftIO m
280279
, MonadWriteFile m
281280
)
282-
=> String
281+
=> VolumeName
283282
-> Restyler
284283
-> [FilePath]
285284
-> m ()
@@ -367,7 +366,7 @@ dockerRunRestyler
367366
, MonadUnliftIO m
368367
, MonadWriteFile m
369368
)
370-
=> String
369+
=> VolumeName
371370
-> Restyler
372371
-> WithProgress DockerRunStyle
373372
-> m ()
@@ -382,7 +381,7 @@ dockerRunRestyler vol r@Restyler {..} WithProgress {..} = do
382381
restrictionOptions restrictions
383382
<> ["--name", cName]
384383
<> ["--pull", "never"]
385-
<> ["--volume", vol <> ":/code", rImage]
384+
<> ["--volume", vol.unwrap <> ":/code", rImage]
386385
<> nub (rCommand <> rArguments)
387386

388387
copyRestyledPaths = traverse_ $ \path ->

0 commit comments

Comments
 (0)