about summary refs log tree commit diff stats
path: root/leo.pl
diff options
context:
space:
mode:
Diffstat (limited to 'leo.pl')
-rwxr-xr-xleo.pl82
1 files changed, 50 insertions, 32 deletions
diff --git a/leo.pl b/leo.pl
index dcb8447..ccdc758 100755
--- a/leo.pl
+++ b/leo.pl
@@ -6,8 +6,11 @@ use feature 'say';
 
 use IPC::Run3;
 use Path::Tiny;
+use Config::Tiny;
 use Getopt::Long qw/ GetOptions /;
 
+# Options.
+
 my %options = (
     encrypt => $ENV{LEO_ENCRYPT},
     sign => $ENV{LEO_SIGN},
@@ -19,66 +22,76 @@ GetOptions(
     qw{ verbose encrypt sign delete help }
 ) or die "Error in command line arguments\n";
 
-# Config file for leo.
+# Configuration.
+
 my $config_file = $ENV{XDG_CONFIG_HOME} || "$ENV{HOME}/.config";
-$config_file .= "/leo.pl";
+$config_file .= "/leo.conf";
 
-require "$config_file";
+my $config = Config::Tiny->new;
+$config = Config::Tiny->read( $config_file )
+    or die "Cannot read config file: `$config_file'\n";
+
+# Reading config file.
 
 my $ymd = ymd(); # YYYY-MM-DD.
-my $backup_dir = get_backup_dir() || "/tmp/backups";
+my $backup_dir = $config->{_}->{backup_dir} || "/tmp/backups";
 $backup_dir .= "/$ymd";
 
 path($backup_dir)->mkpath; # Create backup directory.
-my $prof;
 
+my $gpg_fingerprint = $config->{_}->{gpg_fingerprint};
+my $gpg_bin = $config->{_}->{gpg_bin};
 
-my %profile = get_profile();
-my $gpg_fingerprint = get_gpg_fingerprint();
-my $gpg_bin = get_gpg_bin();
+my %profile;
 
+foreach my $section (sort keys $config->%*) {
+    next if $section eq "_";
+    foreach my $key (sort keys $config->{$section}->%*) {
+        push @{ $profile{$section} }, $key;
+    }
+}
+
+# Print help.
 HelpMessage() and exit 0 if scalar @ARGV == 0 or $options{help};
+
+# Parsing the arguments.
 foreach my $arg ( @ARGV ) {
-    $prof = $arg; # Set $prof.
     if ( $profile{ $arg } ) {
         say "++++++++********++++++++";
 
-        # No encryption for journal profile.
-        my $tmp = $options{encrypt} and undef $options{encrypt}
-            if $prof eq "journal" and $options{encrypt};
-
         # Deref the array here because we want flattened list.
         backup("$backup_dir/${arg}.tar", $profile{$arg}->@*);
-
-        $options{encrypt} = $tmp if $prof eq "journal";
     } elsif ( -e $arg ) {
         # If the file/directory exist then create a new profile & run
         # backup.
         say "++++++++********++++++++";
-        backup("$backup_dir/${arg}.tar",
-               # backup() is expecting path relative to $ENV{HOME}.
-               path($arg)->relative($ENV{HOME}));
+        warn "[WARN] leo: creating temporary profile: `$arg'\n";
+        backup("$backup_dir/${arg}.tar", $arg);
     } else {
         warn "[WARN] leo: no such profile :: `$arg' \n";
     }
 }
 
-# User must pass $tar_file first.
 sub backup {
     my $tar_file = shift @_;
-    my @backup_paths = @_;
+
+    # Make @backup_paths relative to '/'.
+    my @backup_paths;
+    while (my $path = shift @_) {
+        push @backup_paths, path( $path )->relative('/');
+    }
 
     say "Backup: $tar_file";
     warn "[WARN] $tar_file exists, might overwrite.\n" if -e $tar_file;
     print "\n";
-    # All paths should be relative to $ENV{HOME}.
-    tar_create($tar_file, "-C", $ENV{HOME}, @_);
+
+    # All paths should be relative to '/'.
+    tar_create($tar_file, "-C", '/', @backup_paths);
 
     $? # tar returns 1 on errors.
         ? die "Backup creation failed :: $?\n"
         # Print absolute paths for all backup files/directories.
-        : say path($_)->absolute($ENV{HOME}), " backed up."
-        foreach @backup_paths;
+        : say path($_)->absolute('/'), " backed up." foreach @backup_paths;
 
     print "\n" and tar_list($tar_file) if $options{verbose};
     encrypt_sign($tar_file) if $options{encrypt} or $options{sign};
@@ -120,19 +133,24 @@ Profile:};
         print "    $prof\n";
         print "        $_\n" foreach $profile{$prof}->@*;
     }
-    say qq{
+    print qq{
 Options:
-    --encrypt
-        Encrypt files with $gpg_fingerprint
-    --sign
-        Sign files with $gpg_fingerprint
-    --delete
-        Delete the tar file after running $gpg_bin
+    --encrypt };
+    print "[Enabled]" if $ENV{LEO_ENCRYPT};
+    print qq{
+        Encrypt files with $gpg_fingerprint\n
+    --sign };
+        print "[Enabled]" if $ENV{LEO_SIGN};
+    print qq{
+        Sign files with $gpg_fingerprint\n
+    --delete };
+            print "[Enabled]" if $ENV{LEO_DELETE};
+    print qq{
+        Delete the tar file after running $gpg_bin\n
     --verbose
     --help};
 }
 
-
 sub tar_create { run3 ["/bin/tar", "cf", @_]; }
 sub tar_list { run3 ["/bin/tar", "tvf", @_]; }
 
mor/getwtxt/commit/handlers_test.go?id=126c00e4329f3e1f0efa210318f022f4248c53e2'>126c00e ^
e75c7fd ^







126c00e ^

3b6d1cc ^


e75c7fd ^

53b313e ^
e75c7fd ^


53b313e ^
e75c7fd ^
3d2994b ^
e75c7fd ^

126c00e ^
3b6d1cc ^
126c00e ^

3b6d1cc ^
126c00e ^
a9d7d11 ^



























126c00e ^

a9d7d11 ^
53b313e ^
a9d7d11 ^
53b313e ^
a9d7d11 ^


53b313e ^
a9d7d11 ^
53b313e ^

a9d7d11 ^


53b313e ^
a9d7d11 ^


53b313e ^
a9d7d11 ^
















53b313e ^
a9d7d11 ^

53b313e ^
a9d7d11 ^


126c00e ^
a9d7d11 ^
126c00e ^
a1e9de4 ^
126c00e ^

e75c7fd ^
53b313e ^
126c00e ^

e75c7fd ^
53b313e ^
a1e9de4 ^
126c00e ^
53b313e ^

126c00e ^


53b313e ^
e75c7fd ^






126c00e ^

e75c7fd ^





53b313e ^
e75c7fd ^



126c00e ^

e539d23 ^
53b313e ^
a1e9de4 ^
45a6722 ^
e539d23 ^
53b313e ^
a1e9de4 ^
45a6722 ^
53b313e ^

e539d23 ^



53b313e ^
45a6722 ^


e539d23 ^


45a6722 ^

8d44eb0 ^
e539d23 ^





53b313e ^
e539d23 ^




d6fbc25 ^



6753171 ^
d6fbc25 ^
6753171 ^
d6fbc25 ^


e539d23 ^
d6fbc25 ^

3d2994b ^
53b313e ^
d6fbc25 ^
53b313e ^

d6fbc25 ^
53b313e ^
d6fbc25 ^


e75c7fd ^
d6fbc25 ^



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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260