Files
buzz/scripts/grab-emoji.sh
T
cls 4a79c0072c
Docker image / Build (linux/amd64) (push) Has been cancelled
Docker image / Build (linux/arm64) (push) Has been cancelled
Docker image / Merge release multi-arch manifest (push) Has been cancelled
Docker image / Merge debug multi-arch manifest (push) Has been cancelled
Docker image / Build public push gateway (linux/amd64) (push) Has been cancelled
Docker image / Build public push gateway (linux/arm64) (push) Has been cancelled
Docker image / Publish public push gateway image (push) Has been cancelled
Harbor Buzz Orchestra / Python tests and lint (push) Has been cancelled
CI / Detect Changed Paths (push) Has been cancelled
CI / Rust Lint (push) Has been cancelled
CI / Unit Tests (push) Has been cancelled
CI / Desktop Core (push) Has been cancelled
CI / Desktop Smoke E2E (1) (push) Has been cancelled
CI / Desktop Smoke E2E (2) (push) Has been cancelled
CI / Desktop Smoke E2E (3) (push) Has been cancelled
CI / Desktop Smoke E2E (4) (push) Has been cancelled
CI / Desktop (push) Has been cancelled
CI / Desktop E2E Relay (push) Has been cancelled
CI / Desktop E2E Integration (1/2) (push) Has been cancelled
CI / Desktop E2E Integration (2/2) (push) Has been cancelled
CI / Desktop E2E Integration (push) Has been cancelled
CI / Backend Integration (relay e2e) (push) Has been cancelled
CI / Relay E2E (push) Has been cancelled
CI / Web (push) Has been cancelled
CI / Mobile (push) Has been cancelled
CI / Security (push) Has been cancelled
CI / Dead Token Reference Guard (push) Has been cancelled
CI / Server Cross-Compile (aarch64-unknown-linux-musl) (push) Has been cancelled
CI / Server Cross-Compile (x86_64-unknown-linux-musl) (push) Has been cancelled
CI / Windows Rust (x86_64-pc-windows-msvc) (push) Has been cancelled
CI / Desktop Build (macOS) (push) Has been cancelled
helm chart / lint + unittest + render matrix (push) Has been cancelled
helm chart / install on kind (gated) (push) Has been cancelled
helm chart / publish chart to GHCR (push) Has been cancelled
Mesh Lifecycle / Relay-Driven Mesh Lifecycle Smoke (push) Has been cancelled
Sprig image / Build (linux/amd64) (push) Has been cancelled
Sprig image / Build (linux/arm64) (push) Has been cancelled
Sprig image / Merge multi-arch manifest (push) Has been cancelled
Sprig / Build (aarch64-unknown-linux-musl) (push) Has been cancelled
Sprig / Build (x86_64-unknown-linux-musl) (push) Has been cancelled
Sprig / Publish rolling release (push) Has been cancelled
Sprig / Publish tagged release (push) Has been cancelled
Import exact Chinese Buzz source tree
Signed-off-by: cls_???? <908705107@qq.com>
2026-08-13 18:38:15 +08:00

167 lines
4.7 KiB
Bash
Executable File

