Skip to content

Commit 9dd83b1

Browse files
committed
Implement status and restore commands
1 parent 7b3e0af commit 9dd83b1

File tree

3 files changed

+46
-9
lines changed

3 files changed

+46
-9
lines changed

bin/...

+46-9
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ Commands:
1616
1717
list - List all the full paths of the files that would be installed.
1818
19+
status - Get change status of all the dot repositories.
20+
1921
update - Fetch changes from all the dot repositories.
2022
2123
upgrade - Do update and then install.
@@ -57,6 +59,7 @@ my $root_dir; # ~/.../
5759
my $conf_file; # ~/.../conf
5860
my $config; # Config hash
5961
my $timestamp; # Time stamp
62+
my $restore_from; # Directory to restore from
6063

6164
TOP: {
6265
my $class = __PACKAGE__;
@@ -81,6 +84,10 @@ sub parse_command_line {
8184
}
8285
else {
8386
$command = $ARGV[0];
87+
if ($command eq 'restore') {
88+
$restore_from = splice @ARGV, 1, 1
89+
or die "Error: 'restore' command requires a backup directory argument";
90+
}
8491
}
8592
if (@ARGV > 1) {
8693
die "Error: invalid usage. Try: '... -h'.\n";
@@ -148,7 +155,7 @@ sub handle_backup {
148155
my $quiet = shift or 0;
149156
my $backup_dir = "$root_dir/backup/$timestamp";
150157
my $backup_list_file = "$root_dir/tmp/$timestamp-backup-list";
151-
print "Backing up your dot files:\n";
158+
print "Backing up your dot files to $backup_dir/\n";
152159
open F, "> $backup_list_file";
153160
my $n = 0;
154161
for my $file (sort keys %{$class->_all_files}) {
@@ -207,21 +214,39 @@ sub handle_list {
207214

208215
sub handle_update {
209216
my $class = shift;
210-
die "Error: 'update' command not yet implemented\n";
217+
print "Updating your `...` system and dot files:\n";
218+
for my $dir ($root_dir, $class->_all_dot_paths) {
219+
next unless -d "$dir/.git";
220+
my $cmd = "(cd $dir; git pull)";
221+
$class->_run_sys($cmd);
222+
}
223+
}
224+
225+
sub handle_status {
226+
my $class = shift;
227+
for my $dir ($root_dir, $class->_all_dot_paths) {
228+
next unless -d "$dir/.git";
229+
my $cmd = "(cd $dir; git status)";
230+
$class->_run_sys($cmd);
231+
}
211232
}
212233

213234
sub handle_upgrade {
214235
my $class = shift;
215-
die "Error: 'upgrade' command not yet implemented\n";
216-
print "Updating your `...` system and dot files:\n";
217236
$class->handle_update;
218237
$class->handle_install;
219238
}
220239

221240
sub handle_restore {
222241
my $class = shift;
242+
die "'$restore_from' is not a backup directory"
243+
unless -d $restore_from;
244+
223245
$class->handle_backup('quiet') if $config->{auto_backup};
224-
die "Error: 'restore' command not yet implemented\n";
246+
print "Restoring your dot files from $restore_from\n";
247+
my $cmd = "(cd $restore_from; find . | cpio -dump $home_dir)";
248+
$class->_run_sys($cmd);
249+
print "Restore complete.\n";
225250
}
226251

227252
sub handle_remove {
@@ -231,9 +256,12 @@ sub handle_remove {
231256
my $all_files = $class->_all_files;
232257
my $n = 0;
233258
for my $file (sort keys %$all_files) {
234-
my $cmd = "unlink $file";
259+
my $cmd = "rm -f $file";
235260
$class->_run_sys($cmd);
236-
$n++; } print "Deleted $n dot files\n"; }
261+
$n++;
262+
}
263+
print "Deleted $n dot files\n";
264+
}
237265

238266
sub _run_sys {
239267
my $class = shift;
@@ -260,9 +288,9 @@ sub _up_to_date {
260288
return <S> eq <D>;
261289
}
262290

263-
sub _all_files {
291+
sub _all_dot_paths {
264292
my $class = shift;
265-
my $all_files = {};
293+
my @paths;
266294
for my $entry (@{$config->{dot_paths}}) {
267295
my $path = $entry->{path};
268296
if ($path !~ /^\//) {
@@ -275,6 +303,15 @@ sub _all_files {
275303
unless $path =~ /^\//;
276304
die "Error: '$path' is not a directory\n"
277305
unless -d $path;
306+
push @paths, $path;
307+
}
308+
return @paths;
309+
}
310+
311+
sub _all_files {
312+
my $class = shift;
313+
my $all_files = {};
314+
for my $path ($class->_all_dot_paths) {
278315
for my $file (`(cd $path; find . -type f)`) {
279316
$file =~ s!^\./!!;
280317
chomp($file);

src/_ renamed to tmp/...

File renamed without changes.

tmp/_

Whitespace-only changes.

0 commit comments

Comments
 (0)