Use convert_to_json on every function

This commit is contained in:
Orsiris de Jong 2024-01-04 02:03:36 +01:00
parent 06aa5a07cb
commit 950b86a52e

View file

@ -629,7 +629,11 @@ class ResticRunner:
cmd = "list {}".format(subject) cmd = "list {}".format(subject)
result, output = self.executor(cmd) result, output = self.executor(cmd)
return self.convert_to_json_output(result, output, **kwargs) if result:
msg = f"Successfully listed {subject} objects"
else:
msg = f"Failed to list {subject} objects:\n{output}"
return self.convert_to_json_output(result, output, msg=msg, **kwargs)
@check_if_init @check_if_init
@ -649,7 +653,11 @@ class ResticRunner:
cmd = "ls {}".format(snapshot) cmd = "ls {}".format(snapshot)
result, output = self.executor(cmd) result, output = self.executor(cmd)
return self.convert_to_json_output(result, output, **kwargs) if result:
msg = f"Successfuly listed snapshot {snapshot} content"
else:
msg = f"Could not list snapshot {snapshot} content:\n{output}"
return self.convert_to_json_output(result, output, msg=msg, **kwargs)
@check_if_init @check_if_init
@ -663,7 +671,11 @@ class ResticRunner:
cmd = "snapshots" cmd = "snapshots"
result, output = self.executor(cmd) result, output = self.executor(cmd)
return self.convert_to_json_output(result, output, **kwargs) if result:
msg = "Snapshots listed successfully"
else:
msg = f"Could not list snapshots:n{output}"
return self.convert_to_json_output(result, output, msg=msg, **kwargs)
@check_if_init @check_if_init
def backup( def backup(
@ -778,13 +790,13 @@ class ResticRunner:
) )
result, output = self.executor(cmd.replace(" --use-fs-snapshot", "")) result, output = self.executor(cmd.replace(" --use-fs-snapshot", ""))
if self.json_output:
return self.convert_to_json_output(result, output, **kwargs)
if result: if result:
self.write_logs("Backend finished backup with success", level="info") msg = "Backend finished backup with success"
return output else:
self.write_logs("Backup failed backup operation", level="error") msg = f"Backup failed backup operation:\n{output}"
return False # For backups, we need to return the result string restic too, for metrics analysis
result = self.convert_to_json_output(result, output, msg=msg, **kwargs)
return result, output
@check_if_init @check_if_init
def find(self, path: str) -> Union[bool, str, dict]: def find(self, path: str) -> Union[bool, str, dict]:
@ -797,7 +809,11 @@ class ResticRunner:
cmd = f'find "{path}"' cmd = f'find "{path}"'
result, output = self.executor(cmd) result, output = self.executor(cmd)
return self.convert_to_json_output(result, output, **kwargs) if result:
msg = "Find command succeed"
else:
msg = f"Could not find path {path}:\n{output}"
return self.convert_to_json_output(result, output, msg=msg, **kwargs)
@check_if_init @check_if_init
@ -818,13 +834,12 @@ class ResticRunner:
if include: if include:
cmd += ' --{}include "{}"'.format(case_ignore_param, include) cmd += ' --{}include "{}"'.format(case_ignore_param, include)
result, output = self.executor(cmd) result, output = self.executor(cmd)
if self.json_output:
return self.convert_to_json_output(result, output, **kwargs)
if result: if result:
self.write_logs("successfully restored data.", level="info") msg = "successfully restored data"
return True else:
self.write_logs(f"Data not restored: {output}", level="info") msg = f"Data not restored:\n{output}"
return False return self.convert_to_json_output(result, output, msg=msg, **kwargs)
@check_if_init @check_if_init
def forget( def forget(
@ -936,13 +951,11 @@ class ResticRunner:
return False return False
cmd = f"repair {subject}" cmd = f"repair {subject}"
result, output = self.executor(cmd) result, output = self.executor(cmd)
if self.json_output:
return self.convert_to_json_output(result, output, **kwargs)
if result: if result:
self.write_logs(f"Repo successfully repaired:\n{output}", level="info") msg = f"Repo successfully repaired:\n{output}"
return True else:
self.write_logs(f"Repo repair failed:\n {output}", level="critical") msg = f"Repo repair failed:\n{output}"
return False return self.convert_to_json_output(result, output, msg=msg, **kwargs)
@check_if_init @check_if_init
def unlock(self) -> Union[bool, str, dict]: def unlock(self) -> Union[bool, str, dict]:
@ -954,13 +967,11 @@ class ResticRunner:
cmd = f"unlock" cmd = f"unlock"
result, output = self.executor(cmd) result, output = self.executor(cmd)
if self.json_output:
return self.convert_to_json_output(result, output, **kwargs)
if result: if result:
self.write_logs(f"Repo successfully unlocked", level="info") msg = f"Repo successfully unlocked"
return True else:
self.write_logs(f"Repo unlock failed:\n {output}", level="critical") msg = f"Repo unlock failed:\n{output}"
return False return self.convert_to_json_output(result, output, msg=msg, **kwargs)
@check_if_init @check_if_init
def dump(self, path: str) -> Union[bool, str, dict]: def dump(self, path: str) -> Union[bool, str, dict]:
@ -1003,13 +1014,11 @@ class ResticRunner:
kwargs.pop("self") kwargs.pop("self")
result, output = self.executor(command) result, output = self.executor(command)
if self.json_output:
return self.convert_to_json_output(result, output, **kwargs)
if result: if result:
self.write_logs(f"successfully run raw command:\n{output}", level="info") msg = f"successfully run raw command:\n{output}"
return True, output else:
self.write_logs("Raw command failed.", level="error") msg = "Raw command failed:\n{output}"
return False, output return self.convert_to_json_output(result, output, msg=msg, **kwargs)
@staticmethod @staticmethod
def _has_recent_snapshot( def _has_recent_snapshot(