diff options
author | Daniel Friesel <derf@finalrewind.org> | 2011-07-04 12:31:48 +0200 |
---|---|---|
committer | Daniel Friesel <derf@finalrewind.org> | 2011-07-04 12:31:48 +0200 |
commit | d996a363bf26b2291b0fb67ed6331aefee865278 (patch) | |
tree | 01f62b747d07465bc067730175f3d29ecec31c4f | |
parent | 581559e3d6dcac68e7ae297f9b1247b63b143582 (diff) |
raps2: Simplify error checking
-rwxr-xr-x | bin/raps2 | 57 |
1 files changed, 31 insertions, 26 deletions
@@ -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; } |