Skip to content

Commit

Permalink
Added autoloading of secrets when accessing value
Browse files Browse the repository at this point in the history
Off by default, this change adds an `autoload` method to the `Genesis::Secret`
class that will load the secret if it hasn't been loaded yet. This is useful
for secrets that are not needed in all cases, but are needed in some cases.

The Secret::Plan class has been updated to add an autoload method that will
turn on autoloading for all secrets in the plan as a convenience.
  • Loading branch information
dennisjbell committed Oct 17, 2024
1 parent f1d5e6f commit 38ca21d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
6 changes: 6 additions & 0 deletions lib/Genesis/Env/Secrets/Plan.pm
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,12 @@ sub filter {
}

# }}}
# autoload - load secrets on demand for their values {{{
sub autoload {
my $self = shift;
$_->{autoload} = 1 for ($self->secrets);
return $self;
}
# validate - validate the secret definitions, and any dependencies {{{
sub validate {
my $self = shift;
Expand Down
10 changes: 8 additions & 2 deletions lib/Genesis/Secret.pm
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ sub new {
path => $alt_path || $path,
definition => $args,
loaded => 0,
autoload => 0,
%src
}, $class);
}
Expand Down Expand Up @@ -66,6 +67,11 @@ sub load {
$self->plan->store->read($self);
}

sub autoload {
my ($self) = @_;
$self->load unless !$self->{autoload} || $self->loaded;
}

sub exists {
my ($self) = @_;
return 1 if $self->loaded && $self->has_value;
Expand Down Expand Up @@ -113,8 +119,8 @@ sub all_paths {($_[0]->path)}
sub full_path {$_[0]->base_path.$_[0]->path}
sub default_key {undef}
sub definition {$_[0]->{definition}}
sub value {$_[0]->{value}||$_[0]->{stored_value}}
sub has_value {defined($_[0]->{stored_value})}
sub value {$_[0]->autoload; $_[0]->{value}||$_[0]->{stored_value}}
sub has_value {$_[0]->autoload; defined($_[0]->{stored_value})}
sub loaded {$_[0]->{loaded}}
sub missing {!$_[0]->has_value}
sub set_plan {$_[0]->{plan} = $_[1]; $_[0]}
Expand Down

0 comments on commit 38ca21d

Please sign in to comment.