-
Notifications
You must be signed in to change notification settings - Fork 0
/
file-output
executable file
·261 lines (234 loc) · 5.55 KB
/
file-output
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
#!/usr/bin/env bash
set -euo pipefail
function change_mode() {
if [ -n "${MODE}" ]
then
update_trap MODE_FAILED_TO_CHANGE
if chmod -c "${MODE}" "${FILE}" 2>&1 | grep -q '.'
then
update_result MODE_CHANGED
fi
update_trap OK
fi
}
function clear_trap() {
trap '' INT EXIT TERM
}
function create_file() {
if [ ! -e "${FILE}" ]
then
update_trap FILE_FAILED_TO_CREATE
touch "${FILE}"
update_result FILE_CREATED
update_trap OK
fi
}
function create_parents() {
if [ ! -e "$(dirname "${FILE}")" ]
then
update_trap PARENTS_FAILED_TO_CREATE
mkdir -p "$(dirname "${FILE}")"
update_result PARENTS_CREATED
update_trap OK
fi
}
function help() {
cat << EOF >&2
Usage:
${0} [OPTION]... [--] FILE
Write Options:
-A append
-N do not write any contents
-R replace (default)
Overwrite Options:
-a abort if already file exists (default)
-f overwrite
-n do not overwrite
Miscellaneous Options:
-c do not create any files
-h show help
-m MODE set file mode
-p create parent directories
EOF
}
function ignore_contents() {
update_trap CONTENTS_FAILED_TO_READ
cat - > /dev/null
update_trap OK
update_result CONTENTS_IGNORED_TO_OVERWRITE
}
function print_error() {
local file="$(relative_path "${FILE}")"
local message="${ERROR_MESSAGES[${1}]}"
local severity="${ERROR_SEVERITIES[${1}]}"
if which echo-with-color > /dev/null 2>&1
then
if [ ${severity} -ge 100 ]
then
echo-with-color -0 "${file}:" -R "${message}" >&2
elif [ ${severity} -ge 10 ]
then
echo-with-color -0 "${file}:" -Y "${message}" >&2
else
echo-with-color -0 "${file}:" -G "${message}" >&2
fi
else
echo "${file}: ${message}" >&2
fi
}
function print_result() {
print_error "${RESULT}"
}
function relative_path() {
realpath -ms "--relative-to=$(pwd)" "${1}"
}
function update_result() {
local cur_severity="${ERROR_SEVERITIES[${RESULT}]}"
local new_severity="${ERROR_SEVERITIES[${1}]}"
if [ ${cur_severity} -lt ${new_severity} ]
then
RESULT="${1}"
fi
}
function update_trap() {
trap "update_result '${1}'; print_result" INT EXIT TERM
}
function write_contents() {
update_trap CONTENTS_FAILED_TO_WRITE
local old_checksum="$(sha256sum "${FILE}")"
case "${WRITE_POLICY}" in
append)
cat - >> "${FILE}"
;;
no-write)
;;
replace)
cat - > "${FILE}"
;;
*)
clear_trap
echo "${WRITE_POLICY}: invalid write policy" >&2
exit 1
;;
esac
local new_checksum="$(sha256sum "${FILE}")"
if [ "${old_checksum}" != "${new_checksum}" ]
then
update_result CONTENTS_CHANGED
fi
update_trap OK
}
declare -A ERROR_MESSAGES=(
["CONTENTS_ABORTED_TO_OVERWRITE"]="already exists"
["CONTENTS_CHANGED"]="changed"
["CONTENTS_FAILED_TO_READ"]="failed to read from standard input"
["CONTENTS_FAILED_TO_WRITE"]="failed to write file"
["CONTENTS_IGNORED_TO_OVERWRITE"]="already exists, but ignored"
["FILE_CREATED"]="file created"
["FILE_FAILED_TO_CREATE"]="failed to create file"
["MODE_CHANGED"]="mode changed"
["MODE_FAILED_TO_CHANGED"]="failed to change mode"
["OK"]="OK"
["PARENTS_CREATED"]="parent directories created"
["PARENTS_FAILED_TO_CREATE"]="failed to create parent directories"
)
declare -A ERROR_SEVERITIES=(
["CONTENTS_ABORTED_TO_OVERWRITE"]=100
["CONTENTS_CHANGED"]=12
["CONTENTS_FAILED_TO_READ"]=100
["CONTENTS_FAILED_TO_WRITE"]=100
["CONTENTS_IGNORED_TO_OVERWRITE"]=1
["FILE_CREATED"]=13
["FILE_FAILED_TO_CREATE"]=100
["MODE_CHANGED"]=11
["MODE_FAILED_TO_CHANGED"]=100
["OK"]=0
["PARENTS_CREATED"]=10
["PARENTS_FAILED_TO_CREATE"]=100
)
CREATE_FILE=yes
CREATE_PARENTS=
FILE=
MODE=
OVERWRITE_POLICY=abort
RESULT=OK
WRITE_POLICY=replace
while getopts 'ANRacfhm:np' OPT
do
case "${OPT}" in
A)
WRITE_POLICY=append
;;
N)
WRITE_POLICY=no-write
;;
R)
WRITE_POLICY=replace
;;
a)
OVERWRITE_POLICY=abort
;;
c)
CREATE_FILE=
;;
f)
OVERWRITE_POLICY=force
;;
h)
help
exit 1
;;
m)
MODE="${OPTARG}"
;;
n)
OVERWRITE_POLICY=ignore
;;
p)
CREATE_PARENTS=yes
;;
*)
exit 1
;;
esac
done
shift $(($OPTIND - 1))
if [ ${#@} -ne 1 ]
then
help
exit 1
fi
FILE="${1}"
update_trap OK
if [ -e "${FILE}" ]
then
change_mode
if [ "${WRITE_POLICY}" == replace ]
then
case "${OVERWRITE_POLICY}" in
abort)
update_result CONTENTS_ABORTED_TO_OVERWRITE
exit 1
;;
force)
;;
ignore)
ignore_contents
exit 0
;;
*)
clear_trap
echo "${OVERWRITE_POLICY}: invalid overwrite policy" >&2
exit 1
;;
esac
fi
else
if [ -n "${CREATE_FILE}" ]
then
[ -n "${CREATE_PARENTS}" ] && create_parents
create_file
fi
change_mode
fi
write_contents