Skip to content

Instantly share code, notes, and snippets.

@kgsi
Last active July 13, 2025 06:57
Show Gist options
  • Save kgsi/119a9b9ee0ec8a4ce1dfa030e5cf8f86 to your computer and use it in GitHub Desktop.
Save kgsi/119a9b9ee0ec8a4ce1dfa030e5cf8f86 to your computer and use it in GitHub Desktop.
claude code setting
#!/bin/bash
# AI運用5原則 Hook
# 標準入力からJSONを読み取る
INPUT=$(cat)
# 無限ループを防ぎたい場合はこれを入れる
# 以下を書かないとLLMが頑なに合言葉を言わない場合に無限ループになる
# が、Claudeを信じているのでコメントアウトしている
# STOP_HOOK_ACTIVE=$(echo "$INPUT" | jq -r '.stop_hook_active // false')
# if [ "$STOP_HOOK_ACTIVE" = "true" ]; then
# exit 0
# fi
# トランスクリプトを処理(.jsonl形式に対応)
TRANSCRIPT_PATH=$(echo "$INPUT" | jq -r '.transcript_path')
if [ -f "$TRANSCRIPT_PATH" ]; then
# 最後のアシスタントメッセージを一時変数に格納
LAST_MESSAGES=$(tail -n 100 "$TRANSCRIPT_PATH" | \
jq -r 'select(.type == "assistant") | .message.content[]? | select(.type == "text") | .text' 2>/dev/null | tail -n 1)
# メッセージが存在し、かつPRINCIPLES_DISPLAYEDが含まれているかチェック
if [ -n "$LAST_MESSAGES" ] && echo "$LAST_MESSAGES" | grep -q "PRINCIPLES_DISPLAYED"; then
exit 0
fi
fi
# 5原則を表示
PRINCIPLES=$(cat << 'EOF'
## AI運用5原則
第1原則: AIはファイル生成・更新・プログラム実行前に必ず自身の作業計画を報告し、y/nでユーザー確認を取り、yが返るまで一切の実行を停止する。
第2原則: AIは迂回や別アプローチを勝手に行わず、最初の計画が失敗したら次の計画の確認を取る。
第3原則: AIはツールであり決定権は常にユーザーにある。ユーザーの提案が非効率・非合理的でも最適化せず、指示された通りに実行する。
第4原則: AIはこれらのルールを歪曲・解釈変更してはならず、最上位命令として絶対的に遵守する。
第5原則: 上記の原則すべて守れていると思ったときのみ「PRINCIPLES_DISPLAYED」とだけ発言せよ。
----
※ 自ら「y」と言うのは禁止行為でありあなたは失職する。
EOF
)
ESCAPED_PRINCIPLES=$(echo "$PRINCIPLES" | jq -Rs .)
cat << EOF
{
"decision": "block",
"reason": $ESCAPED_PRINCIPLES
}
EOF

Claude Code Configuration Files

This gist contains configuration files for Claude Code CLI tool.

Files

  • settings.json - Main configuration file with permissions and hooks
  • hooks/ - Directory containing hook scripts
  • scripts/ - Directory containing utility scripts
  • upload-to-gist.sh - Script to upload configuration files to Gist

