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

-- Module      : Main
-- 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 Main (main) where

import           Control.Arrow            (second)
import           Control.Error
import           Control.Monad
import qualified Data.HashMap.Strict      as Map
import           Data.List                (isPrefixOf)
import qualified Data.Text                as Text
import qualified Khan.CLI.Ansible         as Ansible
import qualified Khan.CLI.Artifact        as Artifact
import qualified Khan.CLI.Cluster         as Cluster
import qualified Khan.CLI.DNS             as DNS
import qualified Khan.CLI.Group           as Group
import qualified Khan.CLI.Image           as Image
import qualified Khan.CLI.Launch          as Launch
import qualified Khan.CLI.Metadata        as Metadata
import qualified Khan.CLI.Profile         as Profile
import qualified Khan.CLI.Routing         as Routing
import qualified Khan.CLI.SSH             as SSH
import           Khan.Internal
import           Khan.Prelude
import           Network.AWS
import qualified Network.AWS.EC2.Metadata as Meta
import           Options.Applicative      (info)
import           System.Environment

main :: IO ()
main = runScript $ do
    (n, as, es) <- scriptIO $
        (,,) <$> getProgName
             <*> getArgs
             <*> getEnvironment

    ec2 <- Meta.ec2
    mr  <- regionalise ec2
    cmd <- either (\e -> scriptIO (errMessage e n) >>= left)
                  right
                  (execParser as es mr)

    fmapLT format $ run ec2 cmd
  where
    regionalise False = return Nothing
    regionalise True  = do
        t  <- Text.unpack <$> meta AvailabilityZone
        az <- tryRead ("Failed to read Availability Zone from: " ++ t) t
        return $! Just $ azRegion az

    format (Err msg) = msg
    format ex        = show ex

    run ec2 (c@Common{..}, Command f x) = do
        unless cSilent enableLogging
        validate c
        rs <- contextAWS c $ do
            debug "Running in region {}..." [show cRegion]
            y <- discover ec2 c x
            liftEitherT $ validate y
            f c y
        hoistEither rs

execParser :: [String]
           -> [(String, String)]
           -> Maybe Region
           -> Either ParserFailure (Common, Command)
execParser as es mr =
    execParserPure (prefs showHelpOnError) (info parser idm) merged<commit_msg>Preserving optparse-applciative exitCode<commit_after>{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards   #-}

-- Module      : Main
-- 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 Main (main) where

import           Control.Arrow            (second)
import           Control.Error
import           Control.Monad
import qualified Data.HashMap.Strict      as Map
import           Data.List                (isPrefixOf)
import qualified Data.Text                as Text
import qualified Khan.CLI.Ansible         as Ansible
import qualified Khan.CLI.Artifact        as Artifact
import qualified Khan.CLI.Cluster         as Cluster
import qualified Khan.CLI.DNS             as DNS
import qualified Khan.CLI.Group           as Group
import qualified Khan.CLI.Image           as Image
import qualified Khan.CLI.Launch          as Launch
import qualified Khan.CLI.Metadata        as Metadata
import qualified Khan.CLI.Profile         as Profile
import qualified Khan.CLI.Routing         as Routing
import qualified Khan.CLI.SSH             as SSH
import           Khan.Internal
import           Khan.Prelude
import           Network.AWS
import qualified Network.AWS.EC2.Metadata as Meta
import           Options.Applicative      (info)
import           System.Environment
import           System.Exit              (exitWith)
import           System.IO                (hPutStrLn, stderr)

main :: IO ()
main = runScript $ do
    (as, es) <- scriptIO $
        (,) <$> getArgs
            <*> getEnvironment

    ec2 <- Meta.ec2
    mr  <- regionalise ec2
    cmd <- either failure return $ execParser as es mr

    fmapLT format $ run ec2 cmd
  where
    failure ParserFailure{..} = liftIO $ getProgName
        >>= errMessage
        >>= hPutStrLn stderr
        >>  exitWith errExitCode

    regionalise False = return Nothing
    regionalise True  = do
        t  <- Text.unpack <$> meta AvailabilityZone
        az <- tryRead ("Failed to read Availability Zone from: " ++ t) t
        return $! Just $ azRegion az

    format (Err msg) = msg
    format ex        = show ex

    run ec2 (c@Common{..}, Command f x) = do
        unless cSilent enableLogging
        validate c
        rs <- contextAWS c $ do
            debug "Running in region {}..." [show cRegion]
            y <- discover ec2 c x
            liftEitherT $ validate y
            f c y
        hoistEither rs

execParser :: [String]
           -> [(String, String)]
           -> Maybe Region
           -> Either ParserFailure (Common, Command)
execParser as es mr =
    execParserPure (prefs showHelpOnError) (info parser idm) merged
{'fraction_non_alphanumeric': 0.08189728715236308, 'fraction_numerical': 0.0051185804470226925, 'mean_word_length': 1.8679060665362035, 'pattern_counts': {'":': 0, '<': 38, '<?xml version=': 0, '>': 37, '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': '4347414', 'n_tokens_mistral': 1668, 'n_tokens_neox': 1609, 'n_words': 568}