in ec2instanceconnectcli/input_parser.py [0:0]
def parseargs(args, mode='ssh'):
"""
Parses the input arguments to one of the EC2 Instance Connect CLI commands and splits it into pieces that will be
used as appropriate at invocation time.
All commands (ssh, sftp,) follow the same basic structure of
protocol [ec2 connect flags] [command flags] [target] [command or files]
The first two are free - protocol is the mode, ec2 connect flags we get from argparse in args[0]
The rest we need to extract from args[1]
What makes this particularly interesting is that the data we need varies by command.
- For ssh, we need the user (if present), host (required), and command (if present)
- For sftp, we need the user (if present), host (required), and dir or files (if present)
To match this, we have a structure that may contain a given target's instance ID, target DNS/IP/etc,
username to use for connection, EC2 availability zone, and/or a file associated with that host in the given command.
There will be either one or two depending on what mode we're parsing for.
The command field is then used to store either the ssh command or additional files passed to sftp.
:param args: A tuple of known arguments and list of string with unknown arguments
:type args: tuple
:param mode: The protocol we will be using (ssh, sftp, potentially others in-future)
:type mode: basestring
:return: Args split into three pieces: EC2 instance information, command flags, and and the actual command to run
:rtype: tuple
"""
if len(args) < 2:
raise AssertionError('Missing target')
custom_flags = args[1]
_validate_custom_flags(custom_flags)
"""
Our flags. As these are via argparse they're free.
Instance details are a bit weird. Since the instance ID can either be the actual "host" or a flag we have to group it.
We do this with an "instance bundle" dict.
Note we don't load the actual instance DNS/IP/ID here - that comes later.
"""
instance_bundles = [
{
'profile': args[0].profile,
'instance_id': args[0].instance_id,
'region': args[0].region,
'zone': args[0].zone
}
]
# We do this as an array to support future commands that may need multiple instances (eg, scp)
# Process out the command flags & target data
flags, command, instance_bundles = _parse_command_flags(custom_flags, instance_bundles, is_ssh=(mode=='ssh'))
# Process the instance & target data to give us an actual picture of what end hosts we're working with
instance_bundles = _parse_instance_bundles(instance_bundles)
#validate instance_bundles
_validate_instance_bundles(instance_bundles, mode)
return instance_bundles, flags, command