module Language.Docker.Parser.Copy
( parseCopy,
parseAdd,
)
where
import Data.List.NonEmpty (NonEmpty, fromList)
import qualified Data.Text as T
import Language.Docker.Parser.Prelude
import Language.Docker.Syntax
data CopyFlag
= FlagChown Chown
| FlagChmod Chmod
| FlagSource CopySource
| FlagInvalid (Text, Text)
parseCopy :: (?esc :: Char) => Parser (Instruction Text)
parseCopy :: Parser (Instruction Text)
parseCopy = do
(?esc::Char) => Text -> Parser ()
Text -> Parser ()
reserved Text
"COPY"
[CopyFlag]
flags <- Parser CopyFlag
(?esc::Char) => Parser CopyFlag
copyFlag Parser CopyFlag
-> Parser () -> ParsecT DockerfileError Text Identity [CopyFlag]
forall (m :: * -> *) a sep. MonadPlus m => m a -> m sep -> m [a]
`sepEndBy` Parser ()
(?esc::Char) => Parser ()
requiredWhitespace
let chownFlags :: [Chown]
chownFlags = [Chown
c | FlagChown Chown
c <- [CopyFlag]
flags]
let chmodFlags :: [Chmod]
chmodFlags = [Chmod
c | FlagChmod Chmod
c <- [CopyFlag]
flags]
let sourceFlags :: [CopySource]
sourceFlags = [CopySource
f | FlagSource CopySource
f <- [CopyFlag]
flags]
let invalid :: [(Text, Text)]
invalid = [(Text, Text)
i | FlagInvalid (Text, Text)
i <- [CopyFlag]
flags]
case ([(Text, Text)]
invalid, [Chown]
chownFlags, [Chmod]
chmodFlags, [CopySource]
sourceFlags) of
((Text
k, Text
v) : [(Text, Text)]
_, [Chown]
_, [Chmod]
_, [CopySource]
_) -> Text -> Text -> Parser (Instruction Text)
forall a. Text -> Text -> Parser a
unexpectedFlag Text
k Text
v
([(Text, Text)]
_, Chown
_ : Chown
_ : [Chown]
_, [Chmod]
_, [CopySource]
_) -> DockerfileError -> Parser (Instruction Text)
forall a. DockerfileError -> Parser a
customError (DockerfileError -> Parser (Instruction Text))
-> DockerfileError -> Parser (Instruction Text)
forall a b. (a -> b) -> a -> b
$ String -> DockerfileError
DuplicateFlagError String
"--chown"
([(Text, Text)]
_, [Chown]
_, Chmod
_ : Chmod
_ : [Chmod]
_, [CopySource]
_) -> DockerfileError -> Parser (Instruction Text)
forall a. DockerfileError -> Parser a
customError (DockerfileError -> Parser (Instruction Text))
-> DockerfileError -> Parser (Instruction Text)
forall a b. (a -> b) -> a -> b
$ String -> DockerfileError
DuplicateFlagError String
"--chmod"
([(Text, Text)]
_, [Chown]
_, [Chmod]
_, CopySource
_ : CopySource
_ : [CopySource]
_) -> DockerfileError -> Parser (Instruction Text)
forall a. DockerfileError -> Parser a
customError (DockerfileError -> Parser (Instruction Text))
-> DockerfileError -> Parser (Instruction Text)
forall a b. (a -> b) -> a -> b
$ String -> DockerfileError
DuplicateFlagError String
"--from"
([(Text, Text)], [Chown], [Chmod], [CopySource])
_ -> do
let cho :: Chown
cho =
case [Chown]
chownFlags of
[] -> Chown
NoChown
Chown
c : [Chown]
_ -> Chown
c
let chm :: Chmod
chm =
case [Chmod]
chmodFlags of
[] -> Chmod
NoChmod
Chmod
c : [Chmod]
_ -> Chmod
c
let fr :: CopySource
fr =
case [CopySource]
sourceFlags of
[] -> CopySource
NoSource
CopySource
f : [CopySource]
_ -> CopySource
f
(?esc::Char) =>
Text
-> (NonEmpty SourcePath -> TargetPath -> Instruction Text)
-> Parser (Instruction Text)
Text
-> (NonEmpty SourcePath -> TargetPath -> Instruction Text)
-> Parser (Instruction Text)
fileList Text
"COPY" (\NonEmpty SourcePath
src TargetPath
dest -> CopyArgs -> Instruction Text
forall args. CopyArgs -> Instruction args
Copy (NonEmpty SourcePath
-> TargetPath -> Chown -> Chmod -> CopySource -> CopyArgs
CopyArgs NonEmpty SourcePath
src TargetPath
dest Chown
cho Chmod
chm CopySource
fr))
parseAdd :: (?esc :: Char) => Parser (Instruction Text)
parseAdd :: Parser (Instruction Text)
parseAdd = do
(?esc::Char) => Text -> Parser ()
Text -> Parser ()
reserved Text
"ADD"
[CopyFlag]
flags <- Parser CopyFlag
(?esc::Char) => Parser CopyFlag
copyFlag Parser CopyFlag
-> Parser () -> ParsecT DockerfileError Text Identity [CopyFlag]
forall (m :: * -> *) a sep. MonadPlus m => m a -> m sep -> m [a]
`sepEndBy` Parser ()
(?esc::Char) => Parser ()
requiredWhitespace
let chownFlags :: [Chown]
chownFlags = [Chown
c | FlagChown Chown
c <- [CopyFlag]
flags]
let chmodFlags :: [Chmod]
chmodFlags = [Chmod
c | FlagChmod Chmod
c <- [CopyFlag]
flags]
let invalidFlags :: [(Text, Text)]
invalidFlags = [(Text, Text)
i | FlagInvalid (Text, Text)
i <- [CopyFlag]
flags]
ParsecT DockerfileError Text Identity Text -> Parser ()
forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m ()
notFollowedBy (Tokens Text -> ParsecT DockerfileError Text Identity (Tokens Text)
forall e s (m :: * -> *).
MonadParsec e s m =>
Tokens s -> m (Tokens s)
string Tokens Text
"--") Parser () -> String -> Parser ()
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> String -> m a
<?>
String
"only the --chown flag, the --chmod flag or the src and dest paths"
case ([(Text, Text)]
invalidFlags, [Chown]
chownFlags, [Chmod]
chmodFlags) of
((Text
k, Text
v) : [(Text, Text)]
_, [Chown]
_, [Chmod]
_) -> Text -> Text -> Parser (Instruction Text)
forall a. Text -> Text -> Parser a
unexpectedFlag Text
k Text
v
([(Text, Text)]
_, Chown
_ : Chown
_ : [Chown]
_, [Chmod]
_) -> DockerfileError -> Parser (Instruction Text)
forall a. DockerfileError -> Parser a
customError (DockerfileError -> Parser (Instruction Text))
-> DockerfileError -> Parser (Instruction Text)
forall a b. (a -> b) -> a -> b
$ String -> DockerfileError
DuplicateFlagError String
"--chown"
([(Text, Text)]
_, [Chown]
_, Chmod
_ : Chmod
_ : [Chmod]
_) -> DockerfileError -> Parser (Instruction Text)
forall a. DockerfileError -> Parser a
customError (DockerfileError -> Parser (Instruction Text))
-> DockerfileError -> Parser (Instruction Text)
forall a b. (a -> b) -> a -> b
$ String -> DockerfileError
DuplicateFlagError String
"--chmod"
([(Text, Text)], [Chown], [Chmod])
_ -> do
let cho :: Chown
cho = case [Chown]
chownFlags of
[] -> Chown
NoChown
Chown
c : [Chown]
_ -> Chown
c
let chm :: Chmod
chm = case [Chmod]
chmodFlags of
[] -> Chmod
NoChmod
Chmod
c : [Chmod]
_ -> Chmod
c
(?esc::Char) =>
Text
-> (NonEmpty SourcePath -> TargetPath -> Instruction Text)
-> Parser (Instruction Text)
Text
-> (NonEmpty SourcePath -> TargetPath -> Instruction Text)
-> Parser (Instruction Text)
fileList Text
"ADD" (\NonEmpty SourcePath
src TargetPath
dest -> AddArgs -> Instruction Text
forall args. AddArgs -> Instruction args
Add (NonEmpty SourcePath -> TargetPath -> Chown -> Chmod -> AddArgs
AddArgs NonEmpty SourcePath
src TargetPath
dest Chown
cho Chmod
chm))
fileList :: (?esc :: Char) => Text ->
(NonEmpty SourcePath -> TargetPath -> Instruction Text) ->
Parser (Instruction Text)
fileList :: Text
-> (NonEmpty SourcePath -> TargetPath -> Instruction Text)
-> Parser (Instruction Text)
fileList Text
name NonEmpty SourcePath -> TargetPath -> Instruction Text
constr = do
[Text]
paths <-
(ParsecT DockerfileError Text Identity [Text]
-> ParsecT DockerfileError Text Identity [Text]
forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m a
try ParsecT DockerfileError Text Identity [Text]
stringList ParsecT DockerfileError Text Identity [Text]
-> String -> ParsecT DockerfileError Text Identity [Text]
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> String -> m a
<?> String
"an array of strings [\"src_file\", \"dest_file\"]")
ParsecT DockerfileError Text Identity [Text]
-> ParsecT DockerfileError Text Identity [Text]
-> ParsecT DockerfileError Text Identity [Text]
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (ParsecT DockerfileError Text Identity [Text]
-> ParsecT DockerfileError Text Identity [Text]
forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m a
try ParsecT DockerfileError Text Identity [Text]
spaceSeparated ParsecT DockerfileError Text Identity [Text]
-> String -> ParsecT DockerfileError Text Identity [Text]
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> String -> m a
<?> String
"a space separated list of file paths")
case [Text]
paths of
[Text
_] -> DockerfileError -> Parser (Instruction Text)
forall a. DockerfileError -> Parser a
customError (DockerfileError -> Parser (Instruction Text))
-> DockerfileError -> Parser (Instruction Text)
forall a b. (a -> b) -> a -> b
$ String -> DockerfileError
FileListError (Text -> String
T.unpack Text
name)
[Text]
_ -> Instruction Text -> Parser (Instruction Text)
forall (m :: * -> *) a. Monad m => a -> m a
return (Instruction Text -> Parser (Instruction Text))
-> Instruction Text -> Parser (Instruction Text)
forall a b. (a -> b) -> a -> b
$ NonEmpty SourcePath -> TargetPath -> Instruction Text
constr (Text -> SourcePath
SourcePath (Text -> SourcePath) -> NonEmpty Text -> NonEmpty SourcePath
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Text] -> NonEmpty Text
forall a. [a] -> NonEmpty a
fromList ([Text] -> [Text]
forall a. [a] -> [a]
init [Text]
paths)) (Text -> TargetPath
TargetPath (Text -> TargetPath) -> Text -> TargetPath
forall a b. (a -> b) -> a -> b
$ [Text] -> Text
forall a. [a] -> a
last [Text]
paths)
where
spaceSeparated :: ParsecT DockerfileError Text Identity [Text]
spaceSeparated =
(?esc::Char) =>
String
-> (Char -> Bool) -> ParsecT DockerfileError Text Identity Text
String
-> (Char -> Bool) -> ParsecT DockerfileError Text Identity Text
someUnless String
"a file" (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
' ') ParsecT DockerfileError Text Identity Text
-> Parser () -> ParsecT DockerfileError Text Identity [Text]
forall (m :: * -> *) a sep. MonadPlus m => m a -> m sep -> m [a]
`sepEndBy1` (Parser () -> Parser ()
forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m a
try Parser ()
(?esc::Char) => Parser ()
requiredWhitespace Parser () -> String -> Parser ()
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> String -> m a
<?> String
"at least another file path")
stringList :: ParsecT DockerfileError Text Identity [Text]
stringList = ParsecT DockerfileError Text Identity [Text]
-> ParsecT DockerfileError Text Identity [Text]
forall a. (?esc::Char) => Parser a -> Parser a
brackets (ParsecT DockerfileError Text Identity [Text]
-> ParsecT DockerfileError Text Identity [Text])
-> ParsecT DockerfileError Text Identity [Text]
-> ParsecT DockerfileError Text Identity [Text]
forall a b. (a -> b) -> a -> b
$ ParsecT DockerfileError Text Identity Text
-> ParsecT DockerfileError Text Identity [Text]
forall a. (?esc::Char) => Parser a -> Parser [a]
commaSep ParsecT DockerfileError Text Identity Text
stringLiteral
unexpectedFlag :: Text -> Text -> Parser a
unexpectedFlag :: Text -> Text -> Parser a
unexpectedFlag Text
name Text
"" = DockerfileError -> Parser a
forall e s (m :: * -> *) a. MonadParsec e s m => e -> m a
customFailure (DockerfileError -> Parser a) -> DockerfileError -> Parser a
forall a b. (a -> b) -> a -> b
$ String -> DockerfileError
NoValueFlagError (Text -> String
T.unpack Text
name)
unexpectedFlag Text
name Text
_ = DockerfileError -> Parser a
forall e s (m :: * -> *) a. MonadParsec e s m => e -> m a
customFailure (DockerfileError -> Parser a) -> DockerfileError -> Parser a
forall a b. (a -> b) -> a -> b
$ String -> DockerfileError
InvalidFlagError (Text -> String
T.unpack Text
name)
copyFlag :: (?esc :: Char) => Parser CopyFlag
copyFlag :: Parser CopyFlag
copyFlag =
(Chown -> CopyFlag
FlagChown (Chown -> CopyFlag)
-> ParsecT DockerfileError Text Identity Chown -> Parser CopyFlag
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT DockerfileError Text Identity Chown
-> ParsecT DockerfileError Text Identity Chown
forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m a
try ParsecT DockerfileError Text Identity Chown
(?esc::Char) => ParsecT DockerfileError Text Identity Chown
chown Parser CopyFlag -> String -> Parser CopyFlag
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> String -> m a
<?> String
"only one --chown")
Parser CopyFlag -> Parser CopyFlag -> Parser CopyFlag
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (Chmod -> CopyFlag
FlagChmod (Chmod -> CopyFlag)
-> ParsecT DockerfileError Text Identity Chmod -> Parser CopyFlag
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT DockerfileError Text Identity Chmod
-> ParsecT DockerfileError Text Identity Chmod
forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m a
try ParsecT DockerfileError Text Identity Chmod
(?esc::Char) => ParsecT DockerfileError Text Identity Chmod
chmod Parser CopyFlag -> String -> Parser CopyFlag
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> String -> m a
<?> String
"only one --chmod")
Parser CopyFlag -> Parser CopyFlag -> Parser CopyFlag
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (CopySource -> CopyFlag
FlagSource (CopySource -> CopyFlag)
-> ParsecT DockerfileError Text Identity CopySource
-> Parser CopyFlag
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT DockerfileError Text Identity CopySource
-> ParsecT DockerfileError Text Identity CopySource
forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m a
try ParsecT DockerfileError Text Identity CopySource
(?esc::Char) => ParsecT DockerfileError Text Identity CopySource
copySource Parser CopyFlag -> String -> Parser CopyFlag
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> String -> m a
<?> String
"only one --from")
Parser CopyFlag -> Parser CopyFlag -> Parser CopyFlag
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ((Text, Text) -> CopyFlag
FlagInvalid ((Text, Text) -> CopyFlag)
-> ParsecT DockerfileError Text Identity (Text, Text)
-> Parser CopyFlag
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT DockerfileError Text Identity (Text, Text)
-> ParsecT DockerfileError Text Identity (Text, Text)
forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m a
try ParsecT DockerfileError Text Identity (Text, Text)
(?esc::Char) => ParsecT DockerfileError Text Identity (Text, Text)
anyFlag Parser CopyFlag -> String -> Parser CopyFlag
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> String -> m a
<?> String
"no other flags")
chown :: (?esc :: Char) => Parser Chown
chown :: ParsecT DockerfileError Text Identity Chown
chown = do
ParsecT DockerfileError Text Identity Text -> Parser ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (ParsecT DockerfileError Text Identity Text -> Parser ())
-> ParsecT DockerfileError Text Identity Text -> Parser ()
forall a b. (a -> b) -> a -> b
$ Tokens Text -> ParsecT DockerfileError Text Identity (Tokens Text)
forall e s (m :: * -> *).
MonadParsec e s m =>
Tokens s -> m (Tokens s)
string Tokens Text
"--chown="
Text
cho <- (?esc::Char) =>
String
-> (Char -> Bool) -> ParsecT DockerfileError Text Identity Text
String
-> (Char -> Bool) -> ParsecT DockerfileError Text Identity Text
someUnless String
"the user and group for chown" (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
' ')
Chown -> ParsecT DockerfileError Text Identity Chown
forall (m :: * -> *) a. Monad m => a -> m a
return (Chown -> ParsecT DockerfileError Text Identity Chown)
-> Chown -> ParsecT DockerfileError Text Identity Chown
forall a b. (a -> b) -> a -> b
$ Text -> Chown
Chown Text
cho
chmod :: (?esc :: Char) => Parser Chmod
chmod :: ParsecT DockerfileError Text Identity Chmod
chmod = do
ParsecT DockerfileError Text Identity Text -> Parser ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (ParsecT DockerfileError Text Identity Text -> Parser ())
-> ParsecT DockerfileError Text Identity Text -> Parser ()
forall a b. (a -> b) -> a -> b
$ Tokens Text -> ParsecT DockerfileError Text Identity (Tokens Text)
forall e s (m :: * -> *).
MonadParsec e s m =>
Tokens s -> m (Tokens s)
string Tokens Text
"--chmod="
Text
chm <- (?esc::Char) =>
String
-> (Char -> Bool) -> ParsecT DockerfileError Text Identity Text
String
-> (Char -> Bool) -> ParsecT DockerfileError Text Identity Text
someUnless String
"the mode for chmod" (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
' ')
Chmod -> ParsecT DockerfileError Text Identity Chmod
forall (m :: * -> *) a. Monad m => a -> m a
return (Chmod -> ParsecT DockerfileError Text Identity Chmod)
-> Chmod -> ParsecT DockerfileError Text Identity Chmod
forall a b. (a -> b) -> a -> b
$ Text -> Chmod
Chmod Text
chm
copySource :: (?esc :: Char) => Parser CopySource
copySource :: ParsecT DockerfileError Text Identity CopySource
copySource = do
ParsecT DockerfileError Text Identity Text -> Parser ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (ParsecT DockerfileError Text Identity Text -> Parser ())
-> ParsecT DockerfileError Text Identity Text -> Parser ()
forall a b. (a -> b) -> a -> b
$ Tokens Text -> ParsecT DockerfileError Text Identity (Tokens Text)
forall e s (m :: * -> *).
MonadParsec e s m =>
Tokens s -> m (Tokens s)
string Tokens Text
"--from="
Text
src <- (?esc::Char) =>
String
-> (Char -> Bool) -> ParsecT DockerfileError Text Identity Text
String
-> (Char -> Bool) -> ParsecT DockerfileError Text Identity Text
someUnless String
"the copy source path" Char -> Bool
isNl
CopySource -> ParsecT DockerfileError Text Identity CopySource
forall (m :: * -> *) a. Monad m => a -> m a
return (CopySource -> ParsecT DockerfileError Text Identity CopySource)
-> CopySource -> ParsecT DockerfileError Text Identity CopySource
forall a b. (a -> b) -> a -> b
$ Text -> CopySource
CopySource Text
src
anyFlag :: (?esc :: Char) => Parser (Text, Text)
anyFlag :: ParsecT DockerfileError Text Identity (Text, Text)
anyFlag = do
ParsecT DockerfileError Text Identity Text -> Parser ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (ParsecT DockerfileError Text Identity Text -> Parser ())
-> ParsecT DockerfileError Text Identity Text -> Parser ()
forall a b. (a -> b) -> a -> b
$ Tokens Text -> ParsecT DockerfileError Text Identity (Tokens Text)
forall e s (m :: * -> *).
MonadParsec e s m =>
Tokens s -> m (Tokens s)
string Tokens Text
"--"
Text
name <- (?esc::Char) =>
String
-> (Char -> Bool) -> ParsecT DockerfileError Text Identity Text
String
-> (Char -> Bool) -> ParsecT DockerfileError Text Identity Text
someUnless String
"the flag value" (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'=')
ParsecT DockerfileError Text Identity Char -> Parser ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (ParsecT DockerfileError Text Identity Char -> Parser ())
-> ParsecT DockerfileError Text Identity Char -> Parser ()
forall a b. (a -> b) -> a -> b
$ Token Text -> ParsecT DockerfileError Text Identity (Token Text)
forall e s (m :: * -> *).
(MonadParsec e s m, Token s ~ Char) =>
Token s -> m (Token s)
char Char
Token Text
'='
Text
val <- (?esc::Char) =>
(Char -> Bool) -> ParsecT DockerfileError Text Identity Text
(Char -> Bool) -> ParsecT DockerfileError Text Identity Text
anyUnless (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
' ')
(Text, Text) -> ParsecT DockerfileError Text Identity (Text, Text)
forall (m :: * -> *) a. Monad m => a -> m a
return (Text -> Text -> Text
T.append Text
"--" Text
name, Text
val)