Last active
August 14, 2025 04:06
-
-
Save cnosuke/84f5d3783dfbd846511be69e4365a22f to your computer and use it in GitHub Desktop.
Claude Hooks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env ruby | |
| require 'json' | |
| begin | |
| # --- 1. カレントディレクトリに .sqlfluff がなければ即終了 --- | |
| unless File.exist?('.sqlfluff') | |
| # 設定ファイルがないので、このフックは何もしない | |
| exit 0 | |
| end | |
| # --- 2. HookからJSON入力を読み込んでパース --- | |
| begin | |
| input_data = JSON.parse($stdin.read) | |
| rescue JSON::ParserError | |
| exit 0 | |
| end | |
| tool_input = input_data['tool_input'] | |
| exit 0 unless tool_input.is_a?(Hash) | |
| # --- 3. 変更されたファイルのパスを全て抽出 --- | |
| all_paths = [] | |
| all_paths << tool_input['file_path'] if tool_input['file_path'] | |
| all_paths.concat(tool_input['files']) if tool_input['files'].is_a?(Array) | |
| # --- 4. .sqlファイルのみをフィルタリング --- | |
| sql_files = all_paths.compact.map do |path| | |
| File.expand_path(path) | |
| end.select do |path| | |
| path.end_with?('.sql') && File.exist?(path) | |
| end | |
| # 対象の.sqlファイルがなければ、何もせずに正常終了 | |
| if sql_files.empty? | |
| exit 0 | |
| end | |
| # --- 5. sqlfluff fixコマンドを実行 --- | |
| puts "Found .sqlfluff, running sqlfluff fix on:" | |
| puts sql_files.join("\n") | |
| system("sqlfluff", "fix", *sql_files) | |
| puts "SQL formatting complete." | |
| rescue => e | |
| # Print errot to stderr and exit 2 | |
| STDERR.puts "Error: #{e.message}" | |
| exit 1 | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env ruby | |
| require 'json' | |
| begin | |
| # --- 1. HookからJSON入力を読み込んでパース --- | |
| begin | |
| input_data = JSON.parse($stdin.read) | |
| rescue JSON::ParserError | |
| # 不正なJSONの場合は何もせず終了 | |
| exit 0 | |
| end | |
| # tool_inputがなければ処理対象外 | |
| tool_input = input_data['tool_input'] | |
| exit 0 unless tool_input.is_a?(Hash) | |
| # --- 2. 変更されたファイルのパスを全て抽出 --- | |
| all_paths = [] | |
| # 単一ファイルのパス(Write, Editツールなど) | |
| all_paths << tool_input['file_path'] if tool_input['file_path'] | |
| # 複数ファイルのパス(MultiEditツールなど) | |
| all_paths.concat(tool_input['files']) if tool_input['files'].is_a?(Array) | |
| # --- 3. .goファイルのみをフィルタリング --- | |
| # nilを除外し、パスをフルパスに変換し、末尾が「.go」のファイルのみを選択 | |
| go_files = all_paths.compact.map do |path| | |
| File.expand_path(path) | |
| end.select do |path| | |
| path.end_with?('.go') && File.exist?(path) | |
| end | |
| # 対象の.goファイルがなければ、何もせずに正常終了 | |
| if go_files.empty? | |
| exit 0 | |
| end | |
| # --- 4. gofmtコマンドを実行 --- | |
| puts "Running gofmt on:" | |
| puts go_files.join("\n") | |
| # systemに配列でコマンドを渡すことで、シェルインジェクションを防止 | |
| system("gofmt", "-w", *go_files) | |
| puts "Formatting complete." | |
| rescue => e | |
| # Print errot to stderr and exit 2 | |
| STDERR.puts "Error: #{e.message}" | |
| exit 1 | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env ruby | |
| require 'json' | |
| begin | |
| # --- 1. カレントディレクトリに .sqlfluff がなければ即終了 --- | |
| unless File.exist?('.sqlfluff') | |
| # 設定ファイルがないので、このフックは何もしない | |
| exit 0 | |
| end | |
| # --- 2. HookからJSON入力を読み込んでパース --- | |
| begin | |
| input_data = JSON.parse($stdin.read) | |
| rescue JSON::ParserError | |
| exit 0 | |
| end | |
| tool_input = input_data['tool_input'] | |
| exit 0 unless tool_input.is_a?(Hash) | |
| # --- 3. 変更されたファイルのパスを全て抽出 --- | |
| all_paths = [] | |
| all_paths << tool_input['file_path'] if tool_input['file_path'] | |
| all_paths.concat(tool_input['files']) if tool_input['files'].is_a?(Array) | |
| # --- 4. .sqlファイルのみをフィルタリング --- | |
| sql_files = all_paths.compact.map do |path| | |
| File.expand_path(path) | |
| end.select do |path| | |
| path.end_with?('.sql') && File.exist?(path) | |
| end | |
| # 対象の.sqlファイルがなければ、何もせずに正常終了 | |
| if sql_files.empty? | |
| exit 0 | |
| end | |
| # --- 5. sqlfluff fixコマンドを実行 --- | |
| puts "Found .sqlfluff, running sqlfluff fix on:" | |
| puts sql_files.join("\n") | |
| system("sqlfluff", "fix", *sql_files) | |
| puts "SQL formatting complete." | |
| rescue => e | |
| # Print errot to stderr and exit 2 | |
| STDERR.puts "Error: #{e.message}" | |
| exit 1 | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment