mirror of
https://github.com/troglobit/finit.git
synced 2026-11-06 15:51:03 +00:00
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>
This commit is contained in:
1 parent
73b89430e4
commit
c29589c2ca
2 files changed
+39
-12
No files matched your search
@@ -36,6 +36,7 @@ extern int icreate; /* initctl -c */
|
|||||||
extern int iforce; /* initctl -f */
|
extern int iforce; /* initctl -f */
|
||||||
extern int ionce; /* initctl -1 */
|
extern int ionce; /* initctl -1 */
|
||||||
extern int heading; /* initctl -t */
|
extern int heading; /* initctl -t */
|
||||||
|
extern int json; /* initctl -j */
|
||||||
extern int verbose; /* initctl -v */
|
extern int verbose; /* initctl -v */
|
||||||
extern int plain; /* initctl -p */
|
extern int plain; /* initctl -p */
|
||||||
extern int quiet; /* initctl -q */
|
extern int quiet; /* initctl -q */
|
||||||
|
|||||||
+38
-12
@@ -66,7 +66,7 @@ static int calc_width(char *arr[], size_t len)
|
|||||||
return width;
|
return width;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void do_list(const char *path)
|
static void do_list(const char *path, char *title, int comma)
|
||||||
{
|
{
|
||||||
static int once = 0;
|
static int once = 0;
|
||||||
int width, num, prev;
|
int width, num, prev;
|
||||||
@@ -78,6 +78,7 @@ static void do_list(const char *path)
|
|||||||
|
|
||||||
if (!fexist(path))
|
if (!fexist(path))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
dir = strdupa(path);
|
dir = strdupa(path);
|
||||||
if (!dir)
|
if (!dir)
|
||||||
return;
|
return;
|
||||||
@@ -88,6 +89,11 @@ static void do_list(const char *path)
|
|||||||
else
|
else
|
||||||
ptr = dir;
|
ptr = dir;
|
||||||
|
|
||||||
|
if (json) {
|
||||||
|
printf("%s\"%s\": \"%s\"", comma ? "," :"", title, path);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (heading)
|
if (heading)
|
||||||
print_header("%s%s ", once ? "\n" : "", dir);
|
print_header("%s%s ", once ? "\n" : "", dir);
|
||||||
puts(ptr);
|
puts(ptr);
|
||||||
@@ -101,7 +107,7 @@ static void do_list(const char *path)
|
|||||||
if (gl.gl_pathc <= 0)
|
if (gl.gl_pathc <= 0)
|
||||||
goto done;
|
goto done;
|
||||||
|
|
||||||
if (plain) {
|
if (plain && !json) {
|
||||||
if (heading)
|
if (heading)
|
||||||
print_header("%s%s ", once ? "\n" : "", path);
|
print_header("%s%s ", once ? "\n" : "", path);
|
||||||
once++;
|
once++;
|
||||||
@@ -119,8 +125,10 @@ static void do_list(const char *path)
|
|||||||
goto done;
|
goto done;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (heading)
|
if (heading && !json)
|
||||||
print_header("%s ", path);
|
print_header("%s ", path);
|
||||||
|
if (json)
|
||||||
|
printf("%s\"%s\": [", comma ? "," : "", title);
|
||||||
|
|
||||||
width = calc_width(gl.gl_pathv, gl.gl_pathc);
|
width = calc_width(gl.gl_pathv, gl.gl_pathc);
|
||||||
if (width <= 0)
|
if (width <= 0)
|
||||||
@@ -132,17 +140,24 @@ static void do_list(const char *path)
|
|||||||
|
|
||||||
prev = 0;
|
prev = 0;
|
||||||
for (i = 0; i < gl.gl_pathc; i++) {
|
for (i = 0; i < gl.gl_pathc; i++) {
|
||||||
if (i > 0 && !(i % num)) {
|
if (i > 0 && !(i % num) && !json) {
|
||||||
puts("");
|
puts("");
|
||||||
prev = 0;
|
prev = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (prev)
|
if (prev)
|
||||||
printf(" ");
|
printf("%s", json ? "," : " ");
|
||||||
printf("%-*s", width, gl.gl_pathv[i]);
|
if (json)
|
||||||
|
printf("\"%s\"", gl.gl_pathv[i]);
|
||||||
|
else
|
||||||
|
printf("%-*s", width, gl.gl_pathv[i]);
|
||||||
prev++;
|
prev++;
|
||||||
}
|
}
|
||||||
puts("\n");
|
|
||||||
|
if (json)
|
||||||
|
fputs("]", stdout);
|
||||||
|
else
|
||||||
|
puts("\n");
|
||||||
|
|
||||||
done:
|
done:
|
||||||
globfree(&gl);
|
globfree(&gl);
|
||||||
@@ -151,29 +166,40 @@ done:
|
|||||||
int serv_list(char *arg)
|
int serv_list(char *arg)
|
||||||
{
|
{
|
||||||
char path[256];
|
char path[256];
|
||||||
|
int pre = 0;
|
||||||
|
|
||||||
if (arg && arg[0]) {
|
if (arg && arg[0]) {
|
||||||
paste(path, sizeof(path), finit_rcsd, arg);
|
paste(path, sizeof(path), finit_rcsd, arg);
|
||||||
if (fisdir(path)) {
|
if (fisdir(path)) {
|
||||||
do_list(path);
|
if (json)
|
||||||
|
fputs("[", stdout);
|
||||||
|
do_list(path, NULL, 0);
|
||||||
|
if (json)
|
||||||
|
fputs("]\n", stdout);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
/* fall back to list all */
|
/* fall back to list all */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (json)
|
||||||
|
fputs("{", stdout);
|
||||||
|
|
||||||
paste(path, sizeof(path), finit_rcsd, "available");
|
paste(path, sizeof(path), finit_rcsd, "available");
|
||||||
if (fisdir(path))
|
if (fisdir(path))
|
||||||
do_list(path);
|
do_list(path, "available", pre++);
|
||||||
|
|
||||||
paste(path, sizeof(path), finit_rcsd, "enabled");
|
paste(path, sizeof(path), finit_rcsd, "enabled");
|
||||||
if (fisdir(path))
|
if (fisdir(path))
|
||||||
do_list(path);
|
do_list(path, "enabled", pre++);
|
||||||
|
|
||||||
if (fisdir(finit_rcsd))
|
if (fisdir(finit_rcsd))
|
||||||
do_list(finit_rcsd);
|
do_list(finit_rcsd, "static", pre++);
|
||||||
|
|
||||||
if (fexist(finit_conf))
|
if (fexist(finit_conf))
|
||||||
do_list(finit_conf);
|
do_list(finit_conf, "finit.conf", pre++);
|
||||||
|
|
||||||
|
if (json)
|
||||||
|
fputs("}\n", stdout);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user