Skip to content

Commit

Permalink
initctl: add support for --json to list command
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
troglobit committed Jan 7, 2023
1 parent 73b8943 commit c29589c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/initctl.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ extern int icreate; /* initctl -c */
extern int iforce; /* initctl -f */
extern int ionce; /* initctl -1 */
extern int heading; /* initctl -t */
extern int json; /* initctl -j */
extern int verbose; /* initctl -v */
extern int plain; /* initctl -p */
extern int quiet; /* initctl -q */
Expand Down
50 changes: 38 additions & 12 deletions src/serv.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ static int calc_width(char *arr[], size_t len)
return width;
}

static void do_list(const char *path)
static void do_list(const char *path, char *title, int comma)
{
static int once = 0;
int width, num, prev;
Expand All @@ -78,6 +78,7 @@ static void do_list(const char *path)

if (!fexist(path))
return;

dir = strdupa(path);
if (!dir)
return;
Expand All @@ -88,6 +89,11 @@ static void do_list(const char *path)
else
ptr = dir;

if (json) {
printf("%s\"%s\": \"%s\"", comma ? "," :"", title, path);
return;
}

if (heading)
print_header("%s%s ", once ? "\n" : "", dir);
puts(ptr);
Expand All @@ -101,7 +107,7 @@ static void do_list(const char *path)
if (gl.gl_pathc <= 0)
goto done;

if (plain) {
if (plain && !json) {
if (heading)
print_header("%s%s ", once ? "\n" : "", path);
once++;
Expand All @@ -119,8 +125,10 @@ static void do_list(const char *path)
goto done;
}

if (heading)
if (heading && !json)
print_header("%s ", path);
if (json)
printf("%s\"%s\": [", comma ? "," : "", title);

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

prev = 0;
for (i = 0; i < gl.gl_pathc; i++) {
if (i > 0 && !(i % num)) {
if (i > 0 && !(i % num) && !json) {
puts("");
prev = 0;
}

if (prev)
printf(" ");
printf("%-*s", width, gl.gl_pathv[i]);
printf("%s", json ? "," : " ");
if (json)
printf("\"%s\"", gl.gl_pathv[i]);
else
printf("%-*s", width, gl.gl_pathv[i]);
prev++;
}
puts("\n");

if (json)
fputs("]", stdout);
else
puts("\n");

done:
globfree(&gl);
Expand All @@ -151,29 +166,40 @@ static void do_list(const char *path)
int serv_list(char *arg)
{
char path[256];
int pre = 0;

if (arg && arg[0]) {
paste(path, sizeof(path), finit_rcsd, arg);
if (fisdir(path)) {
do_list(path);
if (json)
fputs("[", stdout);
do_list(path, NULL, 0);
if (json)
fputs("]\n", stdout);
return 0;
}
/* fall back to list all */
}

if (json)
fputs("{", stdout);

paste(path, sizeof(path), finit_rcsd, "available");
if (fisdir(path))
do_list(path);
do_list(path, "available", pre++);

paste(path, sizeof(path), finit_rcsd, "enabled");
if (fisdir(path))
do_list(path);
do_list(path, "enabled", pre++);

if (fisdir(finit_rcsd))
do_list(finit_rcsd);
do_list(finit_rcsd, "static", pre++);

if (fexist(finit_conf))
do_list(finit_conf);
do_list(finit_conf, "finit.conf", pre++);

if (json)
fputs("}\n", stdout);

return 0;
}
Expand Down

0 comments on commit c29589c

Please sign in to comment.