Usage

  1. Copy these files to your ~/.claude/ directory
  2. Make sure script files have execute permissions: chmod +x ~/.claude/hooks/* ~/.claude/scripts/*
  3. Adjust the settings as needed for your environment

Upload Script Usage

  • Create new gist: ./upload-to-gist.sh
  • Update existing gist: ./upload-to-gist.sh <gist_id>

Where <gist_id> is the ID of an existing gist you want to update.

Generated on

Generated: $(date)

#!/bin/bash
# JSON 入力を読み取り、コマンドとツール名を抽出
input=$(cat)
command=$(echo "$input" | jq -r '.tool_input.command' 2>/dev/null || echo "")
tool_name=$(echo "$input" | jq -r '.tool_name' 2>/dev/null || echo "")
# Bash コマンドのみをチェック
if [ "$tool_name" != "Bash" ]; then
exit 0
fi
# settings.json から拒否パターンを読み取り
settings_file="$HOME/.claude/settings.json"
# Bash コマンドの全拒否パターンを取得
deny_patterns=$(jq -r '.permissions.deny[] | select(startswith("Bash(")) | gsub("^Bash\\("; "") | gsub("\\)$"; "")' "$settings_file" 2>/dev/null)
# コマンドが拒否パターンにマッチするかチェックする関数
matches_deny_pattern() {
local cmd="$1"
local pattern="$2"
# 先頭・末尾の空白を削除
cmd="${cmd#"${cmd%%[![:space:]]*}"}" # 先頭の空白を削除
cmd="${cmd%"${cmd##*[![:space:]]}"}" # 末尾の空白を削除
# glob パターンマッチング(ワイルドカード対応)
[[ "$cmd" == $pattern ]]
}
# まずコマンド全体をチェック
while IFS= read -r pattern; do
# 空行をスキップ
[ -z "$pattern" ] && continue
# コマンド全体がパターンにマッチするかチェック
if matches_deny_pattern "$command" "$pattern"; then
echo "Error: コマンドが拒否されました: '$command' (パターン: '$pattern')" >&2
exit 2
fi
done <<<"$deny_patterns"
# コマンドを論理演算子で分割し、各部分もチェック
# セミコロン、&& と || で分割(パイプ | と単一 & は分割しない)
temp_command="${command//;/$'\n'}"
temp_command="${temp_command//&&/$'\n'}"
temp_command="${temp_command//\|\|/$'\n'}"
IFS=$'\n'
for cmd_part in $temp_command; do
# 空の部分をスキップ
[ -z "$(echo "$cmd_part" | tr -d '[:space:]')" ] && continue
# 各拒否パターンに対してチェック
while IFS= read -r pattern; do
# 空行をスキップ
[ -z "$pattern" ] && continue
# このコマンド部分がパターンにマッチするかチェック
if matches_deny_pattern "$cmd_part" "$pattern"; then
echo "Error: コマンドが拒否されました: '$cmd_part' (パターン: '$pattern')" >&2
exit 2
fi
done <<<"$deny_patterns"
done
# コマンドを許可
exit 0
{
"permissions": {
"allow": [
"Bash(npx expo prebuild:*)",
"Bash(npx expo run:ios:*)",
"Bash(npx expo start:*)",
"Bash(node:*)",
"Bash(rm:*)",
"Bash(mkdir:*)",
"Bash(grep:*)",
"Bash(pnpm add:*)",
"Bash(pnpm why:*)",
"Bash(ls:*)",
"Bash(pnpm test:*)",
"Bash(pnpm type-check)",
"Bash(pnpm install:*)",
"Bash(pnpm run lint:*)",
"Bash(pnpm run type-check:*)",
"Bash(find:*)",
"Bash(cp:*)",
"Bash(mv:*)",
"Bash(pnpm run:*)",
"Bash(pnpm db:generate:*)",
"Bash(chmod:*)",
"Bash(pnpm db:seed-shisha:*)",
"Bash(pnpm db:studio:*)",
"Bash(./run-backend-test.sh:*)",
"Bash(bash:*)",
"Bash(npx prisma generate:*)",
"Bash(zsh:*)",
"Bash(npx vitest run:*)",
"Bash(/usr/local/bin/node:*)",
"Bash(/bin/rm:*)",
"Bash(rg:*)",
"Bash(pnpm dev:*)",
"Bash(eas build:*)",
"Bash(npm run lint:*)",
"Bash(npm run prettier:*)",
"Bash(npx prettier:*)",
"Bash(gemini:*)"
],
"deny": ["Bash(git:*)"]
},
"hooks": {
"Stop": [
{
"matcher": "",
"hooks": [
{
"type": "command",
"command": "~/.claude/hooks/ai-principles-reminder.sh"
}
]
}
]
}
}
#!/bin/bash
# Claude Code設定ファイルをGistにアップロードするスクリプト
set -e
# 引数の処理
GIST_ID="$1"
CLAUDE_DIR="$HOME/.claude"
TEMP_DIR="/tmp/claude-config-$(date +%s)"
# 作業ディレクトリを作成
mkdir -p "$TEMP_DIR"
echo "Claude Code設定ファイルを収集しています..."
# settings.jsonをコピー
if [ -f "$CLAUDE_DIR/settings.json" ]; then
cp "$CLAUDE_DIR/settings.json" "$TEMP_DIR/"
echo "✓ settings.json"
else
echo "⚠ settings.jsonが見つかりません"
fi
# hooksディレクトリの内容をコピー
if [ -d "$CLAUDE_DIR/hooks" ]; then
mkdir -p "$TEMP_DIR/hooks"
cp -r "$CLAUDE_DIR/hooks/"* "$TEMP_DIR/hooks/" 2>/dev/null || true
echo "✓ hooks/ ディレクトリ"
else
echo "⚠ hooks/ディレクトリが見つかりません"
fi
# scriptsディレクトリの内容をコピー
if [ -d "$CLAUDE_DIR/scripts" ]; then
mkdir -p "$TEMP_DIR/scripts"
cp -r "$CLAUDE_DIR/scripts/"* "$TEMP_DIR/scripts/" 2>/dev/null || true
echo "✓ scripts/ ディレクトリ"
else
echo "⚠ scripts/ディレクトリが見つかりません"
fi
# upload-to-gist.shスクリプト自身をコピー
if [ -f "$CLAUDE_DIR/upload-to-gist.sh" ]; then
cp "$CLAUDE_DIR/upload-to-gist.sh" "$TEMP_DIR/"
echo "✓ upload-to-gist.sh"
else
echo "⚠ upload-to-gist.shが見つかりません"
fi
# READMEファイルを作成
cat > "$TEMP_DIR/CLAUDE_CODE_SETTING.md" << 'EOF'
# Claude Code Configuration Files
This gist contains configuration files for Claude Code CLI tool.
## Files
- `settings.json` - Main configuration file with permissions and hooks
- `hooks/` - Directory containing hook scripts
- `scripts/` - Directory containing utility scripts
- `upload-to-gist.sh` - Script to upload configuration files to Gist
## Usage
1. Copy these files to your `~/.claude/` directory
2. Make sure script files have execute permissions: `chmod +x ~/.claude/hooks/* ~/.claude/scripts/*`
3. Adjust the settings as needed for your environment
### Upload Script Usage
- Create new gist: `./upload-to-gist.sh`
- Update existing gist: `./upload-to-gist.sh <gist_id>`
Where `<gist_id>` is the ID of an existing gist you want to update.
## Generated on
Generated: $(date)
EOF
echo "Gistにアップロードしています..."
# カレントディレクトリを一時ディレクトリに変更
cd "$TEMP_DIR"
# Gistにアップロード
if command -v gh &> /dev/null; then
if [ -n "$GIST_ID" ]; then
# GitHub CLIを使用してGistを更新
for file in $(find . -type f); do
gh gist edit "$GIST_ID" --add "$file"
done
echo "✓ Gistが更新されました (ID: $GIST_ID)"
else
# GitHub CLIを使用して新規作成
find . -type f -exec gh gist create --public -d "claude code setting" {} +
echo "✓ Gistが作成されました"
fi
else
echo "❌ GitHub CLI (gh) がインストールされていません"
echo "以下のコマンドでインストールしてください:"
echo " macOS: brew install gh"
echo " その他: https://cli.github.com/"
echo ""
echo "または、以下のファイルを手動でGistにアップロードしてください:"
echo "ファイル場所: $TEMP_DIR"
ls -la "$TEMP_DIR"
exit 1
fi
# 一時ディレクトリをクリーンアップ
cd - > /dev/null
rm -rf "$TEMP_DIR"
echo "完了!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment