module Dhall.Secret.Aws where

import           Control.Lens             ((&), (.~))
import           Data.Text                (pack)
import           Network.AWS              (AWS, Credentials (Discover),
                                           HasEnv (envLogger), LogLevel (Info),
                                           envRegion, newEnv, newLogger, runAWS,
                                           runResourceT)
import           Network.AWS.Data         (fromText)
import           System.Environment.Blank (getEnv)
import           System.IO                (stdout)

awsRun :: Network.AWS.AWS b -> IO b
awsRun :: AWS b -> IO b
awsRun AWS b
cmd = do
  Logger
logger <- LogLevel -> Handle -> IO Logger
forall (m :: * -> *). MonadIO m => LogLevel -> Handle -> m Logger
Network.AWS.newLogger LogLevel
Network.AWS.Info Handle
stdout
  Env
discover <- Credentials -> IO Env
forall (m :: * -> *).
(Applicative m, MonadIO m, MonadCatch m) =>
Credentials -> m Env
Network.AWS.newEnv Credentials
Network.AWS.Discover
  Maybe String
defaultRegion <- String -> IO (Maybe String)
getEnv String
"AWS_REGION"
  ResourceT IO b -> IO b
forall (m :: * -> *) a. MonadUnliftIO m => ResourceT m a -> m a
Network.AWS.runResourceT (ResourceT IO b -> IO b) -> ResourceT IO b -> IO b
forall a b. (a -> b) -> a -> b
$ Env -> AWS b -> ResourceT IO b
forall (m :: * -> *) r a.
(MonadResource m, HasEnv r) =>
r -> AWS a -> m a
Network.AWS.runAWS (case (Either String Region -> Maybe Region
forall a a. Either a a -> Maybe a
hush (Either String Region -> Maybe Region)
-> (String -> Either String Region) -> String -> Maybe Region
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Either String Region
forall a. FromText a => Text -> Either String a
fromText (Text -> Either String Region)
-> (String -> Text) -> String -> Either String Region
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
pack) (String -> Maybe Region) -> Maybe String -> Maybe Region
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Maybe String
defaultRegion of
    Maybe Region
Nothing     -> Env
discover Env -> (Env -> Env) -> Env
forall a b. a -> (a -> b) -> b
& (Logger -> Identity Logger) -> Env -> Identity Env
forall a. HasEnv a => Lens' a Logger
Network.AWS.envLogger ((Logger -> Identity Logger) -> Env -> Identity Env)
-> Logger -> Env -> Env
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Logger
logger
    Just Region
region -> Env
discover Env -> (Env -> Env) -> Env
forall a b. a -> (a -> b) -> b
& (Region -> Identity Region) -> Env -> Identity Env
forall a. HasEnv a => Lens' a Region
Network.AWS.envRegion ((Region -> Identity Region) -> Env -> Identity Env)
-> Region -> Env -> Env
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Region
region Env -> (Env -> Env) -> Env
forall a b. a -> (a -> b) -> b
& (Logger -> Identity Logger) -> Env -> Identity Env
forall a. HasEnv a => Lens' a Logger
Network.AWS.envLogger ((Logger -> Identity Logger) -> Env -> Identity Env)
-> Logger -> Env -> Env
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Logger
logger)
    AWS b
cmd
  where
    hush :: Either a a -> Maybe a
hush (Left a
_)  = Maybe a
forall a. Maybe a
Nothing
    hush (Right a
x) = a -> Maybe a
forall a. a -> Maybe a
Just a
x