-
Notifications
You must be signed in to change notification settings - Fork 6
/
bucket.fish
executable file
·277 lines (228 loc) · 6.79 KB
/
bucket.fish
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/usr/bin/env fish
# * bucket.fish
# ** Defaults
set bucket bucket # Default bucket name
set dir ~/.cache/bucket
set deleteCommand trash-put
set expireDays 14
# ** Functions
function debug --description "Print debug message in yellow"
# Do NOT use separate set_color commands, or else you risk
# polluting later command substitutions! See
# https://github.com/fish-shell/fish-shell/issues/2378
isset debug
and echo (set_color yellow)"DEBUG: $argv"(set_color normal) >&2
#and echo "DEBUG: $argv" >&2
end
function die --description "Print error message and quit"
echo (set_color red)"$argv"(set_color normal) >&2
#echo "$argv" >&2
exit 1
end
function verbose --description "Print message if verbose"
isset verbose
or isset reallyVerbose
and echo "$argv" >&2
end
function isset --description "Test if variables named by args are set"
# "set -q" everywhere gets old
set -q $argv
end
function usage
echo "Usage:"
echo " command | bucket [OPTIONS] [BUCKET]"
echo " bucket [OPTIONS] [BUCKET] [DATA]"
echo
echo "The first form reads data from STDIN and writes to the default bucket,
or BUCKET if given. The second form reads DATA and/or BUCKET from
arguments, writing to a bucket or printing a bucket's contents."
echo
echo "Options:"
echo " -a, --append Append to bucket"
echo " -d, --date Sort by date"
echo " -e, --edit Edit bucket"
echo " -E, --empty Empty bucket"
echo " -g PATTERN, --grep PATTERN Grep in buckets"
echo " -h, --help i can Haz cheezburger?"
echo " -l, --list List buckets"
echo " -v, --verbose Verbose output"
echo " -V, --VERBOSE VERY verbose output"
echo " -x, --expire eXpire old buckets (default: +$expireDays days)"
echo " --directory DIRECTORY Bucket storage directory"
end
# ** Args
while set optarg (getopts "a:append d:date D:debug e:edit E:empty g:grep h:help l:list v:verbose V:VERBOSE x:expire directory:" $argv)
switch $optarg[1]
case a
set append true
case d
set sort "-tr"
case D
set debug true
debug "Debugging on"
case e
set edit true
case E
set empty true
case g
set -e optarg[1]
set grep "$optarg" # Flatten list
break
case h
usage
exit
case l
set list true
case v
set verbose -v
case V
set reallyVerbose true
case x
set expire
case directory
set customDir $optarg[2]
debug "Using custom directory: $customDir"
case \*
usage
echo
die "Unknown option: $optarg[1]"
end
end
set args $optarg
set numargs (count $args)
# *** Check STDIN and bucket name
if not isatty stdin
debug "Data from STDIN"
set stdin true
if test $numargs -eq 0
debug "No args; using default bucket"
else if test $numargs -eq 1
debug "One arg; using bucket: $args"
set bucket $args
else
debug "Multiple args; using bucket $args[1]"
verbose "Ignoring extra arguments"
set bucket $args[1]
end
else
debug "No data from STDIN"
if test $numargs -eq 0
debug "No args"
else if test $numargs -eq 1
if test -f $dir/$args
# Bucket exists with name; pour it
set bucket $args
debug "Using bucket $bucket; no data remaining in args"
else
# No such bucket; use args as data for default bucket
set data $args
debug "No bucket named '$args'; using args as data for default bucket"
end
else
# Multiple args
set bucket $args[1]
set -e args[1]
set data $args
debug "Using bucket $bucket; using $numargs remaining args as data"
end
end
# **** Sanitize bucket name (since it's passed to eval and trash-put/rm)
set bucket (echo $bucket | sed -r 's/[~.]//g')
# *** Check for conflicting args
if begin; isset empty; and isset expire; end
set conflicting true
end
if isset conflicting
die "Conflicting operations given."
end
# ** Check directory
if isset customDir
# Custom bucket directory; don't make it
test -d $customDir
or die "Directory doesn't exist: $customDir"
set dir $customDir
else
# Standard bucket directory; make it if necessary
if not test -d $dir
# Make dir
mkdir -p $dir
or die "Unable to make bucket directory: $dir"
end
end
# cd just to be extra safe
cd $dir
or die "Unable to enter bucket directory: $dir"
# ** Main
if isset list
# *** List buckets
debug "Listing buckets"
if isset verbose
for file in (ls $sort)
echo -e (set_color blue)$file(set_color normal)": " (head -n1 $file)
#echo "$file:" (head -n1 $file)
end
else if isset reallyVerbose
for file in (ls $sort)
echo -e (set_color blue)$file(set_color normal)":"
#echo "$file:"
cat $file
echo
end
else
ls
end
else if isset grep
# *** Grep buckets
debug "Grepping buckets"
grep -i $grep *
else if isset edit
# *** Edit bucket
debug "Editing bucket"
eval "$EDITOR \"$dir/$bucket\""
else if isset empty
# *** Empty bucket
debug "Emptying bucket $bucket"
if not test -f "$dir/$bucket"
verbose "No such bucket"
else
eval "$deleteCommand $verbose \"$dir/$bucket\""
end
else if isset expire
# *** Expire buckets
debug "Expiring buckets"
find $dir -type f -mtime +$expireDays -exec $deleteCommand $verbose '{}' +
else
if begin; not isset stdin; and not isset data; end
# *** Pour bucket
debug "Pouring bucket..."
if not test -r "$dir/$bucket"
verbose "No such bucket"
else if not test -s "$dir/$bucket"
verbose "Bucket is empty"
else
cat "$dir/$bucket"
end
else
# *** Fill bucket
debug "Filling bucket..."
if isset stdin
debug "Catting STDIN"
if isset append
cat >>"$dir/$bucket"
else
cat >"$dir/$bucket"
end
else
debug "Echoing data"
if isset append
echo $data >>"$dir/$bucket"
else
echo $data >"$dir/$bucket"
end
end
# **** Display bucket if verbose
isset verbose
or isset reallyVerbose
and cat "$dir/$bucket"
end
end