Download src/cli/policy-guard-cli.js from VanyaJ/bada-terminal: direct link, hf CLI and curl.
- Browser
- Download file 6.14 kB
-
https://huggingface.co/VanyaJ/bada-terminal/resolve/main/src/cli/policy-guard-cli.js
- Command line
-
hf download hf://VanyaJ/bada-terminal/src/cli/policy-guard-cli.js
-
curl -L -o policy-guard-cli.js https://huggingface.co/VanyaJ/bada-terminal/resolve/main/src/cli/policy-guard-cli.js
6.14 kB
| /** | |
| * policy-guard-cli.js โ BADA Terminal ์๊ฐ ์น์ Pre-Commit & ์ ์ฑ ๊ฐ๋ CLI | |
| * | |
| * ์ค๋ช ๋ฌธ์: /docs/specs/bada_trio_actionable_spec.md (SPEC-ACT-001 v1.0.0) | |
| * ๊ด๋ จ ๊ท์ฝ: AGENTS.md, Zero-Trust DLP, Pre-Commit Hook Security | |
| * | |
| * ์ฃผ์ ๊ธฐ๋ฅ: | |
| * 1. Git Staged Diff ๊ฒ์ฌ: ์ปค๋ฐ ์ staged ์ํ์ ์ฝ๋ diff๋ฅผ ValueAnchorEvaluator๋ก ์ ๋ฐ ๋ถ์ | |
| * 2. ์๊ฐ ์น์ ํผ๋๋ฐฑ ์ ๊ณต: ์๋ฐ ๋ฐ์ ์ ๋จ์ ์๋ฌ ์ข ๋ฃ๊ฐ ์๋ ๊ตฌ์ฒด์ ์ธ ๊ต์ ๊ฐ์ด๋ ์ถ๋ ฅ | |
| * 3. ์ํด๋ฆญ Git Hook ์ค์น/์ ๊ฑฐ: cross-platform ํธํ sh ๊ธฐ๋ฐ pre-commit ํ ์๋ ์์ฑ | |
| * 4. ํ๋ก๊ทธ๋๋ฐ ๋ฐฉ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋ฐ CLI ์์ฉ ์ง์ | |
| */ | |
| const { execSync } = require('child_process'); | |
| const fs = require('fs'); | |
| const path = require('path'); | |
| const valueAnchorEvaluator = require('../orchestration/value-anchor-evaluator'); | |
| /** | |
| * Git staged ์ํ์ ๋ณ๊ฒฝ์ฌํญ์ ์์งํ์ฌ ๊ฐ์น ์ต์ปค ์ ์ฑ ์ ํ๊ฐํฉ๋๋ค. | |
| * | |
| * @param {Object} [options] | |
| * @param {string} [options.cwd=process.cwd()] - ์์ ๋๋ ํ ๋ฆฌ | |
| * @returns {Object} ํ๊ฐ ๊ฒฐ๊ณผ ๊ฐ์ฒด { passed, hardBlocked, score, violations, feedbackPrompt } | |
| */ | |
| function evaluateStaged(options = {}) { | |
| const cwd = options.cwd || process.cwd(); | |
| // 1. Staged๋ ํ์ผ ๋ชฉ๋ก ์ถ์ถ (git diff --cached --name-only) | |
| let modifiedFiles = []; | |
| try { | |
| const filesOutput = execSync('git diff --cached --name-only', { | |
| cwd, | |
| encoding: 'utf8', | |
| timeout: 10000, | |
| maxBuffer: 10 * 1024 * 1024 | |
| }); | |
| modifiedFiles = filesOutput.split('\n').map(f => f.trim()).filter(Boolean); | |
| } catch (err) { | |
| // git ์ ์ฅ์๊ฐ ์๋๊ฑฐ๋ staged๋ ๋ด์ฉ์ด ์๋ ๊ฒฝ์ฐ | |
| return { | |
| passed: true, | |
| hardBlocked: false, | |
| score: 100, | |
| hardViolations: [], | |
| softViolations: [], | |
| feedbackPrompt: null, | |
| message: 'Git ์ ์ฅ์๊ฐ ์๋๊ฑฐ๋ staged ๋ณ๊ฒฝ์ฌํญ์ด ์์ต๋๋ค.' | |
| }; | |
| } | |
| if (modifiedFiles.length === 0) { | |
| return { | |
| passed: true, | |
| hardBlocked: false, | |
| score: 100, | |
| hardViolations: [], | |
| softViolations: [], | |
| feedbackPrompt: null, | |
| message: 'Staged๋ ํ์ผ์ด ์์ต๋๋ค.' | |
| }; | |
| } | |
| // 2. Staged๋ ์ฝ๋ ์ ์ฒด diff ์์ง | |
| let diff = ''; | |
| let diffWarning = null; | |
| try { | |
| diff = execSync('git diff --cached', { | |
| cwd, | |
| encoding: 'utf8', | |
| timeout: 15000, | |
| maxBuffer: 15 * 1024 * 1024 // 15MB ๋ฒํผ (๋ํ ๋ณ๊ฒฝ ๋๋น) | |
| }); | |
| } catch (err) { | |
| // diff ์์ง ์คํจ ์ ํ์ผ ๊ฒฝ๋ก ๊ธฐ๋ฐ ๊ฒ์ฌ๋ง ์ํ๋จ์ ๊ฒฝ๊ณ | |
| diffWarning = `[๊ฒฝ๊ณ ] git diff --cached ์์ง ์คํจ (${err.message?.slice(0, 80)}). ์ฝ๋ ๋ด์ฉ(์ํฌ๋ฆฟ ๋ฑ) ๊ฒ์ฌ๊ฐ ์๋ต๋ ์ ์์ต๋๋ค.`; | |
| diff = ''; | |
| } | |
| // 3. ValueAnchorEvaluator ์คํ | |
| const result = valueAnchorEvaluator.evaluate({ | |
| diff, | |
| modifiedFiles, | |
| commands: [] | |
| }); | |
| return { | |
| ...result, | |
| modifiedFiles, | |
| diffWarning | |
| }; | |
| } | |
| /** | |
| * .git/hooks/pre-commit ํ ์ ์ํด๋ฆญ์ผ๋ก ์ค์นํฉ๋๋ค. | |
| * ํฌ๋ก์ค ํ๋ซํผ(macOS, Linux, Windows Git Bash) ํธํ sh ์คํฌ๋ฆฝํธ ์์ฑ. | |
| * | |
| * @param {string} [targetGitDir] - .git ๋๋ ํ ๋ฆฌ ๊ฒฝ๋ก (์๋ต ์ ์๋ ๊ฐ์ง) | |
| * @returns {boolean} ์ฑ๊ณต ์ฌ๋ถ | |
| */ | |
| function installHook(targetGitDir = null) { | |
| const gitDir = targetGitDir || path.resolve(process.cwd(), '.git'); | |
| const hooksDir = path.join(gitDir, 'hooks'); | |
| if (!fs.existsSync(hooksDir)) { | |
| fs.mkdirSync(hooksDir, { recursive: true }); | |
| } | |
| const hookPath = path.join(hooksDir, 'pre-commit'); | |
| const scriptContent = `#!/bin/sh | |
| # BADA Terminal Value Anchor & Policy Guard Pre-Commit Hook | |
| # Auto-generated by BADA policy-guard-cli | |
| # ํ๋ก์ ํธ ๋ฃจํธ๋ก ์ด๋ (git hook์ GIT_DIR ๊ธฐ์ค์ผ๋ก ์คํ๋ ์ ์์) | |
| REPO_ROOT="$(git rev-parse --show-toplevel 2>/dev/null)" | |
| if [ -n "$REPO_ROOT" ]; then | |
| cd "$REPO_ROOT" || exit 1 | |
| fi | |
| node src/cli/policy-guard-cli.js --staged | |
| STATUS=$? | |
| if [ $STATUS -ne 0 ]; then | |
| echo "" | |
| echo "โ [BADA-GUARD] ์ ์ฑ ์๋ฐ์ผ๋ก ์ธํด ์ปค๋ฐ์ด ์ฐจ๋จ๋์์ต๋๋ค." | |
| echo "์ ๊ฐ์ด๋๋ฅผ ์ฐธ์กฐํ์ฌ ์ฝ๋๋ฅผ ์์ ํ ๋ค ๋ค์ ์ปค๋ฐํ์ญ์์ค." | |
| exit 1 | |
| fi | |
| exit 0 | |
| `; | |
| fs.writeFileSync(hookPath, scriptContent, { encoding: 'utf8', mode: 0o755 }); | |
| // POSIX ํ๊ฒฝ์์ ์คํ ๊ถํ ๋ถ์ฌ | |
| try { | |
| fs.chmodSync(hookPath, 0o755); | |
| } catch (e) { | |
| // Windows์์๋ ๋ฌด์ | |
| } | |
| return true; | |
| } | |
| /** | |
| * .git/hooks/pre-commit ํ ์ ์์ ํ๊ฒ ์ ๊ฑฐํฉ๋๋ค. | |
| * | |
| * @param {string} [targetGitDir] | |
| * @returns {boolean} | |
| */ | |
| function uninstallHook(targetGitDir = null) { | |
| const gitDir = targetGitDir || path.resolve(process.cwd(), '.git'); | |
| const hookPath = path.join(gitDir, 'hooks', 'pre-commit'); | |
| if (fs.existsSync(hookPath)) { | |
| fs.unlinkSync(hookPath); | |
| return true; | |
| } | |
| return false; | |
| } | |
| /** | |
| * CLI ๋ฉ์ธ ์ง์ ์ | |
| */ | |
| function cliMain(args = process.argv.slice(2)) { | |
| if (args.includes('--install-hook')) { | |
| const ok = installHook(); | |
| console.log(ok ? 'โ BADA Policy Guard Git Pre-Commit Hook ์ค์น ์๋ฃ!' : 'โ ์ค์น ์คํจ'); | |
| process.exit(ok ? 0 : 1); | |
| } | |
| if (args.includes('--uninstall-hook')) { | |
| const ok = uninstallHook(); | |
| console.log(ok ? 'โ BADA Policy Guard Git Pre-Commit Hook ์ ๊ฑฐ ์๋ฃ!' : 'โน๏ธ ์ค์น๋ ํ ์ด ์์ต๋๋ค.'); | |
| process.exit(0); | |
| } | |
| if (args.includes('--staged') || args.length === 0) { | |
| console.log('๐ก๏ธ [BADA Policy Guard] Git Staged ์ฝ๋ ์ ์ฑ ๊ฒ์ฌ ์ค...'); | |
| const result = evaluateStaged(); | |
| if (result.passed) { | |
| console.log(`โ [BADA-GUARD PASS] ๋ชจ๋ ๋ณด์ ๋ฐ ๊ฐ๋ฐ ์ ์ฑ ํต๊ณผ! (์ ์: ${result.score}/100)`); | |
| process.exit(0); | |
| } else { | |
| console.error(`\n๐จ [BADA-GUARD BLOCKED] ์น๋ช ์ ์ ์ฑ ์๋ฐ์ด ๊ฐ์ง๋์์ต๋๋ค! (์ ์: ${result.score}/100)`); | |
| if (result.feedbackPrompt) { | |
| console.error('\n' + result.feedbackPrompt + '\n'); | |
| } | |
| process.exit(1); | |
| } | |
| } | |
| } | |
| // CLI ์ง์ ์คํ ์ ์คํ | |
| if (require.main === module) { | |
| cliMain(); | |
| } | |
| module.exports = { | |
| evaluateStaged, | |
| installHook, | |
| uninstallHook, | |
| cliMain | |
| }; | |