From dfcaca1cf83d7f1ec3ae6d4174a8d20c5cddc20d Mon Sep 17 00:00:00 2001 From: Tommaso Piazza Date: Tue, 16 Aug 2016 12:46:55 +0200 Subject: [PATCH 1/3] Implementing Region Discovery --- Rome.cabal | 1 + app/Main.hs | 14 ++++++++++---- src/Lib.hs | 45 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 4 deletions(-) diff --git a/Rome.cabal b/Rome.cabal index fe78756..d18c4c2 100644 --- a/Rome.cabal +++ b/Rome.cabal @@ -25,6 +25,7 @@ library , directory >= 1.2.2 , containers >= 0.5 , conduit-extra >= 1.1 + , ini >= 0.3.5 , text >= 1.2 , bytestring >= 0.10 , zip-archive >= 0.2 diff --git a/app/Main.hs b/app/Main.hs index 68b5380..3aa8158 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -1,3 +1,7 @@ +{-# LANGUAGE OverloadedStrings #-} + + + module Main where import Control.Monad.Except @@ -8,7 +12,7 @@ import Options.Applicative as Opts romeVersion :: String -romeVersion = "0.3.0.1" +romeVersion = "0.3.0.2" @@ -20,8 +24,10 @@ main = do case cmd of Nothing -> putStrLn $ romeVersion ++ " - Romam uno die non fuisse conditam." Just romeOptions -> do - env <- AWS.newEnv AWS.NorthVirginia AWS.Discover - l <- runExceptT $ runRomeWithOptions env romeOptions - case l of + p <- runExceptT $ do + r <- discoverRegion + env <- AWS.newEnv r AWS.Discover + runRomeWithOptions env romeOptions + case p of Right _ -> return () Left e -> putStrLn e diff --git a/src/Lib.hs b/src/Lib.hs index 1d61a55..a99c0e2 100644 --- a/src/Lib.hs +++ b/src/Lib.hs @@ -9,6 +9,7 @@ module Lib ( parseRomeOptions , runRomeWithOptions + , discoverRegion ) where @@ -23,15 +24,19 @@ import Control.Monad.Reader (ReaderT, ask, runReaderT) import Control.Monad.Trans (MonadIO, lift, liftIO) import Control.Monad.Trans.Resource (runResourceT) import qualified Data.ByteString.Lazy as L +import Data.Char (isSpace) import Data.Conduit.Binary (sinkLbs) +import Data.Ini as INI import qualified Data.Map as M import Data.Maybe import qualified Data.Text as T +import qualified Data.Text.Encoding as T import qualified Network.AWS as AWS import Network.AWS.Data import Network.AWS.S3 as S3 import Options.Applicative as Opts import System.Directory +import System.Environment import qualified Text.Parsec as Parsec import Text.Parsec.String @@ -273,6 +278,46 @@ replaceKnownFrameworkNamesWitGitRepoNamesInProbeResults reverseRomeMap = map (re replaceResultIfFrameworkNameIsInMap reverseRomeMap ((frameworkName, version), present) = ((fromMaybe frameworkName (M.lookup frameworkName reverseRomeMap), version), present) +s3ConfigFile :: (MonadIO m) => m FilePath +s3ConfigFile = (++ p) `liftM` liftIO getHomeDirectory + where + p = "/.aws/config" + +discoverRegion :: RomeMonad AWS.Region +discoverRegion = do + f <- s3ConfigFile + profile <- liftIO $ lookupEnv "AWS_PROFILE" + getRegionFromFile f (fromMaybe "default" profile) + +getRegionFromFile :: FilePath -> String -> RomeMonad AWS.Region +getRegionFromFile f profile = do + i <- liftIO (INI.readIniFile f) + case i of + Left e -> throwError e + Right ini -> do + regionString <- req "region" ini + case (fromText regionString :: Either String AWS.Region) of + Left e -> throwError e + Right r -> return r + where + blank x = T.null x || T.all isSpace x + + req k i = + case INI.lookupValue (T.pack profile) k i of + Left e -> invalidErr (Just $ T.unpack k) e + Right x + | blank x -> invalidErr (Just $ T.unpack k) "cannot be a blank string." + | otherwise -> return x + + opt k i = return $ + case INI.lookupValue (T.pack profile) k i of + Left _ -> Nothing + Right x -> Just x + + invalidErr Nothing e = throwError e + invalidErr (Just k) e = throwError $ f <> ", key " <> k <> " " <> e + + -- Cartfile.resolved parsing From 7e1323678d7f8441b41963b379dd4deb34d44bd0 Mon Sep 17 00:00:00 2001 From: Tommaso Piazza Date: Tue, 16 Aug 2016 13:18:03 +0200 Subject: [PATCH 2/3] Removing unecessary language extenions --- app/Main.hs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/app/Main.hs b/app/Main.hs index 3aa8158..26ac34b 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -1,7 +1,3 @@ -{-# LANGUAGE OverloadedStrings #-} - - - module Main where import Control.Monad.Except From 5991914fc7876f603c4813ea20ed8e8bf9827e54 Mon Sep 17 00:00:00 2001 From: Tommaso Piazza Date: Tue, 16 Aug 2016 13:19:29 +0200 Subject: [PATCH 3/3] Removing import --- src/Lib.hs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Lib.hs b/src/Lib.hs index a99c0e2..3aa68f1 100644 --- a/src/Lib.hs +++ b/src/Lib.hs @@ -30,7 +30,6 @@ import Data.Ini as INI import qualified Data.Map as M import Data.Maybe import qualified Data.Text as T -import qualified Data.Text.Encoding as T import qualified Network.AWS as AWS import Network.AWS.Data import Network.AWS.S3 as S3