Skip to content

Instantly share code, notes, and snippets.

@skanehira
Created March 12, 2026 07:52
Show Gist options
  • Select an option

  • Save skanehira/3dd67aa061be82daa0e980c6b3441137 to your computer and use it in GitHub Desktop.

Select an option

Save skanehira/3dd67aa061be82daa0e980c6b3441137 to your computer and use it in GitHub Desktop.
Rustワークスペースの依存関係に不審な時刻系クレートが含まれていないかチェックするスクリプト
#!/usr/bin/env bash
# check-suspicious-time-deps.sh
# Usage:
# curl -fsSL https://gist.github.com/.../raw | bash
# curl -fsSL https://gist.github.com/.../raw | bash -s -- /path/to/workspace
set -euo pipefail
# チェック対象クレート(ハイフン・アンダースコアは両方探す)
TARGETS=(
"chrono_anchor"
"dnp3times"
"time_calibrator"
"time_calibrators"
"time-sync"
)
# ワークスペースルートの解決(引数 or カレントディレクトリ)
WORKSPACE_ROOT="${1:-$(pwd)}"
# Cargo.lock の存在確認
CARGO_LOCK="$WORKSPACE_ROOT/Cargo.lock"
if [ ! -f "$CARGO_LOCK" ]; then
echo "Error: Cargo.lock が見つかりません: $CARGO_LOCK" >&2
echo "Usage: $0 [workspace_root]" >&2
exit 2
fi
echo "=== Suspicious Time Crate Dependency Checker ==="
echo "Workspace: $WORKSPACE_ROOT"
echo ""
# ────────────────────────────────────────────
# 1. Cargo.toml(直接依存)のチェック
# ────────────────────────────────────────────
echo "[ 直接依存 ] Cargo.toml ファイルを検索中..."
mapfile -t cargo_files < <(find "$WORKSPACE_ROOT" -name "Cargo.toml" 2>/dev/null)
echo " 対象ファイル数: ${#cargo_files[@]}"
echo ""
toml_found=false
for target in "${TARGETS[@]}"; do
normalized=$(echo "$target" | tr '-' '_')
hyphenated=$(echo "$target" | tr '_' '-')
pattern="(\"${target}\"|\"${normalized}\"|\"${hyphenated}\"|^(${target}|${normalized}|${hyphenated})[[:space:]]*=)"
for f in "${cargo_files[@]}"; do
matches=$(grep -nE "$pattern" "$f" 2>/dev/null || true)
if [ -n "$matches" ]; then
echo " [FOUND] $target → $f"
echo "$matches" | sed 's/^/ line /'
toml_found=true
fi
done
done
if ! $toml_found; then
echo " すべての Cargo.toml で対象クレートは未検出"
fi
echo ""
# ────────────────────────────────────────────
# 2. Cargo.lock(推移的依存含む)のチェック
# ────────────────────────────────────────────
echo "[ 推移的依存 ] Cargo.lock を検索中..."
lock_found=false
for target in "${TARGETS[@]}"; do
normalized=$(echo "$target" | tr '-' '_')
hyphenated=$(echo "$target" | tr '_' '-')
result=$(grep -E "^name = \"(${target}|${normalized}|${hyphenated})\"" "$CARGO_LOCK" 2>/dev/null || true)
if [ -n "$result" ]; then
version=$(echo "$result" | head -1 | sed 's/name = //')
echo " [FOUND] $target → $version"
lock_found=true
fi
done
if ! $lock_found; then
echo " Cargo.lock でも対象クレートは未検出"
fi
echo ""
# ────────────────────────────────────────────
# 結果サマリー
# ────────────────────────────────────────────
echo "========================================="
if $toml_found || $lock_found; then
echo "結果: ⚠️ 対象クレートが検出されました"
exit 1
else
echo "結果: ✅ 対象クレートは依存関係に含まれていません"
exit 0
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment