summaryrefslogtreecommitdiff
path: root/bin
diff options
context:
space:
mode:
Diffstat (limited to 'bin')
-rwxr-xr-xbin/envstore49
1 files changed, 30 insertions, 19 deletions
diff --git a/bin/envstore b/bin/envstore
index c8dd7c6..59d9a67 100755
--- a/bin/envstore
+++ b/bin/envstore
@@ -13,7 +13,6 @@ my %store;
my $action = shift;
my $arg = shift;
my $arg2 = shift;
-my ($key, $value);
sub usage {
pod2usage(
@@ -23,42 +22,50 @@ sub usage {
-sections => 'SYNOPSIS|DESCRIPTION',
-output => \*STDERR,
);
+ return;
}
sub check_store {
my ($mode, $uid);
- unless (-e $store_file) {
- return(0);
+
+ if (-e $store_file) {
+ ($mode, $uid) = (stat($store_file))[2,4];
+ }
+ else {
+ return 0;
}
- ($mode, $uid) = (stat($store_file))[2,4];
+
if ($uid != $<) {
print STDERR "envstore: store file is insecure (not owned by us)\n";
- exit(1);
+ exit 1;
}
- $mode &= 0x00077;
- if ($mode > 0) {
+ if (($mode & 0x00077) > 0) {
print STDERR "envstore: store file is insecure (writable by group/others)\n";
- exit(1);
+ exit 1;
}
- return(1);
+ return 1;
}
sub load_store {
- return unless check_store;
- %store = %{retrieve($store_file) || {}};
+ if (check_store) {
+ %store = %{retrieve($store_file) || {}};
+ }
+ return;
}
sub save_store {
umask(0077);
nstore(\%store, $store_file);
+ return;
}
sub get_keyvalue {
my ($key, $value) = @_;
- unless (defined($value)) {
+ if (not defined($value)) {
if (exists($ENV{$key})) {
$value = $ENV{$key};
- } else {
+ }
+ else {
print STDERR "No such parameter: $key (perhaps you forgot to export it?)\n";
exit(1);
}
@@ -66,28 +73,32 @@ sub get_keyvalue {
return($key, $value);
}
-usage unless defined($action);
+if (
+ not defined $action
+ or ($action ~~ ['save', 'rm'] and not defined $arg)
+) {
+ usage;
+}
load_store;
+
given ($action) {
when ('save') {
- usage unless defined($arg);
- ($key, $value) = get_keyvalue($arg, $arg2);
+ my ($key, $value) = get_keyvalue($arg, $arg2);
$store{$key} = $value;
save_store;
}
when ('eval') {
- while (($key, $value) = each(%store)) {
+ while (my ($key, $value) = each(%store)) {
$value =~ s/'/'"'"'/g;
print "export $key='$value'\n";
}
}
when (['show', 'list']) {
- while (($key, $value) = each(%store)) {
+ while (my ($key, $value) = each(%store)) {
printf("%-15s = %s\n", $key, $value);
}
}
when ('rm') {
- usage unless defined($arg);
delete($store{$arg});
save_store;
}