Skip to content

Commit b74d925

Browse files
committed
refactor: cli common for share method between both hooks
1 parent 4fdfa99 commit b74d925

3 files changed

Lines changed: 84 additions & 58 deletions

File tree

hooks/scripts/block-cli-workarounds.sh

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,48 +8,57 @@ if ! command -v jq &>/dev/null; then
88
exit 1
99
fi
1010

11+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
12+
source "$SCRIPT_DIR/vgv-cli-common.sh"
13+
1114
INPUT=$(cat)
1215
COMMAND=$(echo "$INPUT" | jq -r '.tool_input.command // empty')
1316

1417
if [ -z "$COMMAND" ]; then
1518
exit 0
1619
fi
1720

18-
deny() {
19-
jq -n \
20-
--arg reason "$1" \
21-
'{
22-
hookSpecificOutput: {
23-
hookEventName: "PreToolUse",
24-
permissionDecision: "deny",
25-
permissionDecisionReason: $reason
26-
}
27-
}'
28-
exit 0
21+
# Deny with an install/upgrade message when the CLI is missing or outdated,
22+
# otherwise redirect to the MCP tool.
23+
deny_with_cli_check() {
24+
local mcp_hint="$1"
25+
local cli_status
26+
cli_status=$(check_vgv_cli)
27+
case "$cli_status" in
28+
not_installed)
29+
deny "VeryGoodCLI is required but was not found. Install with: dart pub global activate very_good_cli"
30+
;;
31+
outdated:*)
32+
local version="${cli_status#outdated:}"
33+
deny "VeryGoodCLI ${version} is too old (requires >= ${MIN_VERSION}). Update with: dart pub global activate very_good_cli"
34+
;;
35+
*)
36+
deny "$mcp_hint"
37+
;;
38+
esac
2939
}
3040

3141
# Block project creation via CLI
3242
if echo "$COMMAND" | grep -qE '(flutter|dart)\s+create'; then
33-
deny "Do not use 'flutter create' or 'dart create'. Use the very_good_cli MCP 'create' tool instead. If VeryGoodCLI MCP is not available, install with: dart pub global activate very_good_cli"
43+
deny_with_cli_check "Do not use 'flutter create' or 'dart create'. Use the very_good_cli MCP 'create' tool instead."
3444
fi
3545

3646
if echo "$COMMAND" | grep -qE 'very_good\s+create'; then
37-
deny "Do not use 'very_good create' via shell. Use the very_good_cli MCP 'create' tool instead. If VeryGoodCLI MCP is not available, upgrade with: dart pub global activate very_good_cli"
47+
deny_with_cli_check "Do not use 'very_good create' via shell. Use the very_good_cli MCP 'create' tool instead."
3848
fi
3949

4050
# Block test runs via CLI
4151
if echo "$COMMAND" | grep -qE '(flutter|dart)\s+test'; then
42-
deny "Do not use 'flutter test' or 'dart test'. Use MCP test tools instead (very_good_cli MCP 'test' or dart MCP 'Run tests')."
52+
deny_with_cli_check "Do not use 'flutter test' or 'dart test'. Use the very_good_cli MCP 'test' tool instead."
4353
fi
4454

45-
# Uncomment this once `very_good_cli` version check is updated to 1.1.0
46-
#if echo "$COMMAND" | grep -qE 'very_good\s+test'; then
47-
# deny "Do not use 'very_good test' via shell. Use the very_good_cli MCP 'test' tool instead."
48-
#fi
55+
if echo "$COMMAND" | grep -qE 'very_good\s+test'; then
56+
deny_with_cli_check "Do not use 'very_good test' via shell. Use the very_good_cli MCP 'test' tool instead."
57+
fi
4958

5059
# Block license check via CLI
5160
if echo "$COMMAND" | grep -qE 'very_good\s+packages'; then
52-
deny "Do not use 'very_good packages' via shell. Use the very_good_cli MCP 'packages_get' or 'packages_check_licenses' tool instead."
61+
deny_with_cli_check "Do not use 'very_good packages' via shell. Use the very_good_cli MCP 'packages_get' or 'packages_check_licenses' tool instead."
5362
fi
5463

5564
# Not a blocked command — allow

hooks/scripts/check-vgv-cli.sh

Lines changed: 13 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,45 +7,19 @@ if ! command -v jq &>/dev/null; then
77
exit 1
88
fi
99

