summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Rajnoha <prajnoha@redhat.com>2016-05-02 14:22:02 +0200
committerPeter Rajnoha <prajnoha@redhat.com>2016-06-02 14:08:11 +0200
commit59b2815f52f27d15976158ed5fec74c62cd66b72 (patch)
tree878be8018f3611519b6c63e38318b6b245aad4c1
parenta471cac2b6dcc9e5d1f34196e58fc8e0d5d91ad4 (diff)
downloadlvm2-59b2815f52f27d15976158ed5fec74c62cd66b72.tar.gz
toollib: add report_format_init fn to create report group and to create/add status report handle
-rw-r--r--lib/report/report.h3
-rw-r--r--tools/args.h1
-rw-r--r--tools/reporter.c69
3 files changed, 73 insertions, 0 deletions
diff --git a/lib/report/report.h b/lib/report/report.h
index 90fb6d8a3..cff4a5bbe 100644
--- a/lib/report/report.h
+++ b/lib/report/report.h
@@ -77,6 +77,9 @@ struct processing_handle;
typedef int (*field_report_fn) (struct report_handle * dh, struct field * field,
const void *data);
+int report_format_init(struct cmd_context *cmd, struct dm_report_group **report_group,
+ struct dm_report **status_rh);
+
void *report_init(struct cmd_context *cmd, const char *format, const char *keys,
report_type_t *report_type, const char *separator,
int aligned, int buffered, int headings, int field_prefixes,
diff --git a/tools/args.h b/tools/args.h
index ef9dbc951..049ea5a72 100644
--- a/tools/args.h
+++ b/tools/args.h
@@ -96,6 +96,7 @@ arg(refresh_ARG, '\0', "refresh", NULL, 0)
arg(removemissing_ARG, '\0', "removemissing", NULL, 0)
arg(repair_ARG, '\0', "repair", NULL, 0)
arg(replace_ARG, '\0', "replace", string_arg, ARG_GROUPABLE)
+arg(reportformat_ARG, '\0', "reportformat", string_arg, 0)
arg(restorefile_ARG, '\0', "restorefile", string_arg, 0)
arg(restoremissing_ARG, '\0', "restoremissing", NULL, 0)
arg(resync_ARG, '\0', "resync", NULL, 0)
diff --git a/tools/reporter.c b/tools/reporter.c
index 324bc7614..8a5974709 100644
--- a/tools/reporter.c
+++ b/tools/reporter.c
@@ -1061,3 +1061,72 @@ int devtypes(struct cmd_context *cmd, int argc, char **argv)
{
return _report(cmd, argc, argv, DEVTYPES);
}
+
+#define REPORT_FORMAT_NAME_NATIVE "native"
+#define REPORT_FORMAT_NAME_EXTENDED "extended"
+#define REPORT_FORMAT_NAME_JSON "json"
+
+int report_format_init(struct cmd_context *cmd, struct dm_report_group **report_group,
+ struct dm_report **status_rh)
+{
+ static char status_report_name[] = "status";
+ struct report_args args = {0};
+ const char *format_str;
+ dm_report_group_type_t group_type;
+ struct dm_report_group *new_report_group;
+ struct dm_report *tmp_status_rh = NULL;
+
+ if (!(format_str = arg_str_value(cmd, reportformat_ARG, NULL)))
+ return 1;
+
+ if (!strcmp(format_str, REPORT_FORMAT_NAME_NATIVE))
+ return 1;
+
+ if (!strcmp(format_str, REPORT_FORMAT_NAME_EXTENDED))
+ group_type = DM_REPORT_GROUP_EXTENDED;
+ else if (!strcmp(format_str, REPORT_FORMAT_NAME_JSON))
+ group_type = DM_REPORT_GROUP_JSON;
+ else {
+ log_error("%s: unknown report format.", format_str);
+ log_error("Supported report formats: %s, %s, %s.",
+ REPORT_FORMAT_NAME_NATIVE,
+ REPORT_FORMAT_NAME_EXTENDED,
+ REPORT_FORMAT_NAME_JSON);
+ return 0;
+ }
+
+ if (!(new_report_group = dm_report_group_create(group_type, NULL))) {
+ log_error("Failed to create report group.");
+ return 0;
+ }
+
+ if (!*status_rh) {
+ args.report_type = CMDSTATUS;
+ if (!_config_report(cmd, &args))
+ goto_bad;
+
+ if (!(tmp_status_rh = report_init(NULL, args.options, args.keys, &args.report_type,
+ args.separator, args.aligned, args.buffered, args.headings,
+ args.field_prefixes, args.quoted, args.columns_as_rows,
+ args.selection))) {
+ log_error("Failed to create status report.");
+ goto bad;
+ }
+ }
+
+ if (!(dm_report_group_push(new_report_group, *status_rh ? : tmp_status_rh, status_report_name))) {
+ log_error("Failed to add status report to report group.");
+ goto bad;
+ }
+
+ *report_group = new_report_group;
+ if (tmp_status_rh)
+ *status_rh = tmp_status_rh;
+ return 1;
+bad:
+ if (!dm_report_group_destroy(new_report_group))
+ stack;
+ if (tmp_status_rh)
+ dm_report_free(tmp_status_rh);
+ return 0;
+}