summaryrefslogtreecommitdiff
path: root/utils/rd-group
diff options
context:
space:
mode:
authorChristian Pointner <equinox@spreadspace.org>2015-09-30 15:07:41 (GMT)
committerChristian Pointner <equinox@spreadspace.org>2015-09-30 15:07:41 (GMT)
commit5897b1bc3b059a29f57bde6c08344deceb41bd23 (patch)
treef2ca2ba16403ac0d65286fc1b5e3d9783440b32b /utils/rd-group
parentefb5d6557591ef21c27aa46aae220d862c36feeb (diff)
major cleanup for all utils
Diffstat (limited to 'utils/rd-group')
-rwxr-xr-xutils/rd-group346
1 files changed, 259 insertions, 87 deletions
diff --git a/utils/rd-group b/utils/rd-group
index c72e7d2..ecf10cb 100755
--- a/utils/rd-group
+++ b/utils/rd-group
@@ -29,120 +29,292 @@ if ($> != 0 ) {
exit 1;
}
-my $num_args = $#ARGV + 1;
-if ($num_args < 2) {
- print STDERR "Usage: rd-group (check|remove|get-members|get-carts|get-reports) <groupname>\n" .
+sub print_usage
+{
+ print STDERR "Usage: rd-group list\n" .
+ " rd-group (check|remove|get-members|get-carts|get-reports) <groupname>\n" .
" rd-group add <groupname> [ <description> ]\n" .
" rd-group (add-member|remove-member|is-member) <groupname> <user>\n" .
" rd-group set-carts <groupname> [ <low> [ <high> [ <type> [ <enforce range> ]]]\n" .
" rd-group set-reports <groupname> [ <nownext> [ <traffic> [ <music> ]]]\n";
- exit 1;
}
+sub list
+{
+ my ($dbh) = @_;
+
+ my @groups = RHRD::rddb::list_groups($dbh);
+ if(!defined $groups[0] && defined $groups[1]) {
+ print STDERR "$groups[1]: $groups[2]";
+ return 1;
+ }
+ for my $group (@groups) {
+ print $group . "\n";
+ }
+ return 0;
+}
+
+sub check
+{
+ my ($dbh, $groupname) = @_;
+
+ my ($result, $status, $errorstring) = RHRD::rddb::check_group($dbh, $groupname);
+ print STDERR "$result, $status: $errorstring\n";
+ return $result;
+}
+
+sub add
+{
+ my ($dbh, $groupname, $description) = @_;
+
+ my ($cnt, $status, $errorstring) = RHRD::rddb::add_group($dbh, $groupname, $description);
+ unless(defined $cnt) {
+ print STDERR "$errorstring\n";
+ return 1;
+ }
+ print int($cnt) . " rows affected\n";
+ return 0;
+}
+
+sub remove
+{
+ my ($dbh, $groupname) = @_;
+
+ my @results = RHRD::rddb::remove_group($dbh, $groupname);
+ if(!defined $results[0] && defined $results[2]) {
+ print STDERR "$results[2]\n";
+ return 1;
+ }
+ for my $href (@results) {
+ print int($href->{cnt}) . " " . $href->{name} . " deleted\n";
+ }
+ return 0;
+}
+
+sub get_members
+{
+ my ($dbh, $groupname) = @_;
+
+ my @users = RHRD::rddb::get_group_members($dbh, $groupname);
+ if(!defined $users[0] && defined $users[1]) {
+ print STDERR "$users[2]\n";
+ return 1;
+ }
+ for my $user (sort @users) {
+ print "$user\n";
+ }
+ return 0;
+}
+
+sub add_member
+{
+ my ($dbh, $groupname, $username) = @_;
+
+ my ($cnt, undef, $errorstring) = RHRD::rddb::add_group_member($dbh, $groupname, $username);
+ unless(defined $cnt) {
+ print STDERR "$errorstring\n";
+ return 1;
+ }
+ print int($cnt) . " rows affected\n";
+ return 0;
+}
+
+sub remove_member
+{
+ my ($dbh, $groupname, $username) = @_;
+
+ my ($cnt, undef, $errorstring) = RHRD::rddb::remove_group_member($dbh, $groupname, $username);
+ unless(defined $cnt) {
+ print STDERR "$errorstring\n";
+ return 1;
+ }
+ print int($cnt) . " rows affected\n";
+ return 0;
+}
+
+sub is_member
+{
+ my ($dbh, $groupname, $username) = @_;
+
+ my ($cnt, undef, $errorstring) = RHRD::rddb::is_group_member($dbh, $groupname, $username);
+ unless(defined $cnt) {
+ print STDERR "$errorstring\n";
+ return 1;
+ }
+ print $ARGV[2] . " is" . (($cnt) ? "" : " not") . " a member\n";
+ return (($cnt) ? 0 : 1);
+}
+
+sub get_carts
+{
+ my ($dbh, $groupname) = @_;
+
+ my ($low, $high, $type, $enforce_range) = RHRD::rddb::get_group_carts($dbh, $groupname);
+ unless(defined $low) {
+ print STDERR "$type\n";
+ return 1;
+ }
+ print "Range: " . int($low) . " - " . int($high) . ", Type: " . int($type) . ", Enforce Range: " . $enforce_range . "\n";
+ return 0;
+}
+
+sub set_carts
+{
+ my ($dbh, $groupname, $low_cart, $high_cart, $cart_type, $enforce_cart_range) = @_;
+
+ my ($cnt, undef, $errorstring) = RHRD::rddb::set_group_carts($dbh, $groupname, $low_cart, $high_cart, $cart_type, $enforce_cart_range);
+ unless(defined $cnt) {
+ print STDERR "$errorstring\n";
+ return 1;
+ }
+ print int($cnt) . " rows affected\n";
+ return 0;
+}
+
+sub get_reports
+{
+ my ($dbh, $groupname) = @_;
+
+ my ($nownext, $traffic, $music) = RHRD::rddb::get_group_reports($dbh, $groupname);
+ unless(defined $nownext) {
+ print STDERR "$music\n";
+ return 1;
+ }
+ print "Now-Next: " . $nownext . ", Traffic: " . $traffic . ", Music: " . $music . "\n";
+ return 0;
+}
+
+sub set_reports
+{
+ my ($dbh, $groupname, $now_next, $traffic, $music) = @_;
+
+ my ($cnt, undef, $errorstring) = RHRD::rddb::set_group_reports($dbh, $groupname, $now_next, $traffic, $music);
+ unless(defined $cnt) {
+ print STDERR "$errorstring\n";
+ return 1;
+ }
+ print int($cnt) . " rows affected\n";
+
+ return 0;
+}
+
+
+my $num_args = $#ARGV + 1;
+if($num_args < 1) {
+ print_usage();
+ exit(1);
+}
my $cmd = $ARGV[0];
my $groupname = $ARGV[1];
+my $ret = 0;
-my ($dbh, undef, $errorstring) = RHRD::rddb::opendb();
+my ($dbh, $status, $errorstring) = RHRD::rddb::opendb();
if(defined $dbh) {
- if($cmd eq "check") {
- (my $result, my $status, $errorstring) = RHRD::rddb::check_group($dbh, $groupname);
- print STDERR "$result, $status: $errorstring\n";
- RHRD::rddb::closedb($dbh);
- exit $result;
- } elsif($cmd eq "add") {
- (my $cnt, undef, $errorstring) = RHRD::rddb::add_group($dbh, $groupname, $ARGV[2]);
- unless(defined $cnt) {
- print STDERR "$errorstring\n";
- RHRD::rddb::closedb($dbh);
- exit 1;
+ if($cmd eq "list") {
+ if($num_args != 1) {
+ print_usage();
+ $ret = 1;
+ } else {
+ $ret = list($dbh);
}
- print int($cnt) . " rows affected\n";
- } elsif($cmd eq "remove") {
- my @results = RHRD::rddb::remove_group($dbh, $groupname);
- if(!defined $results[0] && defined $results[2]) {
- print STDERR "$results[2]\n";
+ }
+ elsif($cmd eq "check") {
+ if($num_args != 2) {
+ print_usage();
+ $ret = 1;
+ } else {
+ $ret = check($dbh, $groupname);
+ }
+ }
+ elsif($cmd eq "add") {
+ if($num_args < 2 || $num_args > 3) {
+ print_usage();
+ $ret = 1;
} else {
- for my $href (@results) {
- print int($href->{cnt}) . " " . $href->{name} . " deleted\n";
- }
+ $ret = add($dbh, $groupname, $ARGV[2]);
}
- } elsif($cmd eq "get-members") {
- my @users = RHRD::rddb::get_group_members($dbh, $groupname);
- if(!defined $users[0] && defined $users[1]) {
- print STDERR "$users[2]\n";
- RHRD::rddb::closedb($dbh);
- exit 1;
+ }
+ elsif($cmd eq "remove") {
+ if($num_args != 2) {
+ print_usage();
+ $ret = 1;
+ } else {
+ $ret = remove($dbh, $groupname);
}
- for my $user (sort @users) {
- print "$user\n";
+ }
+ elsif($cmd eq "get-members") {
+ if($num_args != 2) {
+ print_usage();
+ $ret = 1;
+ } else {
+ $ret = get_members($dbh, $groupname);
}
- } elsif($cmd eq "add-member") {
- (my $cnt, undef, $errorstring) = RHRD::rddb::add_group_member($dbh, $groupname, $ARGV[2]);
- unless(defined $cnt) {
- print STDERR "$errorstring\n";
- RHRD::rddb::closedb($dbh);
- exit 1;
+ }
+ elsif($cmd eq "add-member") {
+ if($num_args != 3) {
+ print_usage();
+ $ret = 1;
+ } else {
+ $ret = add_member($dbh, $groupname, $ARGV[2]);
}
- print int($cnt) . " rows affected\n";
- } elsif($cmd eq "remove-member") {
- (my $cnt, undef, $errorstring) = RHRD::rddb::remove_group_member($dbh, $groupname, $ARGV[2]);
- unless(defined $cnt) {
- print STDERR "$errorstring\n";
- RHRD::rddb::closedb($dbh);
- exit 1;
+ }
+ elsif($cmd eq "remove-member") {
+ if($num_args != 3) {
+ print_usage();
+ $ret = 1;
+ } else {
+ $ret = remove_member($dbh, $groupname, $ARGV[2]);
}
- print int($cnt) . " rows affected\n";
- } elsif($cmd eq "is-member") {
- (my $cnt, undef, $errorstring) = RHRD::rddb::is_group_member($dbh, $groupname, $ARGV[2]);
- RHRD::rddb::closedb($dbh);
- unless(defined $cnt) {
- print STDERR "$errorstring\n";
- exit 1;
+ }
+ elsif($cmd eq "is-member") {
+ if($num_args != 3) {
+ print_usage();
+ $ret = 1;
+ } else {
+ $ret = is_member($dbh, $groupname, $ARGV[2]);
}
- print $ARGV[2] . " is" . (($cnt) ? "" : " not") . " a member\n";
- exit (($cnt) ? 0 : 1);
- } elsif($cmd eq "get-carts") {
- my ($low, $high, $type, $enforce_range) = RHRD::rddb::get_group_carts($dbh, $groupname);
- unless(defined $low) {
- print STDERR "$type\n";
- RHRD::rddb::closedb($dbh);
- exit 1;
+ }
+ elsif($cmd eq "get-carts") {
+ if($num_args != 2) {
+ print_usage();
+ $ret = 1;
+ } else {
+ $ret = get_carts($dbh, $groupname);
}
- print "Range: " . int($low) . " - " . int($high) . ", Type: " . int($type) . ", Enforce Range: " . $enforce_range . "\n";
- } elsif($cmd eq "set-carts") {
- (my $cnt, undef, $errorstring) = RHRD::rddb::set_group_carts($dbh, $groupname, $ARGV[2], $ARGV[3], $ARGV[4], $ARGV[5]);
- unless(defined $cnt) {
- print STDERR "$errorstring\n";
- RHRD::rddb::closedb($dbh);
- exit 1;
+ }
+ elsif($cmd eq "set-carts") {
+ if($num_args < 2 || $num_args > 6) {
+ print_usage();
+ $ret = 1;
+ } else {
+ $ret = set_carts($dbh, $groupname, $ARGV[2], $ARGV[3], $ARGV[4], $ARGV[5]);
}
- print int($cnt) . " rows affected\n";
- } elsif($cmd eq "get-reports") {
- my ($nownext, $traffic, $music) = RHRD::rddb::get_group_reports($dbh, $groupname);
- unless(defined $nownext) {
- print STDERR "$music\n";
- RHRD::rddb::closedb($dbh);
- exit 1;
+ }
+ elsif($cmd eq "get-reports") {
+ if($num_args != 2) {
+ print_usage();
+ $ret = 1;
+ } else {
+ $ret = get_reports($dbh, $groupname);
}
- print "Now-Next: " . $nownext . ", Traffic: " . $traffic . ", Music: " . $music . "\n";
- } elsif($cmd eq "set-reports") {
- (my $cnt, undef, $errorstring) = RHRD::rddb::set_group_reports($dbh, $groupname, $ARGV[2], $ARGV[3], $ARGV[4]);
- unless(defined $cnt) {
- print STDERR "$errorstring\n";
- RHRD::rddb::closedb($dbh);
- exit 1;
+ }
+ elsif($cmd eq "set-reports") {
+ if($num_args < 2 || $num_args > 5) {
+ print_usage();
+ $ret = 1;
+ } else {
+ $ret = set_reports($dbh, $groupname, $ARGV[2], $ARGV[3], $ARGV[4]);
}
- print int($cnt) . " rows affected\n";
- } else {
+ }
+ else {
print STDERR "unknown command\n";
- RHRD::rddb::closedb($dbh);
- exit 1;
+ $ret = 1;
}
RHRD::rddb::closedb($dbh);
} else {
print STDERR "$errorstring\n";
- exit 1;
+ $ret = 1;
}
-exit 0
+exit $ret;