Created
February 8, 2014 08:22
-
-
Save fujimura/8878486 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module S3.Ls | |
( ls | |
) where | |
import Control.Applicative ((<*)) | |
import Control.Exception (throwIO) | |
import System.Process (readProcessWithExitCode) | |
import Text.Parsec | |
import Text.Parsec.String | |
newtype S3Object = S3Object (String, String, String) deriving (Eq,Show) | |
ls :: String -> IO [S3Object] | |
ls bucketName = do | |
(_,ok,ng) <- readProcessWithExitCode "s3cmd" ["ls", "s3://" ++ bucketName] [] | |
if ok == "" | |
then throwIO $ userError $ "No bucket found" ++ " " ++ "(" ++ bucketName ++ ")" | |
else return $ parseResult ok | |
result = many line <* eof | |
parseResult :: String -> [S3Object] | |
parseResult str = case parse result "ERROR" str of | |
Left l -> error $ show l | |
Right xs -> xs | |
line :: Parser S3Object | |
line = do | |
date <- manyTill anyChar (try (string " ")) | |
size <- manyTill anyChar (try (string " ")) | |
string "s3://" | |
name <- manyTill anyChar (try (char '\n')) | |
return $ S3Object (date, size, name) |
Author
fujimura
commented
Feb 8, 2014
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment