Skip to content

Commit f2c0eb2

Browse files
authored
Merge pull request #34 from acornard/update-option-v2
Update option v2
2 parents cd41ae5 + d38a769 commit f2c0eb2

File tree

5 files changed

+397
-174
lines changed

5 files changed

+397
-174
lines changed

main/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ 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
2324
gridinit-internals
2425
${GLIB2_LIBRARIES} ${LIBEVENT_LIBRARIES})
@@ -33,4 +34,3 @@ install(TARGETS gridinit gridinit_cmd
3334
LIBRARY DESTINATION ${LD_LIBDIR}
3435
PUBLIC_HEADER DESTINATION include
3536
RUNTIME DESTINATION bin)
36-

main/format_output.c

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
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+
FORMAT
21+
parse_format(gchar *format)
22+
{
23+
if (g_strcmp0(format, "json") == 0)
24+
return JSON;
25+
if (g_strcmp0(format, "csv") == 0)
26+
return CSV;
27+
else
28+
return DEFAULT;
29+
}
30+
31+
void
32+
print_as_json(gchar *status, gchar *start, gchar *error, gboolean first)
33+
{
34+
gchar header[] = " {\n";
35+
gchar footer[] = " }";
36+
gchar tab[] = " ";
37+
38+
if(!first)
39+
fprintf(stdout, ",\n");
40+
41+
fprintf(stdout, "%s", header);
42+
fprintf(stdout, "%s\"status\": \"%s\",\n", tab, status);
43+
fprintf(stdout, "%s\"start\": \"%s\",\n", tab, start);
44+
fprintf(stdout, "%s\"error\": \"%s\"\n", tab, error);
45+
fprintf(stdout, "%s", footer);
46+
}
47+
48+
void
49+
print_as_csv(gchar *status, gchar *start, gchar *error)
50+
{
51+
fprintf(stdout, "%s,%s,%s\n", status, start, error);
52+
}
53+
54+
55+
void
56+
print_header(FORMAT format)
57+
{
58+
switch (format) {
59+
case JSON:
60+
fprintf(stdout, "[\n");
61+
break;
62+
case CSV:
63+
fprintf(stdout, "status,start,error\n");
64+
break;
65+
default:
66+
break;
67+
}
68+
}
69+
70+
void
71+
print_footer(FORMAT format)
72+
{
73+
switch (format) {
74+
case JSON:
75+
fprintf(stdout, "\n]\n");
76+
break;
77+
default:
78+
break;
79+
}
80+
return;
81+
}
82+
83+
void
84+
print_body(FORMAT format, gchar *status, gchar *start, gchar *error, gboolean first)
85+
{
86+
switch (format) {
87+
case JSON:
88+
print_as_json(status, start, error, first);
89+
break;
90+
case CSV:
91+
print_as_csv(status, start, error);
92+
break;
93+
default:
94+
fprintf(stdout, "%s\t%s\t%s\n", status, start, error);
95+
}
96+
}
97+
98+
void
99+
print_status_header(FORMAT format)
100+
{
101+
switch (format) {
102+
case JSON:
103+
case CSV:
104+
fprintf(stdout,
105+
"key,status,pid,#start,#died,csz,ssz,mfd,since,group,cmd\n");
106+
break;
107+
default:
108+
break;
109+
}
110+
}
111+
112+
void
113+
status_body_json(gchar *fmt_line, int size)
114+
{
115+
g_snprintf(fmt_line, size,
116+
" {\n \"key\":\"%%s\",\n \"status\":\"%%s\","
117+
"\n \"pid\":\"%%d\",\n \"#start\":\"%%d\","
118+
"\n \"#died\":\"%%d\",\n \"csz\":\"%%ld\","
119+
"\n \"ssz\":\"%%ld\",\n \"mfd\":\"%%ld\","
120+
"\n \"since\":\"%%s\",\n \"group\":\"%%s\","
121+
"\n \"cmd\":\"%%s\"\n }");
122+
}
123+
124+
125+
void
126+
status_body_csv(gchar *fmt_line, int size)
127+
{
128+
g_snprintf(fmt_line, size,
129+
"%%s,%%s,%%d,%%d,%%d,%%ld,%%ld,%%ld,%%s,%%s,%%s\n");
130+
}
131+
132+
void
133+
print_status_sep(FORMAT format, int count)
134+
{
135+
switch (format) {
136+
case JSON:
137+
if(count)
138+
fprintf(stdout, ",\n");
139+
default:
140+
break;
141+
}
142+
}
143+
144+
void
145+
get_line_format(FORMAT format, gchar *fmt_line, int size)
146+
{
147+
switch (format) {
148+
case JSON:
149+
status_body_json(fmt_line, size);
150+
break;
151+
case CSV:
152+
status_body_csv(fmt_line, size);
153+
break;
154+
default:
155+
break;
156+
}
157+
}

