X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fgroupsecret;a=blobdiff_plain;f=lib%2FApp%2FGroupSecret.pm;h=ce85e9db419432d9576e6be1d48b042c31deebda;hp=fe6593de9afd8151303f50478d379410480d0e9d;hb=f5d5e7f98d1f1f52a19304d2b868d30c7a46e267;hpb=93527579d68b0ccdd5dab72d88764a53fef43b88 diff --git a/lib/App/GroupSecret.pm b/lib/App/GroupSecret.pm index fe6593d..ce85e9d 100644 --- a/lib/App/GroupSecret.pm +++ b/lib/App/GroupSecret.pm @@ -21,11 +21,27 @@ use MIME::Base64; use Pod::Usage; use namespace::clean; +=method new + + $script = App::GroupSecret->new; + +Construct a new script object. + +=cut + sub new { my $class = shift; return bless {}, $class; } +=method main + + $script->main(@ARGV); + +Run a command with the given command-line arguments. + +=cut + sub main { my $self = shift; my @args = @_; @@ -78,7 +94,7 @@ sub main { my $command = shift @args; my $lookup = $command; $lookup =~ s/-/_/g; - my $method = 'action_' . ($commands{$lookup} || ''); + my $method = '_action_' . ($commands{$lookup} || ''); if (!$self->can($method)) { warn "Unknown command: $command\n"; @@ -88,21 +104,44 @@ sub main { $self->$method(@args); } +=method filepath + + $filepath = $script->filepath; + +Get the path to the keyfile. + +=cut + sub filepath { shift->{filepath} ||= $ENV{GROUPSECRET_KEYFILE} || 'groupsecret.yml'; - } +=method file + + $file = $script->file; + +Get the L instance for the keyfile. + +=cut + sub file { my $self = shift; return $self->{file} ||= App::GroupSecret::File->new($self->filepath); } +=method private_key + + $filepath = $script->private_key; + +Get the path to a private key used to decrypt the keyfile. + +=cut + sub private_key { shift->{private_key} ||= $ENV{GROUPSECRET_PRIVATE_KEY} || "$ENV{HOME}/.ssh/id_rsa"; } -sub action_print_secret { +sub _action_print_secret { my $self = shift; my $decrypt = 1; @@ -112,7 +151,11 @@ sub action_print_secret { ) or pod2usage(2); my $file = $self->file; - die "No secret in file -- use the \`set-secret' command to set one.\n" if !$file->secret; + my $filepath = $file->filepath; + die "No keyfile '$filepath' exists -- use the \`add-key' command to create one.\n" + unless -e $filepath && !-d $filepath; + die "No secret in keyfile '$filepath' exists -- use the \`set-secret' command to set one.\n" + if !$file->secret; if ($decrypt) { my $private_key = $self->private_key; @@ -124,7 +167,7 @@ sub action_print_secret { } } -sub action_set_secret { +sub _action_set_secret { my $self = shift; my $keep_passphrase = 0; @@ -173,7 +216,7 @@ sub action_set_secret { $file->save; } -sub action_add_key { +sub _action_add_key { my $self = shift; my $embed = 0; @@ -215,7 +258,7 @@ sub action_add_key { $file->save; } -sub action_delete_key { +sub _action_delete_key { my $self = shift; my $file = $self->file; @@ -240,7 +283,7 @@ sub action_delete_key { $file->save; } -sub action_list_keys { +sub _action_list_keys { my $self = shift; my $file = $self->file;