|
| 1 | +import { CLAIM_STATUSES, openClaimLedger } from "./claim-ledger.js"; |
| 2 | + |
| 3 | +const CLAIM_CLAIM_USAGE = |
| 4 | + "Usage: gittensory-miner claim claim <owner/repo> <issue#> [--note <text>] [--json]"; |
| 5 | +const CLAIM_RELEASE_USAGE = "Usage: gittensory-miner claim release <owner/repo> <issue#> [--json]"; |
| 6 | +const CLAIM_LIST_USAGE = |
| 7 | + "Usage: gittensory-miner claim list [--repo <owner/repo>] [--status active|released|expired] [--json]"; |
| 8 | + |
| 9 | +function parseRepoArg(value, usage) { |
| 10 | + if (!value) return { error: usage }; |
| 11 | + const trimmed = value.trim(); |
| 12 | + const [owner, repo, extra] = trimmed.split("/"); |
| 13 | + if (!owner || !repo || extra !== undefined) { |
| 14 | + return { error: "Repository must be in owner/repo form." }; |
| 15 | + } |
| 16 | + return { repoFullName: `${owner}/${repo}` }; |
| 17 | +} |
| 18 | + |
| 19 | +function parseIssueNumberArg(value, usage) { |
| 20 | + if (!value) return { error: usage }; |
| 21 | + const parsed = Number(value); |
| 22 | + if (!Number.isInteger(parsed) || parsed < 1) { |
| 23 | + return { error: "issue number must be a positive integer." }; |
| 24 | + } |
| 25 | + return { issueNumber: parsed }; |
| 26 | +} |
| 27 | + |
| 28 | +export function parseClaimClaimArgs(args) { |
| 29 | + const options = { json: false, note: undefined }; |
| 30 | + const positional = []; |
| 31 | + |
| 32 | + for (let index = 0; index < args.length; index += 1) { |
| 33 | + const token = args[index]; |
| 34 | + if (token === "--json") { |
| 35 | + options.json = true; |
| 36 | + continue; |
| 37 | + } |
| 38 | + if (token === "--note") { |
| 39 | + const note = args[index + 1]; |
| 40 | + if (!note || note.startsWith("-")) { |
| 41 | + return { error: CLAIM_CLAIM_USAGE }; |
| 42 | + } |
| 43 | + options.note = note; |
| 44 | + index += 1; |
| 45 | + continue; |
| 46 | + } |
| 47 | + if (token.startsWith("-")) { |
| 48 | + return { error: `Unknown option: ${token}` }; |
| 49 | + } |
| 50 | + positional.push(token); |
| 51 | + } |
| 52 | + |
| 53 | + if (positional.length !== 2) { |
| 54 | + return { error: CLAIM_CLAIM_USAGE }; |
| 55 | + } |
| 56 | + |
| 57 | + const repo = parseRepoArg(positional[0], CLAIM_CLAIM_USAGE); |
| 58 | + if ("error" in repo) return repo; |
| 59 | + const issue = parseIssueNumberArg(positional[1], CLAIM_CLAIM_USAGE); |
| 60 | + if ("error" in issue) return issue; |
| 61 | + |
| 62 | + return { |
| 63 | + repoFullName: repo.repoFullName, |
| 64 | + issueNumber: issue.issueNumber, |
| 65 | + note: options.note, |
| 66 | + json: options.json, |
| 67 | + }; |
| 68 | +} |
| 69 | + |
| 70 | +export function parseClaimReleaseArgs(args) { |
| 71 | + const options = { json: false }; |
| 72 | + const positional = []; |
| 73 | + |
| 74 | + for (const token of args) { |
| 75 | + if (token === "--json") { |
| 76 | + options.json = true; |
| 77 | + continue; |
| 78 | + } |
| 79 | + if (token.startsWith("-")) { |
| 80 | + return { error: `Unknown option: ${token}` }; |
| 81 | + } |
| 82 | + positional.push(token); |
| 83 | + } |
| 84 | + |
| 85 | + if (positional.length !== 2) { |
| 86 | + return { error: CLAIM_RELEASE_USAGE }; |
| 87 | + } |
| 88 | + |
| 89 | + const repo = parseRepoArg(positional[0], CLAIM_RELEASE_USAGE); |
| 90 | + if ("error" in repo) return repo; |
| 91 | + const issue = parseIssueNumberArg(positional[1], CLAIM_RELEASE_USAGE); |
| 92 | + if ("error" in issue) return issue; |
| 93 | + |
| 94 | + return { |
| 95 | + repoFullName: repo.repoFullName, |
| 96 | + issueNumber: issue.issueNumber, |
| 97 | + json: options.json, |
| 98 | + }; |
| 99 | +} |
| 100 | + |
| 101 | +export function parseClaimListArgs(args) { |
| 102 | + const options = { json: false, repoFullName: null, status: null }; |
| 103 | + const positional = []; |
| 104 | + |
| 105 | + for (let index = 0; index < args.length; index += 1) { |
| 106 | + const token = args[index]; |
| 107 | + if (token === "--json") { |
| 108 | + options.json = true; |
| 109 | + continue; |
| 110 | + } |
| 111 | + if (token === "--repo") { |
| 112 | + const repoArg = args[index + 1]; |
| 113 | + if (!repoArg || repoArg.startsWith("-")) { |
| 114 | + return { error: CLAIM_LIST_USAGE }; |
| 115 | + } |
| 116 | + const repo = parseRepoArg(repoArg, CLAIM_LIST_USAGE); |
| 117 | + if ("error" in repo) return repo; |
| 118 | + options.repoFullName = repo.repoFullName; |
| 119 | + index += 1; |
| 120 | + continue; |
| 121 | + } |
| 122 | + if (token === "--status") { |
| 123 | + const statusArg = args[index + 1]; |
| 124 | + if (!statusArg || statusArg.startsWith("-")) { |
| 125 | + return { error: CLAIM_LIST_USAGE }; |
| 126 | + } |
| 127 | + if (!CLAIM_STATUSES.includes(statusArg)) { |
| 128 | + return { error: `status must be one of: ${CLAIM_STATUSES.join(", ")}.` }; |
| 129 | + } |
| 130 | + options.status = statusArg; |
| 131 | + index += 1; |
| 132 | + continue; |
| 133 | + } |
| 134 | + if (token.startsWith("-")) { |
| 135 | + return { error: `Unknown option: ${token}` }; |
| 136 | + } |
| 137 | + positional.push(token); |
| 138 | + } |
| 139 | + |
| 140 | + if (positional.length > 0) { |
| 141 | + return { error: CLAIM_LIST_USAGE }; |
| 142 | + } |
| 143 | + |
| 144 | + return options; |
| 145 | +} |
| 146 | + |
| 147 | +function display(value) { |
| 148 | + if (value === null || value === undefined) return "-"; |
| 149 | + return String(value); |
| 150 | +} |
| 151 | + |
| 152 | +export function renderClaimsTable(entries) { |
| 153 | + if (!Array.isArray(entries) || entries.length === 0) return "no claim ledger entries"; |
| 154 | + const header = [ |
| 155 | + "repo".padEnd(24), |
| 156 | + "issue".padStart(6), |
| 157 | + "status".padEnd(10), |
| 158 | + "claimed-at".padEnd(24), |
| 159 | + "note".padEnd(16), |
| 160 | + ].join(" "); |
| 161 | + const lines = entries.map((entry) => |
| 162 | + [ |
| 163 | + entry.repoFullName.padEnd(24), |
| 164 | + display(entry.issueNumber).padStart(6), |
| 165 | + entry.status.padEnd(10), |
| 166 | + display(entry.claimedAt).padEnd(24), |
| 167 | + display(entry.note).padEnd(16), |
| 168 | + ].join(" "), |
| 169 | + ); |
| 170 | + return [header, ...lines].join("\n"); |
| 171 | +} |
| 172 | + |
| 173 | +function withClaimLedger(options, run) { |
| 174 | + const ownsLedger = options.openClaimLedger === undefined; |
| 175 | + const claimLedger = (options.openClaimLedger ?? openClaimLedger)(); |
| 176 | + try { |
| 177 | + return run(claimLedger); |
| 178 | + } finally { |
| 179 | + if (ownsLedger) claimLedger.close(); |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +export function runClaimClaim(args, options = {}) { |
| 184 | + const parsed = parseClaimClaimArgs(args); |
| 185 | + if ("error" in parsed) { |
| 186 | + console.error(parsed.error); |
| 187 | + return 2; |
| 188 | + } |
| 189 | + |
| 190 | + try { |
| 191 | + return withClaimLedger(options, (claimLedger) => { |
| 192 | + const claim = claimLedger.claimIssue( |
| 193 | + parsed.repoFullName, |
| 194 | + parsed.issueNumber, |
| 195 | + parsed.note, |
| 196 | + ); |
| 197 | + if (parsed.json) { |
| 198 | + console.log(JSON.stringify({ claim }, null, 2)); |
| 199 | + } else { |
| 200 | + console.log(claim.status); |
| 201 | + } |
| 202 | + return 0; |
| 203 | + }); |
| 204 | + } catch (error) { |
| 205 | + console.error(error instanceof Error ? error.message : String(error)); |
| 206 | + return 2; |
| 207 | + } |
| 208 | +} |
| 209 | + |
| 210 | +export function runClaimRelease(args, options = {}) { |
| 211 | + const parsed = parseClaimReleaseArgs(args); |
| 212 | + if ("error" in parsed) { |
| 213 | + console.error(parsed.error); |
| 214 | + return 2; |
| 215 | + } |
| 216 | + |
| 217 | + try { |
| 218 | + return withClaimLedger(options, (claimLedger) => { |
| 219 | + const claim = claimLedger.releaseClaim(parsed.repoFullName, parsed.issueNumber); |
| 220 | + if (!claim) { |
| 221 | + console.error("claim_not_found"); |
| 222 | + return 2; |
| 223 | + } |
| 224 | + if (parsed.json) { |
| 225 | + console.log(JSON.stringify({ claim }, null, 2)); |
| 226 | + } else { |
| 227 | + console.log(claim.status); |
| 228 | + } |
| 229 | + return 0; |
| 230 | + }); |
| 231 | + } catch (error) { |
| 232 | + console.error(error instanceof Error ? error.message : String(error)); |
| 233 | + return 2; |
| 234 | + } |
| 235 | +} |
| 236 | + |
| 237 | +export function runClaimList(args, options = {}) { |
| 238 | + const parsed = parseClaimListArgs(args); |
| 239 | + if ("error" in parsed) { |
| 240 | + console.error(parsed.error); |
| 241 | + return 2; |
| 242 | + } |
| 243 | + |
| 244 | + try { |
| 245 | + return withClaimLedger(options, (claimLedger) => { |
| 246 | + const filter = {}; |
| 247 | + if (parsed.repoFullName !== null) filter.repoFullName = parsed.repoFullName; |
| 248 | + if (parsed.status !== null) filter.status = parsed.status; |
| 249 | + const claims = claimLedger.listClaims(filter); |
| 250 | + if (parsed.json) { |
| 251 | + console.log(JSON.stringify({ claims }, null, 2)); |
| 252 | + } else { |
| 253 | + console.log(renderClaimsTable(claims)); |
| 254 | + } |
| 255 | + return 0; |
| 256 | + }); |
| 257 | + } catch (error) { |
| 258 | + console.error(error instanceof Error ? error.message : String(error)); |
| 259 | + return 2; |
| 260 | + } |
| 261 | +} |
| 262 | + |
| 263 | +export function runClaimCli(subcommand, args, options = {}) { |
| 264 | + if (subcommand === "claim") return runClaimClaim(args, options); |
| 265 | + if (subcommand === "release") return runClaimRelease(args, options); |
| 266 | + if (subcommand === "list") return runClaimList(args, options); |
| 267 | + console.error(`Unknown claim subcommand: ${subcommand ?? ""}. ${CLAIM_LIST_USAGE}`); |
| 268 | + return 2; |
| 269 | +} |
0 commit comments