main/format_output.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
typedef enum FORMAT FORMAT;
22+
enum FORMAT {DEFAULT = 0, CSV = 1, JSON = 2};
23+
24+
FORMAT parse_format(gchar *format);
25+
void print_as_json(gchar *status, gchar *start, char *error, gboolean first);
26+
void print_as_csv(gchar *status, gchar *start, char *error);
27+
void print_header(FORMAT format);
28+
void print_footer(FORMAT format);
29+
void print_body(FORMAT format, gchar *status, gchar *start, gchar *error, gboolean first);
30+
void print_status_header(FORMAT format);
31+
void status_body_json(gchar *fmt_line, int size);
32+
void status_body_csv(gchar *fmt_line, int size);
33+
void print_status_sep(FORMAT format, int count);
34+
void get_line_format(FORMAT format, gchar *fmt_line, int size);
35+
36+
#endif

main/gridinit.c

Lines changed: 39 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,12 @@ static char *config_subdir = NULL;
9292
static char **groups_only_cli = NULL;
9393
static char **groups_only_cfg = NULL;
9494

95-
static volatile int flag_help = 0;
96-
static volatile int flag_quiet = 0;
97-
static volatile int flag_daemon = 0;
98-
static volatile int flag_running = ~0;
99-
static volatile int flag_check_socket = 0;
100-
static volatile int flag_more_verbose = 0;
95+
static volatile gboolean flag_quiet = FALSE;
96+
static volatile gboolean flag_daemon = FALSE;
97+
static volatile gboolean flag_running = TRUE;
98+
static volatile gboolean flag_check_socket = FALSE;
99+
static volatile gboolean flag_more_verbose = FALSE;
100+
static volatile gboolean flag_version = FALSE;
101101

102102
static volatile gint32 default_uid = -1;
103103
static volatile gint32 default_gid = -1;
@@ -110,6 +110,22 @@ static gboolean _cfg_reload(gboolean services_only, GError **err);
110110

111111
static void servers_ensure(void);
112112

113+
static GOptionEntry entries[] = {
114+
{"daemonize", 'd', 0, G_OPTION_ARG_NONE, (gboolean *)&flag_daemon,
115+
"Detaches then daemonizes the gridinit", NULL},
116+
{"group", 'g', 0, G_OPTION_ARG_STRING_ARRAY, &groups_only_cli,
117+
"limits the services loading to those belonging to the specified"
118+
"group. This option can be repeated", "GROUP"},
119+
{"quiet", 'q', 0, G_OPTION_ARG_NONE, (gboolean *)&flag_quiet,
120+
"quiet mode, suppress non-error output",NULL},
121+
{"version", 'V', 0, G_OPTION_ARG_NONE, (gboolean *)&flag_version,
122+
"Display the version of gridinit", NULL},
123+
{"verbose", 'v', 0, G_OPTION_ARG_NONE, (gboolean *)&flag_more_verbose,
124+
"verbose output mode", NULL},
125+
{"syslog", 's', 0, G_OPTION_ARG_STRING, &syslog_id,
126+
"enable logs using syslog with the given ID", "ID"},
127+
{NULL}
128+
};
113129

