diff --git a/nc_password_client/nc_password_client.py b/nc_password_client/nc_password_client.py index 2355f08..377dcb7 100755 --- a/nc_password_client/nc_password_client.py +++ b/nc_password_client/nc_password_client.py @@ -1214,12 +1214,17 @@ class NcPasswordClient: 'nc_password_client.log' ) self._init_log() + self.timeout = timeout + self.cache_duration = cache_duration + self.https_proxy = https_proxy + self.output_format = output_format + self.field_replacements = field_replacements params = { "host": host, "user": user, "api_token": api_token, "cse_password": cse_password, - "timeout": timeout, + "timeout": self.timeout, "cache_duration": cache_duration, "https_proxy": https_proxy, "logger": self._log, @@ -1227,6 +1232,7 @@ class NcPasswordClient: "field_replacements": field_replacements, } self.nc = NextcloudHandler(params) + self.destination_nc = None def _safer_obj(self, obj, fields=None): if fields is None: @@ -1609,6 +1615,31 @@ class NcPasswordClient: ) return True + def migrate_passwords( + self, + destination_host, + destination_user, + destination_api_token, + destination_cse_password + ): + '''Migrate passwords from one NextCloud instance to another''' + params = { + "host": destination_host, + "user": destination_user, + "api_token": destination_api_token, + "cse_password": destination_cse_password, + "timeout": self.timeout, + "cache_duration": self.cache_duration, + "https_proxy": self.https_proxy, + "logger": self._log, + "output_format": self.output_format, + "field_replacements": self.field_replacements, + } + self.destination_nc = NextcloudHandler(params) + for password in self.nc.list_passwords(): + self.destination_nc.create_password(password, update=True) + return True + def _init_log(self): ''' Initialize log object ''' self._log = logging.getLogger("nc_password_client") @@ -1707,6 +1738,37 @@ def cli( timeout, cache_duration, https_proxy, output_format, field_replacements ) +@cli.command() +@click.option( + '--destination-host', '-D', + required=True, + help='Destination host' +) +@click.option( + '--destination-user', '-U', + required=True, + help='Destination host user name' +) +@click.option( + '--destination-api-token', '-A', + required=True, + help="Destination host user's API token " +) +@click.option( + '--destination-cse-password', '-C', + help='Destination Nextcloud host user\'s end-to-end encryption password' +) +@click_config_file.configuration_option() +@click.pass_context +def migrate_passwords(ctx, destination_host, destination_user, destination_api_token, destination_cse_password): + '''Migrate passwords between two NextCloud Password instances''' + ctx.obj['NcPasswordClient'].migrate_passwords( + destination_host, + destination_user, + destination_api_token, + destination_cse_password + ) + @cli.command() @click.option('--name', '-n', required=True, help='Name of the password to show') @click.option(