Skip to content

Commit c29589c

Browse files
committed
initctl: add support for --json to list command
This allows for easy access to the *disabled* services: root@anarchy:~# initctl ls --json |jq '.available - .enabled' [ "chronyd.conf", "dnsmasq.conf", "gdbserver.conf", "inadyn.conf", "inetd.conf", "isisd.conf", "lldpd.conf", "mstpd.conf", "ntpd.conf", "ospf6d.conf", "ospfd.conf", "querierd.conf", "ripd.conf", "ripng.conf", "sshd.conf", "syslogd.conf", "telnetd.conf", "uftpd.conf", "wpa_supplicant.conf", "zebra.conf" ] As discussed in PR issue #335 Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
1 parent 73b8943 commit c29589c

File tree

2 files changed

+39
-12
lines changed

2 files changed

+39
-12
lines changed

src/initctl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ extern int icreate; /* initctl -c */
3636
extern int iforce; /* initctl -f */
3737
extern int ionce; /* initctl -1 */
3838
extern int heading; /* initctl -t */
39+
extern int json; /* initctl -j */
3940
extern int verbose; /* initctl -v */
4041
extern int plain; /* initctl -p */
4142
extern int quiet; /* initctl -q */

src/serv.c

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ static int calc_width(char *arr[], size_t len)
6666
return width;
6767
}
6868

69-
static void do_list(const char *path)
69+
static void do_list(const char *path, char *title, int comma)
7070
{
7171
static int once = 0;
7272
int width, num, prev;
@@ -78,6 +78,7 @@ static void do_list(const char *path)
7878

7979
if (!fexist(path))
8080
return;
81+
8182
dir = strdupa(path);
8283
if (!dir)
8384
return;
@@ -88,6 +89,11 @@ static void do_list(const char *path)
8889
else
8990
ptr = dir;
9091

92+
if (json) {
93+
printf("%s\"%s\": \"%s\"", comma ? "," :"", title, path);
94+
return;
95+
}
96+
9197
if (heading)
9298
print_header("%s%s ", once ? "\n" : "", dir);
9399
puts(ptr);
@@ -101,7 +107,7 @@ static void do_list(const char *path)
101107
if (gl.gl_pathc <= 0)
102108
goto done;
103109

104-
if (plain) {
110+
if (plain && !json) {
105111
if (heading)
106112
print_header("%s%s ", once ? "\n" : "", path);
107113
once++;
@@ -119,8 +125,10 @@ static void do_list(const char *path)
119125
goto done;
120126
}
121127

122-
if (heading)
128+
if (heading && !json)
123129
print_header("%s ", path);
130+
if (json)
131+
printf("%s\"%s\": [", comma ? "," : "", title);
124132

125133
width = calc_width(gl.gl_pathv, gl.gl_pathc);
126134
if (width <= 0)
@@ -132,17 +140,24 @@ static void do_list(const char *path)
132140

133141
prev = 0;
134142
for (i = 0; i < gl.gl_pathc; i++) {
135-
if (i > 0 && !(i % num)) {
143+
if (i > 0 && !(i % num) && !json) {
136144
puts("");
137145
prev = 0;
138146
}
139147

140148
if (prev)
141-
printf(" ");
142-
printf("%-*s", width, gl.gl_pathv[i]);
149+
printf("%s", json ? "," : " ");
150+
if (json)
151+
printf("\"%s\"", gl.gl_pathv[i]);
152+
else
153+
printf("%-*s", width, gl.gl_pathv[i]);
143154
prev++;
144155
}
145-
puts("\n");
156+
157+
if (json)
158+
fputs("]", stdout);
159+
else
160+
puts("\n");
146161

147162
done:
148163
globfree(&gl);
@@ -151,29 +166,40 @@ static void do_list(const char *path)
151166
int serv_list(char *arg)
152167
{
153168
char path[256];
169+
int pre = 0;
154170

155171
if (arg && arg[0]) {
156172
paste(path, sizeof(path), finit_rcsd, arg);
157173
if (fisdir(path)) {
158-
do_list(path);
174+
if (json)
175+
fputs("[", stdout);
176+
do_list(path, NULL, 0);
177+
if (json)
178+
fputs("]\n", stdout);
159179
return 0;
160180
}
161181
/* fall back to list all */
162182
}
163183

184+
if (json)
185+
fputs("{", stdout);
186+
164187
paste(path, sizeof(path), finit_rcsd, "available");
165188
if (fisdir(path))
166-
do_list(path);
189+
do_list(path, "available", pre++);
167190

168191
paste(path, sizeof(path), finit_rcsd, "enabled");
169192
if (fisdir(path))
170-
do_list(path);
193+
do_list(path, "enabled", pre++);
171194

172195
if (fisdir(finit_rcsd))
173-
do_list(finit_rcsd);
196+
do_list(finit_rcsd, "static", pre++);
174197

175198
if (fexist(finit_conf))
176-
do_list(finit_conf);
199+
do_list(finit_conf, "finit.conf", pre++);
200+
201+
if (json)
202+
fputs("}\n", stdout);
177203

178204
return 0;
179205
}

0 commit comments

Comments
 (0)