commit 51caf74

Emilia Smólska  ·  2026-06-11 13:46:54 +0000 UTC
parent edb3793
add $HASLODIR, haslo list, refactor
1 files changed,  +44, -21
M haslo
M haslo
+44, -21
 1@@ -9,14 +9,12 @@ use MIME::Base64 qw(encode_base64);
 2 
 3 sub usage
 4 {
 5-	die "usage: haslo put|get|gen name\n";
 6+	die "usage:\thaslo gen|get|put name\n\thaslo list\n";
 7 }
 8 
 9-my $command = $ARGV[0] or usage;
10-my $target = $ARGV[1] or usage;
11 my $home = $ENV{'HOME'} or die "\$HOME not set\n";
12 my $hasloemail = $ENV{'HASLOEMAIL'} or die "\$HASLOEMAIL not set\n";
13-my $haslodir = "$home/.haslo";
14+my $haslodir = $ENV{'HASLODIR'} || "$home/.haslo";
15 
16 sub writegpgpassword
17 {
18@@ -24,13 +22,46 @@ sub writegpgpassword
19 		print STDERR "creating directory $haslodir\n";
20 		mkdir $haslodir or die "couldn't create directory $haslodir: $!\n";
21 	}
22+	my $target = shift;
23 	my $password = shift;
24-	open GPG, '|-', 'gpg', '-qer', $hasloemail, '-o', "$haslodir/$target.gpg" or die "can't open pipe to gpg: $!\n";
25+	open GPG, '|-', 'gpg', '-qer', $hasloemail, '-o', "$haslodir/$target.gpg"
26+		or die "couldn't open pipe to gpg: $!\n";
27 	print GPG $password;
28 	close GPG or die "gpg failed with exit code $?\n";
29 }
30 
31-if($command eq 'put'){
32+sub dogen
33+{
34+	my $target = $ARGV[1] or usage;
35+	my $buf = '';
36+	open URANDOM, '<', '/dev/urandom' or die "couldn't open /dev/urandom: $!\n";
37+	read URANDOM, $buf, 128 or die "couldn't read from /dev/urandom: $!\n";
38+	close URANDOM;
39+	$buf = encode_base64 $buf;
40+	writegpgpassword $target, $buf;
41+}
42+
43+sub doget
44+{
45+	my $target = $ARGV[1] or usage;
46+	open GPG, '-|', 'gpg', '-qd', "$haslodir/$target.gpg" or die "couldn't open pipe to gpg: $!\n";
47+	my $password = <GPG>;
48+	close GPG or die "gpg failed with exit code $?\n";
49+	print $password;
50+}
51+
52+sub dolist
53+{
54+	opendir DIR, $haslodir or die "couldn't open $haslodir: $!\n";
55+	for(readdir DIR){
56+		print "$_\n" if s/.gpg$//;
57+	}
58+	close DIR;
59+}
60+
61+sub doput
62+{
63+	my $target = $ARGV[1] or usage;
64 	my($password, $confirm);
65 	do{
66 		ReadMode 'noecho';
67@@ -42,18 +73,10 @@ if($command eq 'put'){
68 		print STDERR "\n";
69 		ReadMode 'restore';
70 	}while($password ne $confirm);
71-	writegpgpassword $password;
72-}elsif($command eq 'get'){
73-	open GPG, '-|', 'gpg', '-qd', "$haslodir/$target.gpg" or die "can't open pipe to gpg: $!\n";
74-	my $password = <GPG>;
75-	close GPG or die "gpg failed with exit code $?\n";
76-	print $password;
77-}elsif($command eq 'gen'){
78-	my $buf = '';
79-	open URANDOM, '<', '/dev/urandom' or die "can't open /dev/urandom: $!\n";
80-	read URANDOM, $buf, 128 or die "can't read from /dev/urandom: $!\n";
81-	$buf = encode_base64 $buf;
82-	writegpgpassword $buf;
83-}else{
84-	usage;
85-}
86+	writegpgpassword $target, $password;
87+}
88+
89+my %commands = (gen => \&dogen, get => \&doget, list => \&dolist, put => \&doput);
90+my $argv0 = $ARGV[0] or usage;
91+my $command = $commands{$argv0} or usage;
92+$command->();