#!/usr/bin/env bash
# grab-emoji.sh — Register custom Slack emoji in Buzz
#
# Looks up each emoji name in your Slack workspace and registers it in Buzz
# via `buzz emoji set`, making it available as :name: in the Buzz emoji picker.
#
# Usage:
# SLACK_TOKEN=xoxp-... ./scripts/grab-emoji.sh [--name <buzz-name>] <emoji-name> [emoji-name ...]
#
# Options:
# --name <buzz-name> Override the shortcode used in Buzz (only valid with a single emoji)
#
# Env:
# SLACK_TOKEN — Slack user token (xoxp-...) with emoji:read scope
#
# Output:
# name → registered as :name: in Buzz on success
# name → ERROR: reason on failure (script continues to next emoji)
set -euo pipefail
CACHE_FILE="${HOME}/.cache/slack-emoji-list.json"
CACHE_TTL=86400 # 24 hours in seconds
# ── Argument parsing ──────────────────────────────────────────────────────────
BUZZ_NAME=""
while [[ $# -gt 0 ]]; do
case "$1" in
--name)
BUZZ_NAME="$2"
shift 2
;;
--)
shift
break
;;
-*)
echo "ERROR: Unknown option: $1" >&2
exit 1
;;
*)
break
;;
esac
done
# ── Preflight checks ──────────────────────────────────────────────────────────
if [[ $# -eq 0 ]]; then
echo "Usage: SLACK_TOKEN=xoxp-... $0 [--name <buzz-name>] <emoji-name> [emoji-name ...]" >&2
exit 1
fi
if [[ -n "$BUZZ_NAME" && $# -ne 1 ]]; then
echo "ERROR: --name can only be used when specifying a single emoji" >&2
exit 1
fi
if [[ -z "${SLACK_TOKEN:-}" ]]; then
echo "ERROR: SLACK_TOKEN is not set. Export your xoxp- Slack token." >&2
exit 1
fi
if ! command -v buzz &>/dev/null; then
echo "ERROR: 'buzz' not found in PATH. Install the Buzz CLI and retry." >&2
exit 1
fi
if ! command -v curl &>/dev/null; then
echo "ERROR: 'curl' not found in PATH." >&2
exit 1
fi
if ! command -v jq &>/dev/null; then
echo "ERROR: 'jq' not found in PATH." >&2
exit 1
fi
# ── Cache management ──────────────────────────────────────────────────────────
_cache_is_fresh() {
[[ -f "$CACHE_FILE" ]] || return 1
local mtime now age
# macOS stat uses -f %m; GNU stat uses -c %Y
if stat --version &>/dev/null 2>&1; then
mtime=$(stat -c %Y "$CACHE_FILE")
else
mtime=$(stat -f %m "$CACHE_FILE")
fi
now=$(date +%s)
age=$(( now - mtime ))
(( age < CACHE_TTL ))
}
_refresh_cache() {
mkdir -p "$(dirname "$CACHE_FILE")"
local response
response=$(curl -sf \
-H "Authorization: Bearer ${SLACK_TOKEN}" \
"https://slack.com/api/emoji.list") || {
echo "ERROR: network failure fetching emoji list from Slack" >&2
return 1
}
local ok
ok=$(echo "$response" | jq -r '.ok')
if [[ "$ok" != "true" ]]; then
local err
err=$(echo "$response" | jq -r '.error // "unknown error"')
echo "ERROR: Slack API returned error: $err" >&2
return 1
fi
echo "$response" > "$CACHE_FILE"
}
if ! _cache_is_fresh; then
_refresh_cache || exit 1
fi
# ── Emoji resolution ──────────────────────────────────────────────────────────
# Returns the URL for an emoji name, resolving one level of aliasing.
# Prints the URL to stdout; returns 1 if not found.
_resolve_url() {
local name="$1"
local value
value=$(jq -r --arg n "$name" '.emoji[$n] // empty' "$CACHE_FILE")
if [[ -z "$value" ]]; then
return 1
fi
if [[ "$value" == alias:* ]]; then
local target="${value#alias:}"
value=$(jq -r --arg n "$target" '.emoji[$n] // empty' "$CACHE_FILE")
if [[ -z "$value" ]]; then
return 1
fi
fi
echo "$value"
}
# ── Per-emoji processing ──────────────────────────────────────────────────────
for emoji_name in "$@"; do
# Use --name override if provided, otherwise use the Slack emoji name
buzz_shortcode="${BUZZ_NAME:-$emoji_name}"
# Resolve URL
emoji_url=$(_resolve_url "$emoji_name") || {
echo "${emoji_name} → ERROR: emoji not found in workspace"
continue
}
# Register in Buzz
set_output=$(buzz emoji set --shortcode "$buzz_shortcode" --url "$emoji_url" 2>&1) || {
echo "${emoji_name} → ERROR: buzz emoji set failed — ${set_output}"
continue
}
echo "${emoji_name} → registered as :${buzz_shortcode}: in Buzz"
done