50 lines
1.0 KiB
Bash
Executable File
50 lines
1.0 KiB
Bash
Executable File
#!/usr/bin/env sh
|
|
# worklog-target.sh: 產生 worklog wiki 目標頁。
|
|
# 用法:
|
|
# worklog-target.sh <owner/repo> [hash|page|contents|all]
|
|
set -eu
|
|
|
|
usage() {
|
|
echo 'usage: worklog-target.sh <owner/repo> [hash|page|contents|all]' >&2
|
|
exit 2
|
|
}
|
|
|
|
[ "$#" -ge 1 ] || usage
|
|
[ "$#" -le 2 ] || usage
|
|
|
|
repo=$1
|
|
field=${2:-page}
|
|
|
|
if [ -z "$repo" ]; then
|
|
usage
|
|
fi
|
|
|
|
if command -v sha1sum >/dev/null 2>&1; then
|
|
raw=$(printf '%s' "$repo" | sha1sum | awk '{print $1}')
|
|
elif command -v shasum >/dev/null 2>&1; then
|
|
raw=$(printf '%s' "$repo" | shasum -a 1 | awk '{print $1}')
|
|
else
|
|
echo 'no SHA-1 helper found' >&2
|
|
exit 1
|
|
fi
|
|
|
|
hash=$(printf '%s' "$raw" | cut -c1-8 | tr a-f A-F)
|
|
case "$hash" in
|
|
[0-9ABC]*) hash="H$(printf '%s' "$hash" | cut -c1-7)" ;;
|
|
esac
|
|
|
|
page="LOG_${hash}"
|
|
contents='LOG_CONTENTS'
|
|
|
|
case "$field" in
|
|
hash) printf '%s\n' "$hash" ;;
|
|
page) printf '%s\n' "$page" ;;
|
|
contents) printf '%s\n' "$contents" ;;
|
|
all)
|
|
printf 'HASH=%s\n' "$hash"
|
|
printf 'PAGE=%s\n' "$page"
|
|
printf 'CONTENTS=%s\n' "$contents"
|
|
;;
|
|
*) usage ;;
|
|
esac
|