Skip to content
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 28 additions & 20 deletions avm/bin/avm
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,49 @@ import argparse
import inquirer
import configparser
from csv import DictReader
from sys import argv
from os import environ
from os.path import expanduser, isfile
import csv

config = configparser.ConfigParser()
home = expanduser('~')
home_creds = home + '/.aws/credentials'
config.read(home_creds)

home_credentials = home + '/.aws/credentials'
credentials = configparser.ConfigParser()
credentials.read(home_credentials)

home_config = home + '/.aws/config'
config = configparser.ConfigParser()
config.read(home_config)

profiles = [
inquirer.List(
'profile',
message="Select a profile to set as default",
choices=config.sections(),
choices=list(set(credentials.sections() + config.sections())),
),
]


def magic(new_profile_name):
if new_profile_name in config:
new_profile_name_default = config[str(new_profile_name)]
config['default'] = new_profile_name_default
with open(home_creds, 'w') as configfile:
config.write(configfile)
print(f'AWS profile `{new_profile_name}` has been set as default')
else:
found_flag = False
for parser, file_path in zip([credentials, config],
[home_credentials, home_config]):
if new_profile_name in parser:
found_flag = True
new_profile_name_default = parser[str(new_profile_name)]
parser['default'] = new_profile_name_default
with open(file_path, 'w') as parser_file:
parser.write(parser_file)
print(f'AWS profile `{new_profile_name}` has been set as default in `{file_path}`')

if not found_flag:
print(f'AWS profile `{new_profile_name}` not found')
exit(1)


parser = argparse.ArgumentParser()
parser.add_argument("--add",required=False, action='store_true')
parser.add_argument("-f","--credentials-file",required=False)
parser.add_argument("-a","--alias",required=False)
parser.add_argument("--use",required=False)
parser.add_argument("--add", required=False, action='store_true')
parser.add_argument("-f", "--credentials-file", required=False)
parser.add_argument("-a", "--alias", required=False)
parser.add_argument("--use", required=False)
args = parser.parse_args()

if args.add:
Expand All @@ -49,7 +57,7 @@ if args.add:
config.add_section(args.alias)
config[args.alias]["aws_access_key_id"] = row['Access key ID']
config[args.alias]["aws_secret_access_key"] = row['Secret access key']
with open(home_creds, 'w') as configfile:
with open(home_credentials, 'w') as configfile:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe you forgot to write into /.aws/config here.

Copy link
Author

@kmendler kmendler Oct 13, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to add to ~/.aws/config if ~/.aws/credentials holds the configuration details, but I think it would be better practise if the fields were split between the two files. Is that what you were thinking? So all fields except aws_access_key_id and aws_secret_access_key would go into ~/.aws/config?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about it more and it doesn't seem like we would want any other fields written from the .csv file except those two... I've updated the variable names to make it more clear that only the ~/.aws/credentials file is being manipulated with the --add option.

config.write(configfile)
else:
exit("No alias was provided")
Expand All @@ -58,4 +66,4 @@ elif args.use:
magic(new_profile_name)
else:
selected_new_profile_name = inquirer.prompt(profiles)
magic(selected_new_profile_name['profile'])
magic(selected_new_profile_name['profile'])