diff options
author | Andinus <andinus@nand.sh> | 2020-08-27 13:37:39 +0530 |
---|---|---|
committer | Andinus <andinus@nand.sh> | 2020-08-27 13:37:39 +0530 |
commit | 34857c394799933bbdd93fe1673de5cc8469e2f9 (patch) | |
tree | 967cf009693a1270ecefd2e1be04e8303700a5c2 | |
parent | 748a3fb76c324d72ea2c62e1f7aa7da35e066410 (diff) | |
download | leo-34857c394799933bbdd93fe1673de5cc8469e2f9.tar.gz |
Restructure code, rename tar_create to archive
Moved @ARGV parsing & HelpMessage sub to the end. tar_create is now a simple wrapper around `/bin/tar'.
-rwxr-xr-x | leo.pl | 85 |
1 files changed, 41 insertions, 44 deletions
diff --git a/leo.pl b/leo.pl index 116a9d6..64b461f 100755 --- a/leo.pl +++ b/leo.pl @@ -21,57 +21,26 @@ my $ymd = ymd(); # YYYY-MM-DD. # Dispatch table. my %dispatch = ( "documents" => sub { - tar_create("$archive_dir/documents_$ymd.tar", - "-C", "$ENV{HOME}/documents", "."); + archive("$archive_dir/documents_$ymd.tar", + "-C", "$ENV{HOME}/documents", "."); }, "journal" => sub { - tar_create("$archive_dir/journal_$ymd.tar", - "-C", "$ENV{HOME}/documents", - "andinus.org.gpg", "archive.org.gpg"); + archive("$archive_dir/journal_$ymd.tar", + "-C", "$ENV{HOME}/documents", + "andinus.org.gpg", "archive.org.gpg"); }, "ssh" => sub { - tar_create("$archive_dir/ssh_$ymd.tar", - "-C", "$ENV{HOME}/.ssh", "."); + archive("$archive_dir/ssh_$ymd.tar", + "-C", "$ENV{HOME}/.ssh", "."); }, "pass" => sub { - tar_create("$archive_dir/pass_$ymd.tar", - "-C", "$ENV{HOME}/.password-store", "."); + archive("$archive_dir/pass_$ymd.tar", + "-C", "$ENV{HOME}/.password-store", "."); }, ); -if ( $ARGV[0] and $dispatch{ $ARGV[0] } ) { - path($archive_dir)->mkpath; # Create archive directory. - $dispatch{ $ARGV[0] }->(); -} elsif ( scalar @ARGV == 0 ) { - HelpMessage(); -} else { - die say "leo: no such option\n"; -} - -sub HelpMessage { - say qq{Archive files to $archive_dir. - -Usage: - documents - Archive $ENV{HOME}/documents - journal - Archive $ENV{HOME}/documents/andinus.org.gpg, - $ENV{HOME}/documents/archive.org.gpg - ssh - Archive $ENV{HOME}/.ssh - pass - Archive $ENV{HOME}/.password-store - -Options: - --encrypt - Encrypt files with $gpg_fingerprint - --sign - Sign files with $gpg_fingerprint}; -} - - # User must pass $tar_file first & `-C' optionally. -sub tar_create { +sub archive { my $tar_file = shift @_; my ( $cwd, @archive_paths ); @@ -92,7 +61,7 @@ sub tar_create { warn "$tar_file exists, might overwrite.\n" if -e $tar_file; print "\n"; - run3 ["/bin/tar", "cf", $tar_file, @_]; + tar_create($tar_file, @_) $? # tar returns 1 on errors. ? die "Archive creation failed :: $?\n" @@ -102,6 +71,8 @@ sub tar_create { print "\n" and tar_list($tar_file) if $options{verbose}; } + +sub tar_create { run3 ["/bin/tar" , "cf", @_]; } sub tar_list { run3 ["/bin/tar", "tvf", @_]; } sub ymd { @@ -118,6 +89,32 @@ sub ymd { return "$year-$month-$mday"; } -# Creating tars of files. -sub archive { +sub HelpMessage { + say qq{Archive files to $archive_dir. + +Usage: + documents + Archive $ENV{HOME}/documents + journal + Archive $ENV{HOME}/documents/andinus.org.gpg, + $ENV{HOME}/documents/archive.org.gpg + ssh + Archive $ENV{HOME}/.ssh + pass + Archive $ENV{HOME}/.password-store + +Options: + --encrypt + Encrypt files with $gpg_fingerprint + --sign + Sign files with $gpg_fingerprint}; +} + +if ( $ARGV[0] and $dispatch{ $ARGV[0] } ) { + path($archive_dir)->mkpath; # Create archive directory. + $dispatch{ $ARGV[0] }->(); +} elsif ( scalar @ARGV == 0 ) { + HelpMessage(); +} else { + die say "leo: no such option\n"; } |