-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsumsrc
More file actions
executable file
·124 lines (116 loc) · 5.16 KB
/
sumsrc
File metadata and controls
executable file
·124 lines (116 loc) · 5.16 KB
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
#!/bin/bash
_is_yielded=false;
yell() { echo "$@" >&2; }
die() { yell "$@"; exit 111; }
flag() { _is_yielded=true; yell "ERROR: $@"; }
yield() { if [ "$_is_yielded" == "true" ]; then die "Aborting because of above errors."; fi; }
SCRIPT_NAME="${0##*/}";
DEFAULT_DIGEST_LENGTH=32;
DEFAULT_OUTPUT_FORMAT='query';
DEFAULT_ALGORITHM_NAME_EXE='b2sum';
DEFAULT_ALGORITHM_NAME_SHORT='b2';
showHelp() {
echo "USAGE (STDIN or PATH modes):";
echo "[…] | ${SCRIPT_NAME} [OPTIONS]";
echo "${SCRIPT_NAME} [OPTIONS] PATH";
echo "";
echo "OPTIONS:";
echo " -h Show this help";
echo " -a A Set algorithm to A; any name that has a *sum command, e.g. b2, sha256.";
#echo " -f FORMAT";
#echo " Specify source FORMAT, so that comments can be escaped correctly (only sh and vim are used right now)";
echo " -j, --json Output JSON";
echo " -y, --yaml Output YAML";
echo " -t, --text Output plain text";
echo " -q, --query Output query string (default)";
echo " -l N Set digest length in bits, default ${DEFAULT_DIGEST_LENGTH} (see ${DEFAULT_ALGORITHM_NAME_SHORT} -l)";
echo "";
echo "EXAMPLE:";
echo " cat myfile.txt | ${SCRIPT_NAME}";
}
algorithmNameExe="$DEFAULT_ALGORITHM_NAME_EXE";
algorithmNameShort="$DEFAULT_ALGORITHM_NAME_SHORT";
targetFile=
opt_sumLength=$DEFAULT_DIGEST_LENGTH;
opt_outputFormat=$DEFAULT_OUTPUT_FORMAT;
while [ $# -gt 0 ]; do
case $1 in
-h) showHelp; exit;;
#-f) opt_inputFormat="$2"; shift;;
-a)
a="${2}";
ax="${a}sum";
if command -v "$ax" &>/dev/null; then
algorithmNameShort="$a";
algorithmNameExe="$ax";
shift;
fi;;
-j|--json) opt_outputFormat='json';;
-y|--yaml) opt_outputFormat='yaml';;
-t|--text) opt_outputFormat='plain';;
-q|--query) opt_outputFormat='query';;
-l) opt_sumLength=$2; shift;;
*) if [[ -z $targetFile ]]; then targetFile="$1"; else flag "Unknown arg '$1'"; fi;;
esac
shift;
done
if [[ -t 0 && ( -z "$targetFile" || ! -s "$targetFile" ) ]]; then
echo "Please provide input data, by STDIN (e.g. 'cat ~/myfile.txt | ${SCRIPT_NAME}') or as first non-option argument (e.g. '${SCRIPT_NAME} myfile.txt')" >&2;
echo '' >&2;
showHelp >&2;
exit 1;
fi
[[ $opt_sumLength =~ ^[0-9]+$ ]] || flag "Please specify only integer numbers for the length value of option '-l'.";
yield;
getOmitLinesPattern() {
local commentLineStart='#';
#local blockCommentPattern=''; # WISH, LATER: omit block comments. Maybe use some other program to do the comment identification (Vim)?
local filename=$1;
#echo "DEBUG(filename): ${filename}"; #XXX
if [[ -n $filename ]]; then
local ftype="${filename##*.}";
ftype="${ftype##*/}";
case $ftype in
vim|vimrc) commentLineStart='"';;
js|javascript|ts|typescript|c|cpp) commentLineStart='//';;
sh) commentLineStart='##';;
# LATER: add more.
esac
fi
local OMIT_KEYWORD='nosum';
# NOTE: grep is using -E extended RegExp option, so pipes are not escaped.
local pattern='^\s*$'; # empty line
pattern+='|^\s*'"${commentLineStart}"'[^!]'; # commented line, besides execution hint FIXME (B) 2024-06-05 only applies when commentLineStart='#'
pattern+='|'"${commentLineStart}${OMIT_KEYWORD}"'$'; # lines ENDING like '#nosum'
echo "$pattern";
}
if [[ -n $targetFile ]]; then
omitPattern="$(getOmitLinesPattern "$targetFile")";
data="$(grep -E -v "$omitPattern" "$targetFile")";
else
omitPattern="$(getOmitLinesPattern)";
data="$(grep -E -v "$omitPattern")";
fi
#>&2 echo -n "[DEBUG(omitPattern):\n${omitPattern}]";
#>&2 echo -n "[DEBUG(DATA):\n${data}]" | head -20;exit 130;
sumCommand=("$algorithmNameExe");
if [[ $algorithmNameShort == 'b2' && $opt_sumLength -gt 0 ]]; then
sumCommand+=(-l ${opt_sumLength});
fi
sum="$("${sumCommand[@]}" <(echo "$data") | awk '{ print $1 }')";
size="$(echo "$data" | wc -c| awk '{ print $1 }')";
case $opt_outputFormat in
json) echo "{\"sum\":\"${sum}\",\"size\":${size},\"date\":\"$(date '+%Y-%m-%dT%H:%M:%S%z')\",\"provider\":{\"name\":\"${SCRIPT_NAME}\",\"method\":\"${algorithmNameExe}\",\"length\":${opt_sumLength}}}";;
yaml) echo -e "---\nsum: ${sum}\nsize: ${size}\nprovider:\n name: ${SCRIPT_NAME}\n method: ${algorithmNameExe}\n length: ${opt_sumLength}\ndate: $(date '+%Y-%m-%dT%H:%M:%S%z')\n...";;
plain) echo "sum:${sum}#a=${algorithmNameShort},l=${opt_sumLength};size:${size};";;
query) echo "${algorithmNameExe}=${sum}&size=${size}";;
*) die "Unknown output form at '${opt_outputFormat}'";;
esac
## HISTORY:
# v0.1.0 2022-12-13 Optional JSON output. Adding -h documentation. b2sum=0c305a3b&size=1592
# v0.2.0 2022-12-16 2nd mode, PATH arg instead of STDIN; Renamed 'sumsrc' from 'sum-stream'. b2sum=bf10f132&size=2041
# v0.3.0 2022-12-19 Add query output format. Include '#!...' execution comment in sum. Change 'sum' key to '*sum' where '*' is algorithm short name. b2sum=b64e99f0&size=2364
# v0.4.0 2022-12-20 Adapt comment pattern to input source language (stub: only Vim by file name). b2sum=beaa596f&size=2691
# v0.5.0 2024-07-07 Fix omit pattern for grep -E. Adding to shell-utilities. b2sum=d23acaeb&size=2787
# v0.5.1 2025-04-25 Add full flags for --text, --query, for parameterized scripting. b2sum=c26fba89&size=3871
# STUB 2024-06-05 Support more formats, and allow specifying with input option arg.