def export_command(ctx): u = Utility() if u.is_init(): helper_ = helper.Helper() content = helper_.read_file("state") profile_config = helper_.read_file(f"{content['profile']}.prof") aws_profile = list(profile_config['credentials_profile'].keys()) if content is None: print("You dont have a profile set in your state file. Choose a profile with `choose` command") exit(1) else: print(f"export AWS_PROFILE={aws_profile[0]}") else: print("You need to initialize first")
def show_command(ctx, all): u = Utility() if u.is_init(): helper_ = helper.Helper() content = helper_.read_file("state") if content is None: print("You dont have a profile set in your state file. Choose a profile with `choose` command") exit(1) else: ## Change below line. ConfigSetup is no longer in use profile_config = helper_.read_file(f"{content['profile']}.prof") aws_profile = list(profile_config['credentials_profile'].keys())[0] sections=u.show_aws_profiles(profile_config, all, aws_profile) else: print("You need to initialize first")
def whoami_command(ctx): u = Utility() if u.is_init(): with open("{}/state".format(APPLICATION_HOME_DIR)) as f: content = yaml.load(f, Loader=yaml.FullLoader) if content is None: print("Jaqen H'ghar, is that you ?") print("A girl is No One") print( "Choose a profile with aptly named command `choose` or configure one with another aplty named command `configure`" ) else: if content.get('profile'): print('Your current profile is : {}'.format( content['profile'])) else: print("You need to initialize first.")
def clean_command(ctx): u = Utility() if u.is_init(): helper_ = helper.Helper() content = helper_.read_file("state") if content is None: print( "You dont have a profile set in your state file. Choose a profile with `choose` command" ) exit(1) else: if content.get("profile"): profile_config = helper_.read_file(content["profile"]) parsers = { "config": profile_config["config"], "credentials": profile_config["credentials"] } paths = { "config": expanduser(profile_config["config"]), "credentials": expanduser(profile_config["credentials"]) } u.clean_sections(parsers, paths)
def generate_credentials_command(ctx): u = Utility() if u.is_init(): helper_ = helper.Helper() content = helper_.read_file("state") creds = u.get_credentials(content['user']) profile_config = helper_.read_file("{}.prof".format( content['profile'])) u.create_section(profile_config, content['user'], creds) else: print("You need to initialize first.")
def initialize_command(ctx): u = Utility() if os.path.isdir(APPLICATION_HOME_DIR): directory = True if os.path.exists(f"{APPLICATION_HOME_DIR}/state"): state = True else: state = False else: directory = False if directory and state: print("Good news, `asm` is already initialized !") if not directory: u.create_directory(APPLICATION_HOME_DIR) u.create_file(APPLICATION_HOME_DIR, "state") state = True print("Initialization was successful..") if not state: u.create_file("state")
def choose_command(ctx, profile, user, role): u = Utility() if u.is_init(): helper_ = helper.Helper() existing_profiles = helper_.get_profiles() while profile not in existing_profiles: profile = u.pick_from_list_of("profile", existing_profiles) details = helper_.read_file("{}.prof".format(profile)) users = details.get('credentials_profile', None) if users is not None: users = list(users.keys()) else: print("Malformed state file. The profile has no users") exit(1) user = u.pick_from_list_of("user", users) roles = details.get('credentials_profile', None).get(user, None) if roles is not None: roles = list(roles.keys()) else: print("Malformed state file. The profile has no roles") exit(1) role = u.pick_from_list_of("role", roles) info = { 'profile': profile, 'user': user, 'role': role, 'account': details['credentials_profile'][user][role] } helper_.write_file('state', info) else: print("You need to initialize first.")
def configure_command(ctx, set, get, add_profile, delete_profile): helper_ = helper.Helper() u = Utility() content = helper_.read_file("state") if u.is_init(): print(content) if content is not None: if content.get("profile"): profile_config = helper_.read_file("{}.prof".format( content["profile"])) if set: key, value = set.split('=') if key.strip() in profile_config.keys(): profile_config[key.strip()] = value.strip() helper_.write_file("{}.prof".format( content["profile"])) elif get: if 'state' in get.strip(): u.print_message(helper_.read_file('state')) state = helper_.read_file('state') for k, v in state.items(): u.print_message("{}: {}".format(k, v)) elif get in profile_config: u.print_message("The value for {} is {}".format( get, profile_config[get])) else: u.print_message( "There is no attribute : {}".format(get)) elif add_profile: user, role_and_account = add_profile.split(".") role, account = role_and_account.strip().split(':') if user.strip() in profile_config['credentials_profile']: profile_config['credentials_profile'][ user.strip()].update( {role.strip(): account.strip()}) helper_.write_file( "{}.prof".format(content["profile"]), profile_config) u.print_message( "New role `{}` added with account number `{}` for user `{}`" .format(role, account, user)) else: profile_config['credentials_profile'][user.strip()] = { role.strip(): account.strip() } helper_.write_file( "{}.prof".format(content["profile"]), profile_config) u.print_message( "New user added `{}` with role `{}` and account number `{}`" .format(user, role, account)) elif delete_profile: if "." in delete_profile: user, role = delete_profile.split(".") del profile_config['credentials_profile'][ user.strip()][role.strip()] helper_.write_file( "{}.prof".format(content["profile"]), profile_config) else: del profile_config['credentials_profile'][ delete_profile.strip()] helper_.write_file( "{}.prof".format(content["profile"]), profile_config) else: conf = u.configure() conf.create_config(conf.config) else: print("You need to initialize first.")