Skip to content

Commit

Permalink
Merge pull request #41 from hadolint/fix-registry-parser
Browse files Browse the repository at this point in the history
Fixed the parser for image registries
  • Loading branch information
lorenzo authored Jul 6, 2018
2 parents 75897fd + f2dc29e commit f28a57e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
7 changes: 5 additions & 2 deletions src/Language/Docker/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import qualified Data.ByteString as B
import Data.Data
import Data.List.NonEmpty (NonEmpty, fromList)
import Data.Maybe (listToMaybe)
import Data.Semigroup ((<>))
import qualified Data.Set as S
import Data.Text (Text)
import qualified Data.Text as T
Expand Down Expand Up @@ -151,9 +152,11 @@ comment = do

parseRegistry :: Parser Registry
parseRegistry = do
name <- someUnless "a registry name" (== '/')
domain <- someUnless "a domain name" (== '.')
void $ char '.'
tld <- someUnless "a TLD" (== '/')
void $ char '/'
return $ Registry name
return $ Registry (domain <> "." <> tld)

taggedImage :: Parser BaseImage
taggedImage = do
Expand Down
11 changes: 5 additions & 6 deletions src/Language/Docker/Syntax.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@ instance IsString Image where
fromString img =
if "/" `isInfixOf` img
then let parts = endBy "/" img
in case parts of
reg:rest ->
Image
(Just (Registry (Text.pack reg)))
(Text.pack . intercalate "/" $ rest)
_ -> Image Nothing (Text.pack img)
in if "." `isInfixOf` head parts
then Image
(Just (Registry (Text.pack (head parts))))
(Text.pack . intercalate "/" $ tail parts)
else Image Nothing (Text.pack img)
else Image Nothing (Text.pack img)

newtype Registry = Registry
Expand Down
4 changes: 4 additions & 0 deletions test/Language/Docker/ParserSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ spec = do
assertAst
"FROM myregistry.com:5000/imagename:5.12-dev"
[From (TaggedImage (Image (Just "myregistry.com:5000") "imagename") "5.12-dev" Nothing)]
it "Not a registry if no TLD" $
assertAst
"FROM myfolder/imagename:5.12-dev"
[From (TaggedImage (Image Nothing "myfolder/imagename") "5.12-dev" Nothing)]

describe "parse LABEL" $ do
it "parse label" $ assertAst "LABEL foo=bar" [Label[("foo", "bar")]]
Expand Down

0 comments on commit f28a57e

Please sign in to comment.