Skip to content

Commit

Permalink
Added --profile-name option to ksm profile import command (#478)
Browse files Browse the repository at this point in the history
  • Loading branch information
idimov-keeper authored Sep 15, 2023
1 parent bb6a1d6 commit e95a1ee
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -489,14 +489,16 @@ def profile_export_command(ctx, plain, file_format, profile_name):
cls=HelpColorsCommand,
help_options_color='blue'
)
@click.option('--profile-name', '-p', type=str, required=False, help='Config profile to import into.')
@click.option('--output-file', '-f', type=str, required=False, help='Create the config in a specific file location.')
@click.argument('config-base64', type=str, required=True, nargs=1)
@click.pass_context
def profile_import_command(ctx, output_file, config_base64):
def profile_import_command(ctx, profile_name, output_file, config_base64):
"""Import an encrypted config file"""
Profile(cli=ctx.obj["cli"], config=global_config).import_config(
file=output_file,
config_base64=config_base64,
file=output_file,
profile_name=profile_name,
launched_from_app=global_config.launched_from_app
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,9 +372,7 @@ def import_config(config_base64, file=None, profile_name=None, launched_from_app

"""
Take base64 config file and write it back to disk.
This file could be a JSON or a Keeper ini file.
"""

config_data = base64.urlsafe_b64decode(config_base64.encode())
Expand All @@ -392,15 +390,26 @@ def import_config(config_base64, file=None, profile_name=None, launched_from_app

# If a JSON file was import, convert the JSON to a INI.
if is_json is True:
if profile_name is None:
profile_name = os.environ.get("KSM_CLI_PROFILE", Profile.default_profile)
config = Config(ini_file=file)

try:
# If the file exists attempt to load as INI to merge new config
config.load()
except:
pass # doesn't exists, inaccessible, or not INI: create new

config.set_profile_using_base64(
profile_name=Profile.default_profile,
profile_name=profile_name,
base64_config=config_base64
)
config.save()

# Else just save the INI. It's in the right format, just save it. No processing needed.
else:
if profile_name:
print("Ignored option --profile-name as incompaible with INI file format that can handle multiple profiles.")
with open(file, "w") as fh:
fh.write(config_data.decode())
fh.close()
Expand Down

0 comments on commit e95a1ee

Please sign in to comment.