From dbfbda14b3abcbabd10851cb9962fddd41ad0e67 Mon Sep 17 00:00:00 2001 From: "Jonathan \"Duke\" Leto" Date: Fri, 24 Nov 2017 13:15:49 -0800 Subject: [PATCH] Refactor some common code into functions we will reuse all over the place --- lib/Hush/List.pm | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/lib/Hush/List.pm b/lib/Hush/List.pm index 1923db8..b6f5f4a 100644 --- a/lib/Hush/List.pm +++ b/lib/Hush/List.pm @@ -118,24 +118,35 @@ sub global_status { } } +sub _hushlist_exists { + my ($name) = @_; + my $list_dir = catdir($HUSHLIST_CONFIG_DIR,$name); + return (-e $list_dir) ? 1 : 0; +} + +sub exit_unless_hushlist_exists { + my ($name) = @_; + if (!_hushlist_exists($name)) { + print "Hushlist $name does not exist!\n"; + exit(1); + }; +} + # show details about a particular hushlist sub status { my ($self,$name) = @_; - my $list_dir = catdir($HUSHLIST_CONFIG_DIR,$name); - if (!-e $list_dir) { - barf "Hushlist $name does not exist!"; - } else { - my $list_specific_conf = catfile($HUSHLIST_CONFIG_DIR,$name,'list.conf'); - my %list_conf = read_file( $list_specific_conf ) =~ /^(\w+)=(.*)$/mg ; - my $member_list = catfile($HUSHLIST_CONFIG_DIR,$name,'members.txt'); - my @members = read_file($member_list); - my @nicknames = map { m/^[^ ]+(.*)/ } @members; - my $num_members = @members; - print "Hushlist '$name' has $num_members members, generated at $list_conf{generated}\n"; - #map { print "\t - $_\n" } @nicknames; - map { print "\t - $_" } @members; - } + exit_unless_hushlist_exists($name); + + my $list_specific_conf = catfile($HUSHLIST_CONFIG_DIR,$name,'list.conf'); + my %list_conf = read_file( $list_specific_conf ) =~ /^(\w+)=(.*)$/mg ; + my $member_list = catfile($HUSHLIST_CONFIG_DIR,$name,'members.txt'); + my @members = read_file($member_list); + my @nicknames = map { m/^[^ ]+(.*)/ } @members; + my $num_members = @members; + print "Hushlist '$name' has $num_members members, generated at $list_conf{generated}\n"; + #map { print "\t - $_\n" } @nicknames; + map { print "\t - $_" } @members; } sub new_list {