{-# LANGUAGE OverloadedStrings #-}
module Docker.API.Client.Images
( updateImageName,
updateImageDigest,
updateImageTag,
tagImageIfMissing,
)
where
import Data.Maybe
import qualified Data.Text as T
type ImageParts = (T.Text, Maybe T.Text, Maybe T.Text)
parseImage :: T.Text -> ImageParts
parseImage :: Text -> ImageParts
parseImage Text
img =
let
(Text
nameTag, Text
digest) = HasCallStack => Text -> Text -> (Text, Text)
Text -> Text -> (Text, Text)
T.breakOn Text
"@" Text
img
(Text
name, Text
tag) = HasCallStack => Text -> Text -> (Text, Text)
Text -> Text -> (Text, Text)
T.breakOn Text
":" Text
nameTag
formatOptional :: Text -> Maybe Text
formatOptional Text
part = if Text -> Bool
T.null Text
part then Maybe Text
forall a. Maybe a
Nothing else Text -> Maybe Text
forall a. a -> Maybe a
Just (Text -> Maybe Text) -> Text -> Maybe Text
forall a b. (a -> b) -> a -> b
$ HasCallStack => Text -> Text
Text -> Text
T.tail Text
part
in (Text
name, Text -> Maybe Text
formatOptional Text
tag, Text -> Maybe Text
formatOptional Text
digest)
maybeWithSymbol :: T.Text -> (Maybe T.Text -> T.Text)
maybeWithSymbol :: Text -> Maybe Text -> Text
maybeWithSymbol Text
symbol = Text -> (Text -> Text) -> Maybe Text -> Text
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Text
"" (Text
symbol Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>)
partsToImage :: ImageParts -> T.Text
partsToImage :: ImageParts -> Text
partsToImage (Text
name, Maybe Text
tag, Maybe Text
digest) =
Text
name Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> Maybe Text -> Text
maybeWithSymbol Text
":" Maybe Text
tag Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> Maybe Text -> Text
maybeWithSymbol Text
"@" Maybe Text
digest
tagImageIfMissing :: T.Text -> T.Text
tagImageIfMissing :: Text -> Text
tagImageIfMissing Text
img =
let (Text
name, Maybe Text
tag, Maybe Text
digest) = Text -> ImageParts
parseImage Text
img
in if Maybe Text -> Bool
forall a. Maybe a -> Bool
isNothing Maybe Text
tag Bool -> Bool -> Bool
&& Maybe Text -> Bool
forall a. Maybe a -> Bool
isNothing Maybe Text
digest
then ImageParts -> Text
partsToImage (Text
name, Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"latest", Maybe Text
digest)
else Text
img
updateImageName :: T.Text -> T.Text -> T.Text
updateImageName :: Text -> Text -> Text
updateImageName Text
img Text
name =
let (Text
_, Maybe Text
tag, Maybe Text
digest) = Text -> ImageParts
parseImage Text
img
in ImageParts -> Text
partsToImage (Text
name, Maybe Text
tag, Maybe Text
digest)
updateImageTag :: T.Text -> Maybe T.Text -> T.Text
updateImageTag :: Text -> Maybe Text -> Text
updateImageTag Text
img Maybe Text
tag =
let (Text
name, Maybe Text
_, Maybe Text
digest) = Text -> ImageParts
parseImage Text
img
in ImageParts -> Text
partsToImage (Text
name, Maybe Text
tag, Maybe Text
digest)
updateImageDigest :: T.Text -> Maybe T.Text -> T.Text
updateImageDigest :: Text -> Maybe Text -> Text
updateImageDigest Text
img Maybe Text
digest =
let (Text
name, Maybe Text
tag, Maybe Text
_) = Text -> ImageParts
parseImage Text
img
in ImageParts -> Text
partsToImage (Text
name, Maybe Text
tag, Maybe Text
digest)