From cea7035d865cc16c7fae8943e863915a0d5775ca Mon Sep 17 00:00:00 2001 From: eleksir Date: Sun, 8 Sep 2019 16:16:47 +0300 Subject: [PATCH] svc: add list, enable, disable abilities --- svc | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 50 insertions(+), 4 deletions(-) diff --git a/svc b/svc index f6e8332..f2810af 100644 --- a/svc +++ b/svc @@ -1,5 +1,11 @@ #!/usr/bin/perl +# runit sv utility ignores evironment variable SVDIR +# and unable to list services, fully relying to user this knowlege + +# TODO handle root user case as system mode! +# TODO handle system mode correctly + use strict; use warnings "all"; use JSON::PP; @@ -30,9 +36,49 @@ if ($c->{mode} eq 'user') { chdir $c->{dir}; } -help unless (defined($ARGV[0]) || defined($ARGV[1])); +unless(defined($ARGV[0])) { + help; +} else { + if ($ARGV[0] ne 'list') { + help unless (defined($ARGV[1])); + } +} -if ((-l "./$ARGV[0]") || (-d "./$ARGV[0]")) { +if ($ARGV[0] eq 'list') { + my $d; + + if (defined($ARGV[1]) && $ARGV[1] eq '--all') { + $d = "$ENV{HOME}$c->{servicedir}"; + } else { + $d = "$ENV{HOME}$c->{dir}"; + } + + opendir(D, $d) or die "Unable to list services\n"; + print join("\n", sort(grep {!/^\.{1,2}$/} readdir(D))) . "\n"; + closedir D; +} elsif ($ARGV[1] eq 'disable') { + my $d = "$ENV{HOME}$c->{dir}/$ARGV[0]"; + + if (-l $d) { + unlink("$d") or die "Unable to disable $ARGV[0]\n"; + } else { + die "No such service $ARGV[0]\n"; + } +} elsif ($ARGV[1] eq 'enable') { + my $d = "$ENV{HOME}$c->{dir}/$ARGV[0]"; + + if (-l $d) { + die "Service $ARGV[0] already enabled\n"; + } else { + my $svcdir = "$ENV{HOME}$c->{servicedir}/$ARGV[0]"; + + if (-d $svcdir) { + symlink($svcdir, $d) or die "Unable to enable service $ARGV[0]\n"; + } else { + die "No such service $ARGV[0]\n"; + } + } +} elsif ((-l "./$ARGV[0]") || (-d "./$ARGV[0]")) { chdir "./$ARGV[0]"; die "Is service running?\n" unless (-d './supervise'); chdir './supervise'; @@ -79,7 +125,8 @@ if ((-l "./$ARGV[0]") || (-d "./$ARGV[0]")) { exit 0; sub help { - print "Usage $0 SERVICE [start|stop|restart|status]\n"; + print "Usage $0 SERVICE [start|stop|restart|status|enable|disable]\n"; + print " $0 list [--all]\n"; exit 0; } @@ -97,5 +144,4 @@ sub status { close S; return $status; } - # vim: set ft=perl noet ai :