about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAndinus <andinus@nand.sh>2020-08-27 12:39:21 +0530
committerAndinus <andinus@nand.sh>2020-08-27 12:39:21 +0530
commit391977a559a3130885d5c512f25d76207ac67b16 (patch)
treee8b9f93dc74322cf1b94d935d8555090d4e33f19
parentb99691cc076ce16b73a2dea8ca119b2229083a62 (diff)
downloadleo-391977a559a3130885d5c512f25d76207ac67b16.tar.gz
Add encrypt & sign option, remove archive_HelpMessage()
`archive_HelpMessage' was removed because variables from `archive' sub
are not shared with it.

On #perl@freenode:
    <andinus> i have a sub archive_Help inside of sub archive, when i try to
              use archive's var in archive_Help perl says "Variable
              "$archive_dir" will not stay shared at..."              [02:55]
    [...]
     <Grinnz> generally, you pass variables into subs that you call   [02:57]
     <Grinnz> defining a sub within another sub is evidence that you did it
              wrong
    [...]
    <andinus> is defining a sub within a sub not a good idea?
     <Grinnz> it does nothing useful                                  [03:01]
     <Grinnz> and causes issues like the one you are having
    [...]
-rwxr-xr-xleo.pl38
1 files changed, 21 insertions, 17 deletions
diff --git a/leo.pl b/leo.pl
index 9f6482b..3c20e8e 100755
--- a/leo.pl
+++ b/leo.pl
@@ -11,7 +11,7 @@ use Getopt::Long qw/ GetOptions /;
 my %options = ();
 GetOptions(
     \%options,
-    qw{ verbose }
+    qw{ verbose encrypt sign }
 ) or die "Error in command line arguments\n";
 
 
@@ -75,6 +75,9 @@ sub tar_list { run3 ["/bin/tar", "tvf", @_]; }
 
 # Creating tars of files.
 sub archive {
+    my $gpg_fingerprint = "D9AE4AEEE1F1B3598E81D9DFB67D55D482A799FD";
+    my $archive_dir = "/tmp/archive";
+
     my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
         localtime(time);
 
@@ -89,37 +92,30 @@ sub archive {
 
     my %archive_dispatch = (
         "documents" => sub {
-            tar_create("/tmp/archive/documents_$ymd.tar",
+            tar_create("$archive_dir/documents_$ymd.tar",
                        "-C", "$ENV{HOME}/documents", ".");
         },
         "journal" => sub {
-            tar_create("/tmp/archive/journal_$ymd.tar",
+            tar_create("$archive_dir/journal_$ymd.tar",
                        "-C", "$ENV{HOME}/documents",
                        "andinus.org.gpg", "archive.org.gpg");
         },
         "ssh" => sub {
-            tar_create("/tmp/archive/ssh_$ymd.tar",
+            tar_create("$archive_dir/ssh_$ymd.tar",
                        "-C", "$ENV{HOME}/.ssh", ".");
         },
         "pass" => sub {
-            tar_create("/tmp/archive/pass_$ymd.tar",
+            tar_create("$archive_dir/pass_$ymd.tar",
                        "-C", "$ENV{HOME}/.password-store", ".");
         },
     );
 
-    shift @ARGV;
-    if ( $ARGV[0]
-             and $archive_dispatch{ $ARGV[0] } ) {
-        path("/tmp/archive")->mkpath; # Create archive directory.
+    shift @ARGV; # Drop `archive' from @ARGV.
+    if ( $ARGV[0] and $archive_dispatch{ $ARGV[0] } ) {
+        path($archive_dir)->mkpath; # Create archive directory.
         $archive_dispatch{ $ARGV[0] }->();
     } elsif ( scalar @ARGV == 0 ) {
-        archive_HelpMessage();
-    } else {
-        say "leo/archive: no such option";
-    }
-
-    sub archive_HelpMessage {
-        say qq{Archive files to /tmp/archive.
+        say qq{Archive files to $archive_dir.
 
 Usage:
     documents
@@ -130,6 +126,14 @@ Usage:
     ssh
         Archive $ENV{HOME}/.ssh
     pass
-        Archive $ENV{HOME}/.password-store};
+        Archive $ENV{HOME}/.password-store
+
+Options:
+    --encrypt
+        Encrypt files with $gpg_fingerprint
+    --sign
+        Sign files with $gpg_fingerprint};
+    } else {
+        say "leo/archive: no such option";
     }
 }