/
sync-dotfiles
executable file
·204 lines (153 loc) · 3.98 KB
/
sync-dotfiles
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
# gnu_compat: --opt="..." is allowed.
# no_bundling: single character option is not bundled.
# no_ignore_case: no ignore case on long option.
use Getopt::Long qw(:config gnu_compat no_bundling no_ignore_case);
use FindBin ();
use Pod::Usage;
use File::Spec::Functions qw(catfile);
use File::Basename qw(dirname);
use Perl6::Say;
use File::Path qw(rmtree);
use lib "$FindBin::Bin/lib";
use Dotto::Util qw(get_user_from_home get_home_from_user determine_user_and_home load_config convert_filename install hashize_arg_config);
sub usage () {
pod2usage(-verbose => 1);
}
my $needhelp = 0;
my $home;
my $config_file;
my $dry_run = 0;
my $verbose = 0;
my $force_overwrite = 0;
my $username;
my $directory;
my $extract = 0;
my $dereference = 0;
my @arg_config;
GetOptions(
'h|help'
=> \$needhelp,
'u|username=s'
=> \$username,
'c|config-file=s'
=> \$config_file,
'dry-run'
=> \$dry_run,
'v|verbose'
=> \$verbose,
'f|force'
=> \$force_overwrite,
'x|extract'
=> \$extract,
'd|directory=s'
=> \$directory,
'H|home-directory=s'
=> \$home,
'L|dereference'
=> \$dereference,
'C=s'
=> \@arg_config,
) or usage;
usage if $needhelp;
if (defined $home) {
$username = get_user_from_home $home;
unless (defined $username) {
die "error: can't determine username "
. "from home directory on your platform.";
}
}
elsif (defined $username) {
$home = get_home_from_user $username;
}
else {
($username, $home) = determine_user_and_home;
}
if (!defined $config_file && exists $ENV{DOTTORC}) {
$config_file = $ENV{DOTTORC};
}
unless (defined $config_file) {
die "error: specify config file with -c option.\n";
}
my $c = load_config($config_file);
if (@arg_config) {
%$c = (%$c, %{hashize_arg_config @arg_config});
}
my @files = @{$c->{files}};
if (!defined $directory && exists $c->{directory}) {
$directory = $c->{directory};
}
unless (defined $directory) {
warn "error: directory is undefined: please specify with -d {directory}\n";
usage;
}
# Install dotfiles to $home.
for my $file (@files) {
my ($src, $dest);
if ($extract) {
$src = catfile($directory, $file);
$dest = catfile($home, convert_filename $c, $file);
}
else {
$src = catfile($home, $file);
$dest = catfile($directory, $file);
}
if ($dry_run || $verbose) {
say "Copy $src -> $dest";
next if $dry_run;
}
if (-e $dest) {
unless ($force_overwrite) {
warn "warning: sync-dotfiles: File exists '$dest'.\n";
next;
}
rmtree($dest);
}
install($src, $dest, $username, {dereference => $dereference});
}
unless ($extract) {
for my $file (@{$c->{ignore_files}}) {
my $dest = catfile($directory, convert_filename $c, $file);
if (-e $dest) {
rmtree($dest);
}
}
}
__END__
=head1 NAME
sync-dotfiles - Copy dotfiles from directory to directory.
=head1 SYNOPSIS
$ sync-dotfiles [OPTIONS]
$ sync-dotfiles -x [OPTIONS]
=head1 OPTIONS
=over 10
=item -h, --help
Show help.
=item -u, --username {user}
Owner of copied dotfiles.
=item -c, --config-file {config-file}
Config file path to get dotfiles list.
=item -f, --force
Overwrite dotfiles if they exist.
=item --dry-run
Output verbose messages and do nothing.
=item -v, --verbose
Output verbose messages and do normal operations.
=item -x, --extract
Copy dotfiles from C<--directory> to home directory.
if this is not given,
Copy dotfiles from home directory to C<--directory>.
=item -d, --directory {directory}
Directory which contains dotfiles (not home directory).
=item -H, --home-directory {directory}
Directory of home directory.
If you want to specify home directory from username,
use C<-u> option.
=item -L, --dereference
Dereference symlinks if given.
=back
=head1 AUTHOR
tyru <tyru.exe@gmail.com>