Universal Command Line Environment for AWS.
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
The foundational CLI execution system that provides programmatic access to AWS CLI functionality. The CLI driver manages session state, command routing, plugin loading, and serves as the primary entry point for executing AWS CLI operations programmatically.
The primary function for executing AWS CLI commands programmatically, identical to running the aws command from the command line.
def main() -> int:
"""
Main CLI entry point that creates a driver and executes commands.
Returns:
int: Exit code (0 for success, non-zero for failure)
"""Usage Example:
from awscli.clidriver import main
import sys
# Execute AWS CLI command programmatically
exit_code = main(['s3', 'ls'])
if exit_code == 0:
print("Command succeeded")
else:
print("Command failed")
sys.exit(exit_code)Factory function to create a CLI driver instance with proper session initialization and plugin loading.
def create_clidriver() -> CLIDriver:
"""
Create and configure a CLI driver instance.
Returns:
CLIDriver: Configured CLI driver ready for command execution
"""The main CLI driver class that orchestrates command execution, argument parsing, and output formatting.
class CLIDriver:
def __init__(self, session=None):
"""
Initialize CLI driver with optional botocore session.
Parameters:
session: botocore.session.Session, optional AWS session
"""
def main(self, args=None) -> int:
"""
Execute CLI command with given arguments.
Parameters:
args: list[str], optional command arguments (defaults to sys.argv[1:])
Returns:
int: Exit code (0 for success, non-zero for failure)
"""
def create_help_command(self):
"""
Create help command instance for the CLI.
Returns:
Help command instance
"""
@property
def session(self):
"""Botocore session instance for AWS service communication."""Usage Example:
from awscli.clidriver import create_clidriver
# Create driver with default session
driver = create_clidriver()
# Execute multiple commands
commands = [
['s3', 'ls'],
['ec2', 'describe-instances', '--region', 'us-east-1'],
['iam', 'list-users']
]
for cmd in commands:
exit_code = driver.main(cmd)
print(f"Command {' '.join(cmd)} completed with exit code: {exit_code}")Represents AWS service commands (e.g., s3, ec2, iam) in the command hierarchy.
class ServiceCommand(CLICommand):
def __init__(self, cli_name, session, service_name=None):
"""
Initialize service command.
Parameters:
cli_name: str, CLI name for the service
session: botocore.session.Session, AWS session
service_name: str, optional service name (defaults to cli_name)
"""
@property
def name(self) -> str:
"""Service command name."""
@property
def service_model(self):
"""Botocore service model for the service."""
@property
def lineage(self) -> list:
"""Command hierarchy lineage."""
def create_help_command(self):
"""Create service-level help command."""Represents individual operations within AWS services (e.g., s3 cp, ec2 describe-instances).
class ServiceOperation:
def __init__(self, name, parent_name, operation_caller, operation_model, session):
"""
Initialize service operation.
Parameters:
name: str, operation name
parent_name: str, parent service name
operation_caller: CLIOperationCaller, operation executor
operation_model: botocore operation model
session: botocore.session.Session, AWS session
"""
@property
def name(self) -> str:
"""Operation name."""
@property
def lineage(self) -> list:
"""Command hierarchy lineage."""
@property
def lineage_names(self) -> list[str]:
"""List of command names in hierarchy."""
@property
def arg_table(self) -> dict:
"""Argument table for operation parameters."""
def create_help_command(self):
"""Create operation-level help command."""Handles the execution of AWS service operations and response formatting.
class CLIOperationCaller:
def __init__(self, session):
"""
Initialize operation caller with AWS session.
Parameters:
session: botocore.session.Session, AWS session
"""
def invoke(self, service_name, operation_name, parameters, parsed_globals):
"""
Execute AWS service operation.
Parameters:
service_name: str, AWS service name
operation_name: str, operation name
parameters: dict, operation parameters
parsed_globals: parsed global arguments
Returns:
Operation response
"""Usage Example:
import botocore.session
from awscli.clidriver import CLIOperationCaller
# Create session and operation caller
session = botocore.session.Session()
caller = CLIOperationCaller(session)
# Execute operation directly
response = caller.invoke(
service_name='s3',
operation_name='list-objects-v2',
parameters={'Bucket': 'my-bucket'},
parsed_globals={}
)import botocore.session
from awscli.clidriver import CLIDriver
# Create custom session with specific configuration
session = botocore.session.Session()
session.set_config_variable('region', 'us-west-2')
session.set_config_variable('output', 'json')
# Create driver with custom session
driver = CLIDriver(session=session)
# Execute commands with custom configuration
exit_code = driver.main(['s3', 'ls'])from awscli.clidriver import main
from botocore.exceptions import NoCredentialsError, NoRegionError
try:
exit_code = main(['s3', 'ls'])
if exit_code != 0:
print("Command failed with non-zero exit code")
except NoCredentialsError:
print("AWS credentials not configured")
except NoRegionError:
print("AWS region not configured")
except Exception as e:
print(f"Unexpected error: {e}")Install with Tessl CLI
npx tessl i tessl/pypi-awscli