This repository was archived by the owner on Nov 6, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotification-handler.sh
More file actions
executable file
·132 lines (112 loc) · 3.6 KB
/
notification-handler.sh
File metadata and controls
executable file
·132 lines (112 loc) · 3.6 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
125
126
127
128
129
130
131
132
#!/bin/bash
# Claude Code 通知ハンドラー
#
# 使用方法:
# 1. notifiersディレクトリに通知スクリプトを配置
# 2. スクリプトに実行権限を付与: chmod +x notifiers/*.sh
# 3. Claude Code settings.jsonで設定:
# echo '{"message":"test"}' | ./notification-handler.sh
#
# ログ出力オプション:
# --log /path/to/logfile : ログファイルを指定
# -l /path/to/logfile : ログファイルを指定(短縮形)
# --log=/path/to/logfile : ログファイルを指定(=形式)
set -euo pipefail
# 設定
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
NOTIFIERS_DIR="${SCRIPT_DIR}/notifiers"
LOG_FILE=""
# 引数パース
while [[ $# -gt 0 ]]; do
case $1 in
--log|-l)
LOG_FILE="$2"
shift 2
;;
--log=*)
LOG_FILE="${1#*=}"
shift
;;
*)
shift
;;
esac
done
log_message() {
local message="$1"
local timestamp
timestamp="[$(date '+%Y-%m-%d %H:%M:%S')]"
# 引数で指定されたログファイルに出力
if [[ -n "$LOG_FILE" ]]; then
# ディレクトリが存在しない場合は作成
local log_dir
log_dir=$(dirname "$LOG_FILE")
[[ ! -d "$log_dir" ]] && mkdir -p "$log_dir"
echo "$timestamp $message" >> "$LOG_FILE"
fi
# デバッグモードの場合は標準エラー出力にも出力
if [[ "${DEBUG_MODE:-false}" == "true" ]]; then
echo "$timestamp $message" >&2
fi
}
# ハンドラー開始ログ
log_message "notification-handler: 通知ハンドラー開始"
# jqの必須チェック
if ! command -v jq &> /dev/null; then
echo "❌ エラー: jqがインストールされていません" >&2
echo " インストール方法: brew install jq または apt-get install jq" >&2
exit 1
fi
# JSON入力を受信
input=$(cat)
# 受信データログ
log_message "受信データ: $input"
# テストモードの確認
if [[ "${CLAUDE_TEST_MODE:-}" == "true" ]] || [[ "${TEST_MODE:-}" == "true" ]]; then
echo "🧪 テストモード: 通知スキップ" >&2
log_message "通知完了: 0 個実行, 0 個失敗 (テストモード)"
exit 0
fi
# notifiersディレクトリの存在確認
if [[ ! -d "$NOTIFIERS_DIR" ]]; then
log_message "エラー: notifiersディレクトリが存在しません"
exit 0
fi
# 実行可能な通知スクリプトを処理
any_executed=false
any_succeeded=false
for notifier in "$NOTIFIERS_DIR"/*.sh; do
# ファイルが存在しない場合はスキップ
[[ ! -f "$notifier" ]] && continue
# 実行権限がない場合はスキップ
if [[ ! -x "$notifier" ]]; then
log_message "スキップ: $(basename "$notifier") (実行権限なし)"
continue
fi
notifier_name=$(basename "$notifier")
any_executed=true
# 各通知スクリプトにJSONを渡して実行
# || true を使ってエラーでも継続
if echo "$input" | "$notifier" 2>&1; then
log_message "成功: $notifier_name"
any_succeeded=true
else
log_message "失敗: $notifier_name (exit: $?)"
fi
done
# 結果をログ
if [[ "$any_executed" == "true" ]]; then
if [[ "$any_succeeded" == "true" ]]; then
log_message "通知完了: 成功"
else
log_message "通知完了: 全て失敗"
fi
else
log_message "通知完了: 実行可能な通知スクリプトなし"
fi
# 少なくとも1つの通知が成功していれば正常終了
if [[ "$any_succeeded" == "true" ]]; then
exit 0
else
exit 1
fi