p2p: Added --one-way option.
authorJoey Hess <joeyh@joeyh.name>
Fri, 16 Dec 2016 20:43:37 +0000 (16:43 -0400)
committerJoey Hess <joeyh@joeyh.name>
Fri, 16 Dec 2016 20:43:37 +0000 (16:43 -0400)
This commit was sponsored by Fernando Jimenez on Patreon.

CHANGELOG
Command/P2P.hs
doc/git-annex-p2p.mdwn

index faadc132e1852950309c180cd0b71bff9b63c321..c4d3e2712891f9589a143874d07f778580a96931 100644 (file)
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -17,6 +17,7 @@ git-annex (6.20161211) UNRELEASED; urgency=medium
   * p2p --link now defaults to setting up a bi-directional link;
     both the local and remote git repositories get remotes added
     pointing at one-another.
+  * p2p: Added --one-way option.
 
  -- Joey Hess <id@joeyh.name>  Sun, 11 Dec 2016 21:29:51 -0400
 
index 323906f36167a6b8d893ab0f44f36d3c6c2ebfd1..817840d0704de6e9ae48f72b9f50e9c9dde00762 100644 (file)
@@ -24,10 +24,13 @@ data P2POpts
        = GenAddresses
        | LinkRemote
 
-optParser :: CmdParamsDesc -> Parser (P2POpts, Maybe RemoteName)
-optParser _ = (,)
+data LinkDirection = BiDirectional | OneWay
+
+optParser :: CmdParamsDesc -> Parser (P2POpts, Maybe RemoteName, LinkDirection)
+optParser _ = (,,)
        <$> (genaddresses <|> linkremote)
        <*> optional name
+       <*> direction
   where
        genaddresses = flag' GenAddresses
                ( long "gen-addresses"
@@ -42,13 +45,17 @@ optParser _ = (,)
                <> metavar paramName
                <> help "name of remote"
                )
+       direction = flag BiDirectional OneWay
+               ( long "one-way"
+               <> help "make one-way link, rather than default bi-directional link"
+               )
 
-seek :: (P2POpts, Maybe RemoteName) -> CommandSeek
-seek (GenAddresses, _) = genAddresses =<< loadP2PAddresses
-seek (LinkRemote, Just name) = commandAction $
-       linkRemote (Git.Remote.makeLegalName name)
-seek (LinkRemote, Nothing) = commandAction $
-       linkRemote =<< unusedPeerRemoteName
+seek :: (P2POpts, Maybe RemoteName, LinkDirection) -> CommandSeek
+seek (GenAddresses, _, _) = genAddresses =<< loadP2PAddresses
+seek (LinkRemote, Just name, direction) = commandAction $
+       linkRemote direction (Git.Remote.makeLegalName name)
+seek (LinkRemote, Nothing, direction) = commandAction $
+       linkRemote direction =<< unusedPeerRemoteName
 
 -- Only addresses are output to stdout, to allow scripting.
 genAddresses :: [P2PAddress] -> Annex ()
@@ -62,8 +69,8 @@ genAddresses addrs = do
                        map (`P2PAddressAuth` authtoken) addrs
 
 -- Address is read from stdin, to avoid leaking it in shell history.
-linkRemote :: RemoteName -> CommandStart
-linkRemote remotename = do
+linkRemote :: LinkDirection -> RemoteName -> CommandStart
+linkRemote direction remotename = do
        showStart "p2p link" remotename
        next $ next prompt
   where
@@ -81,9 +88,12 @@ linkRemote remotename = do
                                        liftIO $ hPutStrLn stderr "Unable to parse that address, please check its format and try again."
                                        prompt
                                Just addr -> do
-                                       myaddrs <- loadP2PAddresses
-                                       authtoken <- liftIO $ genAuthToken 128
-                                       storeP2PAuthToken authtoken
-                                       let linkbackto = map (`P2PAddressAuth` authtoken) myaddrs
+                                       linkbackto <- case direction of
+                                               OneWay -> return []
+                                               BiDirectional -> do
+                                                       myaddrs <- loadP2PAddresses
+                                                       authtoken <- liftIO $ genAuthToken 128
+                                                       storeP2PAuthToken authtoken
+                                                       return $ map (`P2PAddressAuth` authtoken) myaddrs
                                        linkAddress addr linkbackto remotename
                                                >>= either giveup return
index cefa116d6a5649c34e29f44f26a2255882ac3632..dcfb36a3f529f2782e87f63f14fe6fc7743ee9c5 100644 (file)
@@ -39,6 +39,11 @@ services.
 
   Specify a name to use when setting up a git remote.
 
+* `--one-way`
+
+  Use with `--link` to create a one-way link with a peer, rather than the
+  default bi-directional link.
+
 # SEE ALSO
 
 [[git-annex]](1)