summaryrefslogtreecommitdiff
path: root/bin/raps2
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2011-07-04 12:31:48 +0200
committerDaniel Friesel <derf@finalrewind.org>2011-07-04 12:31:48 +0200
commitd996a363bf26b2291b0fb67ed6331aefee865278 (patch)
tree01f62b747d07465bc067730175f3d29ecec31c4f /bin/raps2
parent581559e3d6dcac68e7ae297f9b1247b63b143582 (diff)
raps2: Simplify error checking
Diffstat (limited to 'bin/raps2')
-rwxr-xr-xbin/raps257
1 files changed, 31 insertions, 26 deletions
diff --git a/bin/raps2 b/bin/raps2
index f908040..4898dd5 100755
--- a/bin/raps2
+++ b/bin/raps2
@@ -15,16 +15,35 @@ my ( $action, @args ) = @ARGV;
our $VERSION = '0.4';
-sub cmd_add {
- my ($name) = @_;
+sub file_must_exist {
+ my ( $file, $name ) = @_;
- my $pwfile = data_home('raps2') . "/${name}";
+ if ( not defined $file ) {
+ say STDERR "No such account: ${name}";
+ exit 2;
+ }
+
+ return;
+}
- if ( -e $pwfile ) {
+sub file_must_not_exist {
+ my ( $file, $name ) = @_;
+
+ if ( -e $file ) {
say STDERR "Account already exists: ${name}";
exit 2;
}
+ return;
+}
+
+sub cmd_add {
+ my ($name) = @_;
+
+ my $pwfile = data_home('raps2') . "/${name}";
+
+ file_must_not_exist( $pwfile, $name );
+
$raps2->get_master_password();
my $url = $raps2->ui->read_line('URL');
@@ -48,10 +67,7 @@ sub cmd_dump {
my $pwfile = data_files("raps2/${name}");
- if ( not defined $pwfile ) {
- say STDERR "Account does not exist: ${name}";
- exit 2;
- }
+ file_must_exist( $pwfile, $name );
$raps2->get_master_password();
@@ -74,10 +90,7 @@ sub cmd_edit {
my $pwfile = data_files("raps2/${name}");
- if ( not defined $pwfile ) {
- say STDERR "Account does not exist: ${name}";
- exit 2;
- }
+ file_must_exist( $pwfile, $name );
$raps2->get_master_password();
@@ -111,10 +124,7 @@ sub cmd_get {
my $pwfile = data_files("raps2/${name}");
- if ( not defined $pwfile ) {
- say STDERR "Account does not exist: ${name}";
- exit 2;
- }
+ file_must_exist( $pwfile, $name );
$raps2->get_master_password();
@@ -134,10 +144,7 @@ sub cmd_info {
my $pwfile = data_files("raps2/${name}");
- if ( not defined $pwfile ) {
- say STDERR "Account does not exist: ${name}";
- exit 2;
- }
+ file_must_exist( $pwfile, $name );
my $key = $raps2->pw_load_info( file => $pwfile );
$raps2->ui->output( [ 'URL', $key->{url} ], [ 'Login', $key->{login} ], );
@@ -165,12 +172,10 @@ sub cmd_remove {
my $pwfile = data_files("raps2/${name}");
- if ( defined $pwfile ) {
- unlink($pwfile);
- }
- else {
- say STDERR "Account did not exist: ${name}";
- }
+ file_must_exist( $pwfile, $name );
+
+ unlink($pwfile)
+ or die("Could not unlink ${pwfile}: ${!}\n");
return;
}