Skip to content

Commit

Permalink
create binaries for region and profile switching
Browse files Browse the repository at this point in the history
  • Loading branch information
snigdhasjg committed Nov 28, 2023
1 parent d1b1777 commit 3c63345
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 7 deletions.
26 changes: 19 additions & 7 deletions aws_fusion/commands/config_switch.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import inquirer
import boto3
import os
import sys


def setup(subparsers, parent_parser):
Expand Down Expand Up @@ -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):
Expand All @@ -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)
16 changes: 16 additions & 0 deletions bin/_awsp
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions bin/_awsr
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 3c63345

Please sign in to comment.