Handle error when no passwords

This commit is contained in:
Antonio J. Delgado 2025-01-24 11:15:40 +02:00
parent c90144b20e
commit 74338be3e1

View file

@ -112,6 +112,7 @@ class NextcloudHandler:
self._log = params['logger']
else:
self._init_log(params)
self.params = params
self.http = 'https'
self.timeout = params.get('timeout', 3)
self.ssl = True
@ -391,6 +392,10 @@ class NextcloudHandler:
def debug(self, obj, unsafe=False):
'''Show debug information'''
obj['connection'] = {
"host": self.params['host'],
"user": self.params['user']
}
self._log.debug(
self._output(obj, unsafe=unsafe)
)
@ -444,7 +449,7 @@ class NextcloudHandler:
def get(self, path):
'''Do a GET request'''
self.debug({ "action": "get", "message": f"Requesting {path}" })
self.debug({ "action": "get", "message": f"Requesting '{self.http}://{self.host}/{path}'" })
try:
r = self.session.get(
f'{self.http}://{self.host}/{path}',
@ -784,6 +789,13 @@ class NextcloudHandler:
return folder
return False
def _is_uuid(self, string):
'''Test if a string is a UUID'''
match = re.match(r'^[0-9a-f\-]*$', string)
if match:
return True
return False
def create_passwords_folder(self, name):
'''Create passwords folder'''
if not self.exists_passwords_folder(name):
@ -903,7 +915,10 @@ class NextcloudHandler:
}
)
if new_obj.get('folder', '') != '':
folder_id = self.get_folder_id(new_obj['folder'])
if self._is_uuid(new_obj['folder']):
folder_id = new_obj['folder']
else:
folder_id = self.get_folder_id(new_obj['folder'])
if not folder_id:
created_folder = self.create_passwords_folder(new_obj['folder'])
if created_folder:
@ -1636,8 +1651,12 @@ class NcPasswordClient:
"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)
all_passwords = self.nc.list_passwords()
if all_passwords:
for password in all_passwords:
self.destination_nc.create_password(password, update=True)
else:
return False
return True
def _init_log(self):