diff --git a/aws_fusion/commands/config_switch.py b/aws_fusion/commands/config_switch.py index 68f3fd5..6c1418d 100644 --- a/aws_fusion/commands/config_switch.py +++ b/aws_fusion/commands/config_switch.py @@ -1,7 +1,6 @@ import inquirer import boto3 import os -import sys def setup(subparsers, parent_parser): @@ -34,9 +33,9 @@ def switch_profile(args): ] answers = inquirer.prompt(questions, theme=inquirer.themes.GreenPassion()) - profile = answers.get('profile') - command = '$env:' if sys.platform == 'win32' else 'export ' - print(f'{command}AWS_PROFILE="{profile}"') + + profile = answers.get('profile') if answers.get('profile') != 'default' else None + __update_file('profile', profile) def switch_region(args): @@ -54,6 +53,19 @@ def switch_region(args): ] answers = inquirer.prompt(questions, theme=inquirer.themes.GreenPassion()) - region = answers.get('region') - command = '$env:' if sys.platform == 'win32' else 'export ' - print(f'{command}AWS_REGION="{region}"') + + region = answers.get('region') if answers.get('region') != session.region_name else None + __update_file('region', region) + + +def __update_file(file_name, value): + config_dir = os.path.expanduser(os.path.join('~', '.aws', 'fusion')) + if not os.path.isdir(config_dir): + os.makedirs(config_dir) + full_key = os.path.join(config_dir, file_name) + with os.fdopen( + os.open(full_key, os.O_WRONLY | os.O_CREAT, 0o600), 'w' + ) as f: + f.truncate() + if value is not None: + f.write(value) diff --git a/bin/_awsp b/bin/_awsp new file mode 100644 index 0000000..292c8f6 --- /dev/null +++ b/bin/_awsp @@ -0,0 +1,16 @@ +#!/bin/bash + +aws-fusion config-switch profile + +selected_profile="$(cat ~/.aws/fusion/profile)" + +# Unset region as switching profile +unset AWS_REGION + +if [ -z "$selected_profile" ] +then + # Unset profile for default profile + unset AWS_PROFILE +else + export AWS_PROFILE="$selected_profile" +fi \ No newline at end of file diff --git a/bin/_awsr b/bin/_awsr new file mode 100644 index 0000000..eb26762 --- /dev/null +++ b/bin/_awsr @@ -0,0 +1,13 @@ +#!/bin/bash + +aws-fusion config-switch region + +selected_region="$(cat ~/.aws/fusion/region)" + +if [ -z "$selected_region" ] +then + # Unset region as it is same one mentioned in the current profile + unset AWS_REGION +else + export AWS_REGION="$selected_region" +fi \ No newline at end of file diff --git a/setup.py b/setup.py index 416b87a..71efcff 100755 --- a/setup.py +++ b/setup.py @@ -39,6 +39,10 @@ def find_version(*file_paths): 'aws-fusion = aws_fusion.app:main', ] }, + scripts=[ + 'bin/_awsp', + 'bin/_awsr' + ], install_requires=[ 'boto3>=1.29', 'pyperclip>=1.8',