10-
MIN_VERSION="1.1.0"
11-
MIN_MAJOR=1
12-
MIN_MINOR=1
13-
MIN_PATCH=0
14-
15-
deny() {
16-
jq -n \
17-
--arg reason "$1" \
18-
'{
19-
hookSpecificOutput: {
20-
hookEventName: "PreToolUse",
21-
permissionDecision: "deny",
22-
permissionDecisionReason: $reason
23-
}
24-
}'
25-
exit 0
26-
}
27-
28-
# CLI not installed
29-
if ! command -v very_good &>/dev/null; then
30-
deny "VeryGoodCLI is not installed. This tool requires VeryGoodCLI >= ${MIN_VERSION}. Install with: dart pub global activate very_good_cli"
31-
fi
32-
33-
# Parse version (first semver from first line)
34-
RAW=$(very_good --version 2>/dev/null)
35-
VERSION=$(echo "$RAW" | head -1 | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -1)
36-
37-
if [ -z "$VERSION" ]; then
38-
deny "Could not determine VeryGoodCLI version. This tool requires VeryGoodCLI >= ${MIN_VERSION}. Check with: very_good --version"
39-
fi
40-
41-
IFS='.' read -r MAJOR MINOR PATCH <<< "$VERSION"
42-
43-
# Version too old
44-
if [ "$MAJOR" -lt "$MIN_MAJOR" ] 2>/dev/null ||
45-
{ [ "$MAJOR" -eq "$MIN_MAJOR" ] && [ "$MINOR" -lt "$MIN_MINOR" ]; } 2>/dev/null ||
46-
{ [ "$MAJOR" -eq "$MIN_MAJOR" ] && [ "$MINOR" -eq "$MIN_MINOR" ] && [ "$PATCH" -lt "$MIN_PATCH" ]; } 2>/dev/null; then
47-
deny "VeryGoodCLI ${VERSION} is too old. This tool requires VeryGoodCLI >= ${MIN_VERSION}. Update with: dart pub global activate very_good_cli"
48-
fi
10+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
11+
source "$SCRIPT_DIR/vgv-cli-common.sh"
12+
13+
cli_status=$(check_vgv_cli)
14+
case "$cli_status" in
15+
not_installed)
16+
deny "VeryGoodCLI is not installed. This tool requires VeryGoodCLI >= ${MIN_VERSION}. Install with: dart pub global activate very_good_cli"
17+
;;
18+
outdated:*)
19+
version="${cli_status#outdated:}"
20+
deny "VeryGoodCLI ${version} is too old. This tool requires VeryGoodCLI >= ${MIN_VERSION}. Update with: dart pub global activate very_good_cli"
21+
;;
22+
esac
4923

5024
# Version OK
5125
exit 0

hooks/scripts/vgv-cli-common.sh

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/bin/bash
2+
# Shared helpers for VeryGoodCLI version checks and hook deny responses.
3+
4+
MIN_VERSION="1.1.0"
5+
MIN_MAJOR=1
6+
MIN_MINOR=1
7+
MIN_PATCH=0
8+
9+
deny() {
10+
jq -n \
11+
--arg reason "$1" \
12+
'{
13+
hookSpecificOutput: {
14+
hookEventName: "PreToolUse",
15+
permissionDecision: "deny",
16+
permissionDecisionReason: $reason
17+
}
18+
}'
19+
exit 0
20+
}
21+
22+
# Check VeryGoodCLI availability and version.
23+
# Returns: "ok", "not_installed", or "outdated:<version>"
24+
check_vgv_cli() {
25+
if ! command -v very_good &>/dev/null; then
26+
echo "not_installed"
27+
return
28+
fi
29+
RAW=$(very_good --version 2>/dev/null)
30+
VERSION=$(echo "$RAW" | head -1 | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -1)
31+
if [ -z "$VERSION" ]; then
32+
echo "not_installed"
33+
return
34+
fi
35+
IFS='.' read -r MAJOR MINOR PATCH <<< "$VERSION"
36+
if [ "$MAJOR" -lt "$MIN_MAJOR" ] 2>/dev/null ||
37+
{ [ "$MAJOR" -eq "$MIN_MAJOR" ] && [ "$MINOR" -lt "$MIN_MINOR" ]; } 2>/dev/null ||
38+
{ [ "$MAJOR" -eq "$MIN_MAJOR" ] && [ "$MINOR" -eq "$MIN_MINOR" ] && [ "$PATCH" -lt "$MIN_PATCH" ]; } 2>/dev/null; then
39+
echo "outdated:$VERSION"
40+
return
41+
fi
42+
echo "ok"
43+
}

0 commit comments

Comments
 (0)