114130
/* ------------------------------------------------------------------------- */
115131

@@ -966,24 +982,6 @@ signals_clean(void)
966982

967983
/* Configuration ----------------------------------------------------------- */
968984

969-
static void
970-
main_usage(void)
971-
{
972-
if (flag_quiet)
973-
return;
974-
g_printerr("\n"
975-
"Usage: %s [OPTIONS] ... CONFIG_PATH [LOG4C_PATH]\n"
976-
" with OPTIONS:\n"
977-
" -d : Detaches then daemonizes the gridinit\n"
978-
" -h : displays this help section\n"
979-
" -g GROUP : limits the services loading to those belonging to\n"
980-
" the specified group. This option can be repeated.\n"
981-
" -q : quiet mode, suppress non-error output\n"
982-
" -v : verbose output mode.\n"
983-
" -s ID : enable logs using syslog with the given ID.\n"
984-
"\n", g_get_prgname());
985-
}
986-
987985
static gboolean
988986
_cfg_value_is_true(const gchar *val)
989987
{
@@ -1787,54 +1785,31 @@ logger_stderr(const gchar *log_domain, GLogLevelFlags log_level,
17871785
static void
17881786
__parse_options(int argc, char ** args)
17891787
{
1790-
int c;
17911788
GError *error_local = NULL;
1792-
1793-
while (-1 != (c = getopt(argc, args, "vqhdg:s:"))) {
1794-
switch (c) {
1795-
case 'd':
1796-
flag_daemon = ~0;
1797-
break;
1798-
case 'h':
1799-
flag_help = ~0;
1800-
break;
1801-
case 'g':
1802-
if (!optarg) {
1803-
g_printerr("Expected argument to the '-%c' option\n", c);
1804-
exit(1);
1805-
}
1806-
_str_set_array(TRUE, &groups_only_cli, optarg);
1807-
break;
1808-
case 's':
1809-
if (!optarg) {
1810-
g_printerr("Expected argument to the '-%c' option\n", c);
1811-
exit(1);
1812-
}
1813-
g_strlcpy(syslog_id, optarg, sizeof(syslog_id));
1814-
break;
1815-
case 'v':
1816-
logger_verbose_default();
1817-
break;
1818-
case 'q':
1819-
flag_quiet = ~0;
1820-
logger_init_level(GRID_LOGLVL_ERROR);
1821-
break;
1822-
default:
1823-
if (!flag_quiet)
1824-
g_printerr("Unexpected option : %c\n", c);
1825-
exit(1);
1826-
}
1789+
GOptionContext *context;
1790+
1791+
context = g_option_context_new(" CONFIG_PATH [LOG4C_PATH]");
1792+
g_option_context_add_main_entries(context, entries, NULL);
1793+
if (!g_option_context_parse(context, &argc, &args, &error_local)) {
1794+
g_print("option parsing failed: %s\n", error_local->message);
1795+
gchar *usage = g_option_context_get_help (context, TRUE, NULL);
1796+
g_print("%s", usage);
1797+
exit(1);
1798+
return;
18271799
}
18281800

1829-
if (flag_help) {
1830-
main_usage();
1801+
if (flag_more_verbose)
1802+
logger_verbose_default();
1803+
1804+
if (flag_version) {
1805+
fprintf(stdout, "gridinit version: %s\n", API_VERSION);
18311806
exit(0);
18321807
return;
18331808
}
1834-
18351809
/* check for additionnal arguments */
18361810
if (optind >= argc) {
1837-
main_usage();
1811+
gchar *usage = g_option_context_get_help (context, TRUE, NULL);
1812+
g_print("%s", usage);
18381813
exit(1);
18391814
return;
18401815
}

0 commit comments

Comments
 (0)