This commit is contained in:
Joey Hess 2015-07-09 16:05:45 -04:00
parent 8ad927dbc6
commit a7f58634b8
11 changed files with 109 additions and 79 deletions

View file

@ -16,41 +16,50 @@ import qualified Remote
import Types.Remote
cmd :: Command
cmd = withOptions transferKeyOptions $ noCommit $
cmd = noCommit $
command "transferkey" SectionPlumbing
"transfers a key from or to a remote"
paramKey (withParams seek)
paramKey (seek <--< optParser)
transferKeyOptions :: [Option]
transferKeyOptions = fileOption : fromToOptions
data TransferKeyOptions = TransferKeyOptions
{ keyOptions :: CmdParams
, fromToOptions :: FromToOptions
, fileOption :: AssociatedFile
}
fileOption :: Option
fileOption = fieldOption [] "file" paramFile "the associated file"
optParser :: CmdParamsDesc -> Parser TransferKeyOptions
optParser desc = TransferKeyOptions
<$> cmdParams desc
<*> parseFromToOptions
<*> optional (strOption
( long "file" <> metavar paramFile
<> help "the associated file"
))
seek :: CmdParams -> CommandSeek
seek ps = do
to <- getOptionField toOption Remote.byNameWithUUID
from <- getOptionField fromOption Remote.byNameWithUUID
file <- getOptionField fileOption return
withKeys (start to from file) ps
instance DeferredParseClass TransferKeyOptions where
finishParse v = TransferKeyOptions
<$> pure (keyOptions v)
<*> finishParse (fromToOptions v)
<*> pure (fileOption v)
start :: Maybe Remote -> Maybe Remote -> AssociatedFile -> Key -> CommandStart
start to from file key =
case (from, to) of
(Nothing, Just dest) -> next $ toPerform dest key file
(Just src, Nothing) -> next $ fromPerform src key file
_ -> error "specify either --from or --to"
seek :: TransferKeyOptions -> CommandSeek
seek o = withKeys (start o) (keyOptions o)
toPerform :: Remote -> Key -> AssociatedFile -> CommandPerform
toPerform remote key file = go Upload file $
start :: TransferKeyOptions -> Key -> CommandStart
start o key = case fromToOptions o of
ToRemote dest -> next $ toPerform key (fileOption o) =<< getParsed dest
FromRemote src -> next $ fromPerform key (fileOption o) =<< getParsed src
toPerform :: Key -> AssociatedFile -> Remote -> CommandPerform
toPerform key file remote = go Upload file $
upload (uuid remote) key file forwardRetry noObserver $ \p -> do
ok <- Remote.storeKey remote key file p
when ok $
Remote.logStatus remote key InfoPresent
return ok
fromPerform :: Remote -> Key -> AssociatedFile -> CommandPerform
fromPerform remote key file = go Upload file $
fromPerform :: Key -> AssociatedFile -> Remote -> CommandPerform
fromPerform key file remote = go Upload file $
download (uuid remote) key file forwardRetry noObserver $ \p ->
getViaTmp key $ \t -> Remote.retrieveKeyFile remote key file t p