From b64034d27406519ef7aa5601b22c8855c5420abd Mon Sep 17 00:00:00 2001 From: Andinus Date: Thu, 27 Aug 2020 16:00:05 +0530 Subject: Add several paths to dispatch table, simplify table generation I added a lot of things to profile, there was a lot of repetition in dispatch table so I moved it to a function. Aliases were used for inconvenient paths but I could've done this too: my %directories = ( ssh => "$ENV{HOME}/.ssh", pass => "$ENV{HOME}/.password-store", ); foreach my $dir (sort keys %directories) { $dispatch{$dir} = sub { archive("$archive_dir/${dir}_${ymd}.tar", "-C", "$directories{$dir}", "."); }; } --- leo.pl | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) (limited to 'leo.pl') diff --git a/leo.pl b/leo.pl index b82ed74..3d4e6b3 100755 --- a/leo.pl +++ b/leo.pl @@ -24,11 +24,7 @@ my $ymd = ymd(); # YYYY-MM-DD. # Dispatch table. my %dispatch = ( - "documents" => sub { - archive("$archive_dir/documents_$ymd.tar", - "-C", "$ENV{HOME}/documents", "."); - }, - "journal" => sub { + journal => sub { my $tmp = $options{encrypt} and undef $options{encrypt} if $options{encrypt}; archive("$archive_dir/journal_$ymd.tar", @@ -36,16 +32,22 @@ my %dispatch = ( "andinus.org.gpg", "archive.org.gpg"); $options{encrypt} = $tmp; }, - "ssh" => sub { - archive("$archive_dir/ssh_$ymd.tar", - "-C", "$ENV{HOME}/.ssh", "."); - }, - "pass" => sub { - archive("$archive_dir/pass_$ymd.tar", - "-C", "$ENV{HOME}/.password-store", "."); - }, ); +# This adds the directories that have same path relative to $ENV{HOME} +# as profile name. +foreach my $profile (qw( emails music projects documents .ssh + .password-store)) { + $dispatch{$profile} = sub { + archive("$archive_dir/${profile}_$ymd.tar", + "-C", "$ENV{HOME}/$profile", "."); + }; +} + +# Aliases for inconvenient paths. +$dispatch{ssh} = $dispatch{".ssh"}; +$dispatch{pass} = $dispatch{".password-store"}; + # User must pass $tar_file first & `-C' optionally. sub archive { my $tar_file = shift @_; @@ -134,11 +136,11 @@ HelpMessage() if scalar @ARGV == 0; path($archive_dir)->mkpath; # Create archive directory. foreach my $arg ( @ARGV ) { - say "--------------------------------"; if ( $dispatch{ $arg } ) { + say "--------------------------------"; $dispatch{ $arg }->(); } else { - die say "leo: no such option\n"; + die "leo: no such option\n"; } } -- cgit 1.4.1-2-gfad0