about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAndinus <andinus@nand.sh>2020-08-27 16:00:05 +0530
committerAndinus <andinus@nand.sh>2020-08-27 16:00:43 +0530
commitb64034d27406519ef7aa5601b22c8855c5420abd (patch)
tree46a620d3b1ed1629f727f7b3e66d806ac7dfbe5f
parenta263b887df4f22d12f119acb40c9f5ca8d76b3ca (diff)
downloadleo-b64034d27406519ef7aa5601b22c8855c5420abd.tar.gz
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}", ".");
        };
    }
-rwxr-xr-xleo.pl32
1 files changed, 17 insertions, 15 deletions
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";
     }
 }