<commit_before>{-# LANGUAGE ExtendedDefaultRules       #-}
{-# LANGUAGE NoImplicitPrelude          #-}
{-# LANGUAGE OverloadedStrings          #-}
{-# LANGUAGE RecordWildCards            #-}

{-# OPTIONS_GHC -fno-warn-type-defaults #-}

-- Module      : Khan.CLI.Profile
-- Copyright   : (c) 2013 <NAME> <<EMAIL>>
-- License     : This Source Code Form is subject to the terms of
--               the Mozilla Public License, v. 2.0.
--               A copy of the MPL can be found in the LICENSE file or
--               you can obtain it at http://mozilla.org/MPL/2.0/.
-- Maintainer  : <NAME> <<EMAIL>>
-- Stability   : experimental
-- Portability : non-portable (GHC extensions)

module Khan.CLI.Profile (commands) where

import           Data.Aeson
import qualified Data.Aeson.Encode.Pretty   as Aeson
import qualified Data.ByteString.Lazy.Char8 as LBS
import qualified Data.Text                  as Text
import qualified Data.Text.Encoding         as Text
import           Khan.Internal
import           Khan.Model.Profile         (Policy(..))
import qualified Khan.Model.Profile         as Profile
import           Khan.Prelude
import           Network.AWS.IAM            hiding (Role)
import           Network.HTTP.Types         (urlDecode)

default (Text)

data Role = Role
    { rRole   :: !Text
    , rEnv    :: !Text
    , rTrust  :: !FilePath
    , rPolicy :: !FilePath
    } deriving (Show)

roleParser :: Parser Role
roleParser = Role
    <$> roleOption
    <*> envOption
    <*> trustOption
    <*> policyOption

instance Options Role where
    discover _ Common{..} r@Role{..} = return $! r
        { rTrust  = pTrustPath
        , rPolicy = pPolicyPath
        }
      where
        Policy{..} = Profile.policy r cConfig rTrust rPolicy

    validate Role{..} = do
        checkPath rTrust  " specified by --trust must exist."
        checkPath rPolicy " specified by --policy must exist."

instance Naming Role where
    names Role{..} = unversioned rRole rEnv

commands :: Mod CommandFields Command
commands = group "profile" "Long description." $ mconcat
    [ command "info" info roleParser
        "Create or update IAM profiles."
    , command "update" update roleParser
        "Create or update IAM profiles."
    ]

info :: Common -> Role -> AWS ()
info _ r = do
    p <- Profile.find r
    log_ . Text.unlines $
        [ "Arn                  = " <> rArn p
        , "RoleId               = " <> rRoleId p
        , "RoleName             = " <> rRoleName p
        , "CreateDate           = " <> Text.pack (show $ rCreateDate p)
        , "Path                 = " <> rPath p
        , "AssumePolicyDocument = " <> (Text.decodeUtf8
            . LBS.toStrict
            . maybe "" Aeson.encodePretty
            . join
            . fmap dec
            $ rAssumeRolePolicyDocument p)
        ]
  where
    dec :: Text -> Maybe Object
    dec = decode . LBS.fromStrict . urlDecode True . Text.encodeUtf8

update :: Common -> Role -> AWS ()
update _ r = void $ Profile.update r (rPolicy r) (rTrust r)
<commit_msg>Correcting fuckup due to lack of newtype wrappers<commit_after>{-# LANGUAGE ExtendedDefaultRules       #-}
{-# LANGUAGE NoImplicitPrelude          #-}
{-# LANGUAGE OverloadedStrings          #-}
{-# LANGUAGE RecordWildCards            #-}

{-# OPTIONS_GHC -fno-warn-type-defaults #-}

-- Module      : Khan.CLI.Profile
-- Copyright   : (c) 2013 <NAME> <<EMAIL>>
-- License     : This Source Code Form is subject to the terms of
--               the Mozilla Public License, v. 2.0.
--               A copy of the MPL can be found in the LICENSE file or
--               you can obtain it at http://mozilla.org/MPL/2.0/.
-- Maintainer  : <NAME> <<EMAIL>>
-- Stability   : experimental
-- Portability : non-portable (GHC extensions)

module Khan.CLI.Profile (commands) where

import           Data.Aeson
import qualified Data.Aeson.Encode.Pretty   as Aeson
import qualified Data.ByteString.Lazy.Char8 as LBS
import qualified Data.Text                  as Text
import qualified Data.Text.Encoding         as Text
import           Khan.Internal
import           Khan.Model.Profile         (Policy(..))
import qualified Khan.Model.Profile         as Profile
import           Khan.Prelude
import           Network.AWS.IAM            hiding (Role)
import           Network.HTTP.Types         (urlDecode)

default (Text)

data Role = Role
    { rRole   :: !Text
    , rEnv    :: !Text
    , rTrust  :: !FilePath
    , rPolicy :: !FilePath
    } deriving (Show)

roleParser :: Parser Role
roleParser = Role
    <$> roleOption
    <*> envOption
    <*> trustOption
    <*> policyOption

instance Options Role where
    discover _ Common{..} r@Role{..} = return $! r
        { rTrust  = pTrustPath
        , rPolicy = pPolicyPath
        }
      where
        Policy{..} = Profile.policy r cConfig rTrust rPolicy

    validate Role{..} = do
        checkPath rTrust  " specified by --trust must exist."
        checkPath rPolicy " specified by --policy must exist."

instance Naming Role where
    names Role{..} = unversioned rRole rEnv

commands :: Mod CommandFields Command
commands = group "profile" "Long description." $ mconcat
    [ command "info" info roleParser
        "Create or update IAM profiles."
    , command "update" update roleParser
        "Create or update IAM profiles."
    ]

info :: Common -> Role -> AWS ()
info _ r = do
    p <- Profile.find r
    log_ . Text.unlines $
        [ "Arn                  = " <> rArn p
        , "RoleId               = " <> rRoleId p
        , "RoleName             = " <> rRoleName p
        , "CreateDate           = " <> Text.pack (show $ rCreateDate p)
        , "Path                 = " <> rPath p
        , "AssumePolicyDocument = " <> (Text.decodeUtf8
            . LBS.toStrict
            . maybe "" Aeson.encodePretty
            . join
            . fmap dec
            $ rAssumeRolePolicyDocument p)
        ]
  where
    dec :: Text -> Maybe Object
    dec = decode . LBS.fromStrict . urlDecode True . Text.encodeUtf8

update :: Common -> Role -> AWS ()
update _ r = void $ Profile.update r (rTrust r) (rPolicy r)

{'fraction_non_alphanumeric': 0.105297580117724, 'fraction_numerical': 0.0035971223021582736, 'mean_word_length': 1.8691369606003752, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3065251', 'n_tokens_mistral': 1835, 'n_tokens_neox': 1763, 'n_words': 545}