Sun, 29 Jan 2017 00:07:24 -0500
Start a hammerspoon config!
476 | 1 | #!/usr/bin/env perl |
2 | ||
3 | use warnings; | |
4 | use strict; | |
5 | use Time::Piece; | |
6 | use Path::Tiny; | |
7 | use Getopt::Long::Descriptive; | |
8 | ||
9 | sub main { | |
10 | my ($opt, $usage) = describe_options( | |
11 | 'archive-dir %o <directory> ...', | |
12 | ['dest|d=s' => "destination path"], | |
13 | ['subdir' => hidden => { | |
14 | one_of => [ | |
15 | ['year|y' => 'file by year'], | |
16 | ['month|m' => 'file by month'], | |
17 | ], | |
18 | default => 'year', | |
19 | } | |
20 | ], | |
21 | ['age|a=i' => "minimum age in days for archival", {default => 60}], | |
477
eeafc178ddc4
add -f for only plain files
Meredith Howard <mhoward@roomag.org>
parents:
476
diff
changeset
|
22 | ['files|f' => "operate on plain files only"], |
eeafc178ddc4
add -f for only plain files
Meredith Howard <mhoward@roomag.org>
parents:
476
diff
changeset
|
23 | ['yes|y' => "actually move stuff"], |
476 | 24 | ['help|h' => "print usage message and exit", {shortcircuit => 1}], |
25 | ); | |
485
a58682115510
print usage on no args too
Meredith Howard <mhoward@roomag.org>
parents:
477
diff
changeset
|
26 | print($usage->text), exit if $opt->help || !@ARGV; |
476 | 27 | |
28 | archive_dir($opt, $_) for @ARGV; | |
29 | } | |
30 | ||
31 | sub archive_dir { | |
32 | my $opt = shift; | |
33 | my $dir = path(shift); | |
34 | my $destdir = path($opt->dest // '.'); | |
35 | $destdir = $dir->child($destdir) if $destdir->is_relative; | |
36 | my $destfmt = ($opt->subdir eq 'month') ? '%Y-%m' : '%Y'; | |
37 | my $nowish = time; | |
38 | my $age = $opt->age * 24 * 60 * 60; | |
39 | ||
40 | for my $child ($dir->children) { | |
477
eeafc178ddc4
add -f for only plain files
Meredith Howard <mhoward@roomag.org>
parents:
476
diff
changeset
|
41 | next if $child->is_dir && ( |
eeafc178ddc4
add -f for only plain files
Meredith Howard <mhoward@roomag.org>
parents:
476
diff
changeset
|
42 | $opt->files || $child eq $destdir || $child =~ /^(?:\d{4}|\d\d)$/ |
eeafc178ddc4
add -f for only plain files
Meredith Howard <mhoward@roomag.org>
parents:
476
diff
changeset
|
43 | ); |
eeafc178ddc4
add -f for only plain files
Meredith Howard <mhoward@roomag.org>
parents:
476
diff
changeset
|
44 | next if $opt->files && $child->basename =~ /^\./; |
476 | 45 | |
46 | my $mtime = $child->stat->mtime; | |
47 | next unless ($nowish - $mtime) >= $age; | |
48 | ||
49 | my $dest = $destdir->child(localtime($mtime)->strftime($destfmt)); | |
50 | ||
51 | print "$dest \t $child\n"; | |
52 | ||
53 | next unless $opt->yes; | |
54 | $dest->mkpath; | |
55 | $child->move($dest . '/' . $child->relative($dir)); | |
56 | } | |
57 | } | |
58 | ||
59 | main(); |