diff options
author | Joey Hess <joey@kitenet.net> | 2011-05-15 02:49:43 -0400 |
---|---|---|
committer | Joey Hess <joey@kitenet.net> | 2011-05-15 03:38:08 -0400 |
commit | cad0e1c8b7eb21f8dceca8dd9fa3bc1d1aa7eabd (patch) | |
tree | b6be12dc1cc83a35ca7d89a862d85e6d71c38572 /Remote | |
parent | efa7f544050c0d5be6bc1b0fc0125278e475c213 (diff) |
simplified a bunch of Maybe handling
Diffstat (limited to 'Remote')
-rw-r--r-- | Remote/Bup.hs | 5 | ||||
-rw-r--r-- | Remote/Directory.hs | 5 | ||||
-rw-r--r-- | Remote/Encryptable.hs | 9 | ||||
-rw-r--r-- | Remote/Hook.hs | 15 | ||||
-rw-r--r-- | Remote/Rsync.hs | 5 | ||||
-rw-r--r-- | Remote/S3real.hs | 16 |
6 files changed, 22 insertions, 33 deletions
diff --git a/Remote/Bup.hs b/Remote/Bup.hs index 0aaff06b2..d2b771bf7 100644 --- a/Remote/Bup.hs +++ b/Remote/Bup.hs @@ -68,9 +68,8 @@ gen r u c = do bupSetup :: UUID -> RemoteConfig -> Annex RemoteConfig bupSetup u c = do -- verify configuration is sane - let buprepo = case M.lookup "buprepo" c of - Nothing -> error "Specify buprepo=" - Just r -> r + let buprepo = maybe (error "Specify buprepo=") id $ + M.lookup "buprepo" c c' <- encryptionSetup c -- bup init will create the repository. diff --git a/Remote/Directory.hs b/Remote/Directory.hs index c680d6121..0cd3760d6 100644 --- a/Remote/Directory.hs +++ b/Remote/Directory.hs @@ -60,9 +60,8 @@ gen r u c = do directorySetup :: UUID -> RemoteConfig -> Annex RemoteConfig directorySetup u c = do -- verify configuration is sane - let dir = case M.lookup "directory" c of - Nothing -> error "Specify directory=" - Just d -> d + let dir = maybe (error "Specify directory=") id $ + M.lookup "directory" c e <- liftIO $ doesDirectoryExist dir when (not e) $ error $ "Directory does not exist: " ++ dir c' <- encryptionSetup c diff --git a/Remote/Encryptable.hs b/Remote/Encryptable.hs index 31ef1f37a..f9b388c8a 100644 --- a/Remote/Encryptable.hs +++ b/Remote/Encryptable.hs @@ -73,11 +73,10 @@ encryptableRemote c storeKeyEncrypted retrieveKeyFileEncrypted r = {- Gets encryption Cipher. The decrypted Cipher is cached in the Annex - state. -} remoteCipher :: RemoteConfig -> Annex (Maybe Cipher) -remoteCipher c = do - cache <- Annex.getState Annex.cipher - case cache of - Just cipher -> return $ Just cipher - Nothing -> case extractCipher c of +remoteCipher c = maybe expensive cached =<< Annex.getState Annex.cipher + where + cached cipher = return $ Just cipher + expensive = case extractCipher c of Nothing -> return Nothing Just encipher -> do showNote "gpg" diff --git a/Remote/Hook.hs b/Remote/Hook.hs index ba38355ca..7f2d5dbee 100644 --- a/Remote/Hook.hs +++ b/Remote/Hook.hs @@ -61,9 +61,8 @@ gen r u c = do hookSetup :: UUID -> RemoteConfig -> Annex RemoteConfig hookSetup u c = do - let hooktype = case M.lookup "hooktype" c of - Nothing -> error "Specify hooktype=" - Just r -> r + let hooktype = maybe (error "Specify hooktype=") id $ + M.lookup "hooktype" c c' <- encryptionSetup c gitConfigSpecialRemote u c' "hooktype" hooktype return c' @@ -94,14 +93,12 @@ lookupHook hooktype hook =do hookname = hooktype ++ "-" ++ hook ++ "-hook" runHook :: String -> String -> Key -> Maybe FilePath -> Annex Bool -> Annex Bool -runHook hooktype hook k f a = do - command <- lookupHook hooktype hook - case command of - Nothing -> return False - Just c -> do +runHook hooktype hook k f a = maybe (return False) run =<< lookupHook hooktype hook + where + run command = do showProgress -- make way for hook output res <- liftIO $ boolSystemEnv - "sh" [Param "-c", Param c] $ hookEnv k f + "sh" [Param "-c", Param command] $ hookEnv k f if res then a else do diff --git a/Remote/Rsync.hs b/Remote/Rsync.hs index 682c96174..c15ab37a7 100644 --- a/Remote/Rsync.hs +++ b/Remote/Rsync.hs @@ -82,9 +82,8 @@ genRsyncOpts r = do rsyncSetup :: UUID -> RemoteConfig -> Annex RemoteConfig rsyncSetup u c = do -- verify configuration is sane - let url = case M.lookup "rsyncurl" c of - Nothing -> error "Specify rsyncurl=" - Just d -> d + let url = maybe (error "Specify rsyncurl=") id $ + M.lookup "rsyncurl" c c' <- encryptionSetup c -- The rsyncurl is stored in git config, not only in this remote's diff --git a/Remote/S3real.hs b/Remote/S3real.hs index b0371eb5e..eaa6590b1 100644 --- a/Remote/S3real.hs +++ b/Remote/S3real.hs @@ -123,11 +123,7 @@ storeHelper (conn, bucket) r k file = do content <- liftIO $ L.readFile file -- size is provided to S3 so the whole content does not need to be -- buffered to calculate it - size <- case keySize k of - Just s -> return $ fromIntegral s - Nothing -> do - s <- liftIO $ getFileStatus file - return $ fileSize s + size <- maybe getsize (return . fromIntegral) $ keySize k let object = setStorageClass storageclass $ S3Object bucket (show k) "" [("Content-Length",(show size))] content @@ -137,6 +133,9 @@ storeHelper (conn, bucket) r k file = do case fromJust $ M.lookup "storageclass" $ fromJust $ config r of "REDUCED_REDUNDANCY" -> REDUCED_REDUNDANCY _ -> STANDARD + getsize = do + s <- liftIO $ getFileStatus file + return $ fileSize s retrieve :: Remote Annex -> Key -> FilePath -> Annex Bool retrieve r k f = s3Action r False $ \(conn, bucket) -> do @@ -201,11 +200,8 @@ bucketKey :: String -> Key -> S3Object bucketKey bucket k = S3Object bucket (show k) "" [] L.empty s3ConnectionRequired :: RemoteConfig -> Annex AWSConnection -s3ConnectionRequired c = do - conn <- s3Connection c - case conn of - Nothing -> error "Cannot connect to S3" - Just conn' -> return conn' +s3ConnectionRequired c = + maybe (error "Cannot connect to S3") return =<< s3Connection c s3Connection :: RemoteConfig -> Annex (Maybe AWSConnection) s3Connection c = do |