diff options
author | Andinus <andinus@nand.sh> | 2020-08-27 11:09:13 +0530 |
---|---|---|
committer | Andinus <andinus@nand.sh> | 2020-08-27 11:09:13 +0530 |
commit | 9e478c175e2f178320f4eae851e3f4feccb7145e (patch) | |
tree | 08ffde9637cbd739cdbcb367488ae1d523b172d2 | |
parent | 5a88b1e4a36d84e4989d2c96690595744914a44a (diff) | |
download | leo-9e478c175e2f178320f4eae851e3f4feccb7145e.tar.gz |
Remove repetition, move checks to tar_create()
This does make tar_create() complex & confusing but that's okay because otherwise there would've been lots of repetition in program as I added more definied archive paths.
-rwxr-xr-x | leo.pl | 45 |
1 files changed, 30 insertions, 15 deletions
diff --git a/leo.pl b/leo.pl index 1964d43..fbcf243 100755 --- a/leo.pl +++ b/leo.pl @@ -38,7 +38,36 @@ sub HelpMessage { } sub rsync { run3 ["openrsync", @_]; } -sub tar_create { run3 ["/bin/tar", "cf", @_]; } + +# User must pass $tar_file first & `-C' optionally. +sub tar_create { + my $tar_file = shift @_; + + my ( $cwd, @archive_paths ); + # Passing `-C' won't print "tar: Removing leading / from absolute + # path names in the archive" to STDERR in some cases. + if ( $_[0] eq "-C" ) { + $cwd = $_[1]; + + # Remove `-C' & cwd to get @archive_paths; + my @tmp = @_; + shift @tmp; shift @tmp; + @archive_paths = @tmp; + } else { + @archive_paths = @_; + } + + say "Archive file: $tar_file\n"; + run3 ["/bin/tar", "cf", $tar_file, @_]; + + $? # tar returns 1 on errors. + ? die "Archive creation failed :: $?\n" + # Print absolute paths for all archived files/directories. + : say path($_)->absolute($cwd), " archived." + foreach @archive_paths; + + print "\n" and tar_list($tar_file) if $options{verbose}; +} sub tar_list { run3 ["/bin/tar", "tvf", @_]; } # Creating tars of files. @@ -46,26 +75,12 @@ sub archive { my %archive_dispatch = ( "documents" => sub { tar_create("/tmp/archive/documents.tar", - # Won't print "tar: Removing leading / from - # absolute path names in the archive" to - # STDERR. "-C", "$ENV{HOME}/documents", "."); - - $? # tar returns 1 on errors. - ? die "Archive creation failed :: $?\n" - : say "$ENV{HOME}/documents archived."; - tar_list("/tmp/archive/documents.tar") if $options{verbose}; }, "journal" => sub { tar_create("/tmp/archive/journal.tar", "-C", "$ENV{HOME}/documents", "andinus.org.gpg", "archive.org.gpg"); - - $? - ? die "Archive creation failed :: $?\n" - : say "$ENV{HOME}/documents/andinus.org.gpg, -$ENV{HOME}/documents/archive.org.gpg archived."; - tar_list("/tmp/archive/journal.tar") if $options{verbose}; }, ); |