1
1
mirror of https://github.com/github/semantic.git synced 2024-11-24 17:04:47 +03:00

Rework the way we read a userID or repoID from the environment

- TIL : mkRegexWithOpts documentation is misleading. The first boolean
option is described as enforcing beginning of line (`^`) and end of
line (`$`) matching, along with not matching newline characters with
`.`. From my testing, I discovered this is not true, and in order to
enforce that regex behavior, I needed to explicitly add `^…$` to my
regex. Because the `mkRegexWithOpts` function was no longer doing what
I wanted, I opted for the simpler `mkRegex` function.

- readInt now returns a `Maybe Int` and expects a single `Just [s]`
match  supplied to attempt reading. This function is not general
purpose enough to be safe to use elsewhere, but is safe to use within
the context of `readIntFromEnv` because of the way the regex enforces
matches.
This commit is contained in:
Rick Winfrey 2017-03-21 10:39:14 -07:00
parent 773e808bdb
commit 4207e5f901

View File

@ -135,12 +135,17 @@ reportGitmon' SocketFactory{..} program gitCommand = do
readIntFromEnv :: Maybe String -> Maybe Int
readIntFromEnv Nothing = Nothing
readIntFromEnv (Just s) = Just (readInt $ subRegex regex s "")
where regex :: Regex
regex = mkRegexWithOpts "uint:" True False
readIntFromEnv (Just s) = readInt $ matchRegex regex s
where
-- | Expected format for userID and repoID is: "uint:123",
-- | where "uint:" indicates an unsigned integer followed by an integer value.
regex :: Regex
regex = mkRegex "^uint:([0-9]+)$"
readInt :: Maybe [String] -> Maybe Int
readInt (Just [s]) = Just (read s :: Int)
readInt _ = Nothing
readInt :: String -> Int
readInt s = read s :: Int
withGitmonSocket :: (Socket -> IO c) -> IO c
withGitmonSocket = bracket connectSocket close