Skip to content

Commit a6a7837

Browse files
acornardmurlock
authored andcommitted
[WIP] Add different outputs format (JSON, YAML, CSV)
1 parent c1b96c1 commit a6a7837

File tree

4 files changed

+354
-258
lines changed

4 files changed

+354
-258
lines changed

main/CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ target_link_libraries(gridinit
1818
${GLIB2_LIBRARIES} ${LIBEVENT_LIBRARIES})
1919

2020
add_executable(gridinit_cmd
21-
gridinit_cmd.c)
21+
gridinit_cmd.c
22+
format_output.c)
2223
target_link_libraries(gridinit_cmd
23-
gridinit-internals
24+
gridinit-internals
2425
${GLIB2_LIBRARIES} ${LIBEVENT_LIBRARIES})
2526

2627
add_executable(gridinit_testcmd

main/format_output.c

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
/*
2+
Copyright (C) 2015 OpenIO SAS, as part of OpenIO SDS
3+
4+
This program is free software: you can redistribute it and/or modify
5+
it under the terms of the GNU Affero General Public License as
6+
published by the Free Software Foundation, either version 3 of the
7+
License, or (at your option) any later version.
8+
This program is distributed in the hope that it will be useful,
9+
but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
GNU Affero General Public License for more details.
12+
You should have received a copy of the GNU Affero General Public License
13+
along with this program. If not, see <http://www.gnu.org/licenses/>.
14+
*/
15+
16+
#include <stdio.h>
17+
#include <glib.h>
18+
#include "./format_output.h"
19+
20+
int
21+
format_to_int(gchar *format)
22+
{
23+
if (g_strcmp0(format, "json") == 0)
24+
return 0;
25+
if (g_strcmp0(format, "csv") == 0)
26+
return 1;
27+
if (g_strcmp0(format, "yaml") == 0)
28+
return 2;
29+
else
30+
return -1;
31+
}
32+
33+
void
34+
print_as_json(gchar *status, gchar *start, gchar *error, gboolean first)
35+
{
36+
gchar header[] = " {\n";
37+
gchar footer[] = " }";
38+
gchar tab[] = " ";
39+
40+
if(!first)
41+
fprintf(stdout, ",\n");
42+
43+
fprintf(stdout, "%s", header);
44+
fprintf(stdout, "%s\"status\": \"%s\",\n", tab, status);
45+
fprintf(stdout, "%s\"start\": \"%s\",\n", tab, start);
46+
fprintf(stdout, "%s\"error\": \"%s\"\n", tab, error);
47+
fprintf(stdout, "%s", footer);
48+
}
49+
50+
void
51+
print_as_yaml(gchar *status, gchar *start, gchar *error, gboolean first)
52+
{
53+
gchar header[] = " {\n";
54+
gchar footer[] = " }";
55+
gchar tab[] = " ";
56+
57+
if(!first)
58+
fprintf(stdout, ",\n");
59+
60+
fprintf(stdout, "%s", header);
61+
fprintf(stdout, "%sstatus: %s,\n", tab, status);
62+
fprintf(stdout, "%sstart: %s,\n", tab, start);
63+
fprintf(stdout, "%serror: %s\n", tab, error);
64+
fprintf(stdout, "%s", footer);
65+
}
66+
67+
68+
69+
void
70+
print_as_csv(gchar *status, gchar *start, gchar *error)
71+
{
72+
fprintf(stdout, "%s,%s,%s\n", status, start, error);
73+
}
74+
75+
76+
void
77+
print_header(gchar *format)
78+
{
79+
switch (format_to_int(format)) {
80+
case 0:
81+
case 2:
82+
fprintf(stdout, "[\n");
83+
break;
84+
case 1:
85+
fprintf(stdout, "status,start,error\n");
86+
break;
87+
default:
88+
break;
89+
}
90+
}
91+
92+
void
93+
print_footer(gchar *format)
94+
{
95+
switch (format_to_int(format)) {
96+
case 0 :
97+
case 2:
98+
fprintf(stdout, "\n]\n");
99+
break;
100+
default:
101+
break;
102+
}
103+
return;
104+
}
105+
106+
void
107+
print_body(gchar *format, gchar *status, gchar *start, gchar *error, gboolean first){
108+
switch (format_to_int(format)) {
109+
case 0:
110+
print_as_json(status, start, error, first);
111+
break;
112+
case 1:
113+
print_as_csv(status, start, error);
114+
break;
115+
case 2:
116+
print_as_yaml(status, start, error, first);
117+
break;
118+
default:
119+
fprintf(stdout, "%s\t%s\t%s\n", status, start, error);
120+
}
121+
}
122+
123+
void
124+
print_status_header(gchar *format)
125+
{
126+
switch (format_to_int(format)) {
127+
case 0:
128+
case 2:
129+
fprintf(stdout, "[\n");
130+
break;
131+
case 1:
132+
fprintf(stdout,
133+
"key,status,pid,#start,#died,csz,ssz,mfd,since,group,cmd\n");
134+
break;
135+
default:
136+
break;
137+
}
138+
}
139+
140+
void
141+
status_body_json(gchar *fmt_line, int size)
142+
{
143+
g_snprintf(fmt_line, size,
144+
"{\n \"key\":\"%%s\",\n \"status\":\"%%s\","
145+
"\n \"pid\":\"%%d\",\n \"#start\":\"%%d\","
146+
"\n \"#died\":\"%%d\",\n \"csz\":\"%%ld\","
147+
"\n \"ssz\":\"%%ld\",\n \"mfd\":\"%%ld\","
148+
"\n \"since\":\"%%s\",\n \"group\":\"%%s\","
149+
"\n \"cmd\":\"%%s\"\n }");
150+
}
151+
152+
153+
void
154+
status_body_yaml(gchar *fmt_line, int size)
155+
{
156+
g_snprintf(fmt_line, size,
157+
" {\n key: %%s,\n status: %%s,"
158+
"\n pid: %%d,\n #start: %%d,"
159+
"\n #died: %%d,\n csz: %%ld,"
160+
"\n ssz: %%ld,\n mfd: %%ld,"
161+
"\n since: %%s,\n group: %%s,"
162+
"\n cmd: %%s\n }");
163+
}
164+
165+
void
166+
status_body_csv(gchar *fmt_line, int size)
167+
{
168+
g_snprintf(fmt_line, size,
169+
"%%s,%%s,%%d,%%d,%%d,%%ld,%%ld,%%ld,%%s,%%s,%%s\n");
170+
}
171+
172+
void
173+
print_status_sep(gchar *format, int count)
174+
{
175+
switch (format_to_int(format)) {
176+
case 0:
177+
case 2:
178+
if(count)
179+
fprintf(stdout, ",\n");
180+
default:
181+
break;
182+
}
183+
}
184+
185+
void
186+
get_line_format(gchar *format, gchar *fmt_line, int size)
187+
{
188+
switch (format_to_int(format)) {
189+
case 0:
190+
status_body_json(fmt_line, size);
191+
break;
192+
case 1:
193+
status_body_csv(fmt_line, size);
194+
break;
195+
case 2:
196+
status_body_yaml(fmt_line, size);
197+
break;
198+
default:
199+
break;
200+
}
201+
}

main/format_output.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
Copyright (C) 2015 OpenIO SAS, as part of OpenIO SDS
3+
4+
This program is free software: you can redistribute it and/or modify
5+
it under the terms of the GNU Affero General Public License as
6+
published by the Free Software Foundation, either version 3 of the
7+
License, or (at your option) any later version.
8+
This program is distributed in the hope that it will be useful,
9+
but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
GNU Affero General Public License for more details.
12+
You should have received a copy of the GNU Affero General Public License
13+
along with this program. If not, see <http://www.gnu.org/licenses/>.
14+
*/
15+
16+
#ifndef __FORMAT_OUTPUT__
17+
#define __FORMAT_OUTPUT__
18+
19+
# include <glib.h>
20+
21+
int format_to_int(gchar *format);
22+
void print_as_json(gchar *status, gchar *start, char *error, gboolean first);
23+
void print_as_yaml(gchar *status, gchar *start, char *error, gboolean first);
24+
void print_as_csv(gchar *status, gchar *start, char *error);
25+
void print_header(gchar *format);
26+
void print_footer(gchar *format);
27+
void print_body(gchar *format, gchar *status, gchar *start, gchar *error, gboolean first);
28+
void print_status_header(gchar *format);
29+
void status_body_json(gchar *fmt_line, int size);
30+
void status_body_yaml(gchar *fmt_line, int size);
31+
void status_body_csv(gchar *fmt_line, int size);
32+
void print_status_sep(gchar *format, int count);
33+
void get_line_format(gchar *format, gchar *fmt_line, int size);
34+
35+
#endif

0 commit comments

Comments
 (0)