Skip to content

Commit

Permalink
Merge pull request #116 from dirkpetersen:issue-114-import-export-config
Browse files Browse the repository at this point in the history
Issue-114-import-export-config
  • Loading branch information
victormachadoperez authored Jun 28, 2024
2 parents 001fb28 + d749c1f commit e42fd13
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 4 deletions.
128 changes: 125 additions & 3 deletions froster/froster.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,74 @@ def __inquirer_check_path_exists(self, answers, current):
raise inquirer.errors.ValidationError(
"", reason="Path does not exist")
return True

def export_config(self, export_dir):
'''Export the configuration files'''

try:
#patata
# Create a ConfigParser object
config = configparser.ConfigParser()

# if exists, read the config file
if os.path.exists(self.config_file):
config.read(self.config_file)


if config.has_section('USER'):
config.remove_section('USER')

if config.has_section('UPDATE'):
config.remove_section('UPDATE')

for provider in PROVIDERS_LIST:
if config.has_section(provider):
# Get the profile before being deleted
profile = config.get(provider, 'profile')
# Remove the profile as this per-user information
config.remove_option(provider, 'profile')

# Get and set the region
exported_region = self.get_region(profile)
config.set(provider, 'exported_region', exported_region)

# Get and set the endpoint
export_endpoint = self.get_endpoint(profile)
config.set(provider, 'exported_endpoint', export_endpoint)


os.makedirs(export_dir, exist_ok=True, mode=0o775)

export_config_file = os.path.join(export_dir, 'froster_config_template.ini')

with open(export_config_file, 'w') as f:
config.write(f)

log(f'\nConfiguration file exported successfully to {export_config_file}\n')

return True
else:
log(f'Error: Config file {self.config_file} does not exist')
return False

except Exception:
print_error()
return False

def import_config(self, import_file):
try:
if os.path.exists(import_file):
shutil.copy(import_file, self.config_file)
log(f'\nConfiguration file imported successfully.\n')
return True
else:
log(f'Error: Config file {import_file} does not exist')
return False

except Exception:
print_error()
return False


def print_config(self):
'''Print the configuration files'''
Expand Down Expand Up @@ -616,6 +684,9 @@ def set_region(self, aws: 'AWSBoto'):
# Update region in the config file
self.__set_aws_config(profile_name=self.profile, region=region)

# Remove the exported_region from the config object if it exists
self.__remove_config_option(self.provider, 'exported_region')

return True

except Exception:
Expand Down Expand Up @@ -759,7 +830,12 @@ def get_region(self, profile):
'''Get the AWS region for the given profile'''

try:
return self.get_aws_config_option(profile, 'region')
# Check if there is an exported region
exported_region = self.__get_configuration_entry(self.provider, 'exported_region', None)
if exported_region:
return exported_region
else:
return self.get_aws_config_option(profile, 'region')

except Exception:
print_error()
Expand All @@ -770,7 +846,14 @@ def get_endpoint(self, profile):
'''Get the endpoint_url for the given profile'''

try:
return self.get_aws_config_option(profile, 's3.endpoint_url')
# Check if there is an exported endpoint
exported_endpoint = self.__get_configuration_entry(
self.provider, 'exported_endpoint', None)

if exported_endpoint:
return exported_endpoint
else:
return self.get_aws_config_option(profile, 's3.endpoint_url')

except Exception:
print_error()
Expand Down Expand Up @@ -807,6 +890,27 @@ def get_credential(self, profile, key_name):
print_error()
return None

def __remove_config_option(self, section, option):
'''Remove a configuration option in the config file'''

try:
# Create a ConfigParser object
config = configparser.ConfigParser()

# if exists, read the config file
if os.path.exists(self.config_file):
config.read(self.config_file)

# Remove the option
if config.has_option(self.provider, option):
config.remove_option(section, option)

# Write the config object to the config file
with open(self.config_file, 'w') as f:
config.write(f)

except Exception:
print_error()
def __set_configuration_entry(self, section, key, value):
'''Set a configuration entry in the config file'''

Expand Down Expand Up @@ -922,7 +1026,7 @@ def set_endpoint(self):
# Get the user answer
endpoint = inquirer.text(
message=f'Enter the {self.provider} endpoint',
default=self.__get_configuration_entry(self.provider, 'endpoint'),
default=self.get_endpoint(self.profile),
validate=self.__inquirer_check_required)

# Ensure the endpoint starts with "https://" for IDrive
Expand All @@ -936,6 +1040,9 @@ def set_endpoint(self):
# Manually setting the endpoint
self.endpoint = endpoint

# Remove the exported_region from the config object if it exists
self.__remove_config_option(self.provider, 'exported_endpoint')

return True

except Exception:
Expand Down Expand Up @@ -6254,10 +6361,19 @@ def subcmd_config(self, cfg: ConfigManager, aws: AWSBoto):
try:
if self.args.print:
return cfg.print_config()

if self.args.import_config:
return cfg.import_config(import_file=self.args.import_config)

if self.args.export_config:
return cfg.export_config(export_dir=self.args.export_config)

if self.args.reset:
if os.path.exists(cfg.config_file):
os.remove(cfg.config_file)
log(f'\nConfiguration file removed: {cfg.config_file}\n')
return True


log(f'\n*****************************')
log(f'*** FROSTER CONFIGURATION ***')
Expand Down Expand Up @@ -6703,6 +6819,12 @@ def parse_arguments(self):

parser_config.add_argument('-r', '--reset', dest='reset', action='store_true',
help="Delete the current configuration and start over")

parser_config.add_argument('-i', '--import', dest='import_config', action='store', default='',
help="Import a given configuration file")

parser_config.add_argument('-e', '--export', dest='export_config', action='store', default='',
help="Export the current configuration to the given directory")

# ***

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"

[tool.poetry]
name = "froster"
version = "0.15.1"
version = "0.15.2"
description = "Froster is a tool for easy data transfer between local file systems and AWS S3 storage."
authors = ["Victor Machado <[email protected]>"]
readme = "README.md"
Expand Down

0 comments on commit e42fd13

Please sign in to comment.