fromkey, registerurl: Improve handling of urls that happen to also be parsable as strange keys.

This commit is contained in:
Joey Hess 2015-05-30 02:08:49 -04:00
parent fa38fe0cf6
commit 36b9c9ca5f
3 changed files with 25 additions and 5 deletions

View file

@ -54,12 +54,20 @@ massAdd = go True =<< map (separate (== ' ')) . lines <$> liftIO getContents
go status' rest
go _ _ = error "Expected pairs of key and file on stdin, but got something else."
-- From user input to a Key.
-- User can input either a serialized key, or an url.
--
-- In some cases, an input can be parsed as both a key and as an uri.
-- For example, "WORM--a:a" parses as an uri. To disambiguate, check
-- the uri scheme, to see if it looks like the prefix of a key. This relies
-- on key backend names never containing a ':'.
mkKey :: String -> Key
mkKey s = case file2key s of
Just k -> k
Nothing -> case parseURI s of
Just _u -> Backend.URL.fromUrl s Nothing
Nothing -> error $ "bad key " ++ s
mkKey s = case parseURI s of
Just u | not (isKeyPrefix (uriScheme u)) ->
Backend.URL.fromUrl s Nothing
_ -> case file2key s of
Just k -> k
Nothing -> error $ "bad key/url " ++ s
perform :: Key -> FilePath -> CommandPerform
perform key file = do