-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgate-cli.ts
More file actions
147 lines (137 loc) · 3.95 KB
/
Copy pathgate-cli.ts
File metadata and controls
147 lines (137 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env tsx
/**
* gate-cli.ts — tiny CLI wrapper around the deploy-gate decision logic
* (src/deploy/gate.ts). scripts/deploy-gate.sh calls this to decide approval,
* keeping the pure decision logic in TypeScript (unit-tested) and the bash
* wrapper thin (UI + path safety + invoking the deploy script).
*
* Usage:
* node --import tsx src/deploy/gate-cli.ts \
* --repo <path> [--batch <name>] [--notes <file>] [--state <file>] \
* [--token-file <path>] [--approval-file <path>] \
* [--interactive] [--interactive-input <text>]
*
* Environment:
* DEPLOY_APPROVAL_TOKEN the token to match against the token file
* DEPLOY_APPROVAL_FILE fallback for --approval-file
*
* Output (parsed by the bash wrapper):
* DEPLOY_SUMMARY_BEGIN
* <multi-line deployment summary>
* DEPLOY_SUMMARY_END
* DEPLOY_APPROVED=yes|no
* DEPLOY_REASON=<reason>
*
* Exit code: 0 when approved, 3 when denied (the gate's "hard stop" code).
*/
import * as path from "node:path"
import { buildDeploySummary, evaluateApproval } from "./gate.js"
interface CliArgs {
repo: string
batch?: string
notes?: string
state?: string
tokenFile?: string
approvalFile?: string
interactive: boolean
interactiveInput?: string
}
function parseArgs(argv: string[]): CliArgs {
const args: CliArgs = { repo: "", interactive: false }
for (let i = 0; i < argv.length; i++) {
const arg = argv[i]
const next = (): string | undefined => {
const v = argv[i + 1]
if (v === undefined || v.startsWith("--")) {
return undefined
}
i++
return v
}
switch (arg) {
case "--repo":
case "--batch":
case "--notes":
case "--state":
case "--token-file":
case "--approval-file":
case "--interactive-input": {
const v = next()
if (v === undefined) {
throw new Error(`Missing value for ${arg}`)
}
switch (arg) {
case "--repo":
args.repo = v
break
case "--batch":
args.batch = v
break
case "--notes":
args.notes = v
break
case "--state":
args.state = v
break
case "--token-file":
args.tokenFile = v
break
case "--approval-file":
args.approvalFile = v
break
case "--interactive-input":
args.interactiveInput = v
break
}
break
}
case "--interactive":
args.interactive = true
break
case "--help":
case "-h":
console.error(
"gate-cli.ts --repo <path> [--batch <name>] [--notes <file>] [--state <file>] [--token-file <path>] [--approval-file <path>] [--interactive] [--interactive-input <text>]",
)
process.exit(0)
break
default:
throw new Error(`Unknown argument: ${arg}`)
}
}
return args
}
function main(): void {
const args = parseArgs(process.argv.slice(2))
if (!args.repo) {
throw new Error("--repo <path> is required")
}
// Default artifact paths inside the target repo (mirror scripts/deploy-gate.sh).
const tokenFile = args.tokenFile ?? path.join(args.repo, ".deploy-approval")
const batch = args.batch ?? "round"
const approvalFile =
args.approvalFile ??
process.env.DEPLOY_APPROVAL_FILE ??
path.join(args.repo, ".worktrees", `.deploy-approved-${batch}`)
const statePath = args.state ?? path.join(args.repo, ".worktrees", ".orchestrator-state.json")
// 1. Deployment summary (what's being deployed).
const summary = buildDeploySummary({
repo: args.repo,
batch,
notesPath: args.notes,
statePath,
})
process.stdout.write(`DEPLOY_SUMMARY_BEGIN\n${summary}\nDEPLOY_SUMMARY_END\n`)
// 2. Approval decision (pure logic; file reads happen in evaluateApproval).
const result = evaluateApproval({
interactive: args.interactive,
interactiveInput: args.interactiveInput,
approvalToken: process.env.DEPLOY_APPROVAL_TOKEN,
tokenFile,
approvalFile,
})
process.stdout.write(`DEPLOY_APPROVED=${result.approved ? "yes" : "no"}\n`)
process.stdout.write(`DEPLOY_REASON=${result.reason.replace(/\n/g, " ")}\n`)
process.exitCode = result.approved ? 0 : 3
}
main()