| e93a978 | | | 1 | #!/usr/bin/env node |
| e93a978 | | | 2 | |
| 59e6667 | | | 3 | import { log } from "@clack/prompts"; |
| e93a978 | | | 4 | import { authLogin } from "./commands/auth-login.js"; |
| e93a978 | | | 5 | import { authStatus } from "./commands/auth-status.js"; |
| e93a978 | | | 6 | import { whoami } from "./commands/whoami.js"; |
| 8d8e815 | | | 7 | import { clone } from "./commands/clone.js"; |
| 8d8e815 | | | 8 | import { init } from "./commands/init.js"; |
| 0d9d723 | | | 9 | import { status } from "./commands/status.js"; |
| e93a978 | | | 10 | import { repoList } from "./commands/repo-list.js"; |
| e93a978 | | | 11 | import { repoCreate } from "./commands/repo-create.js"; |
| e93a978 | | | 12 | import { instanceCreate } from "./commands/instance-create.js"; |
| 69d1a72 | | | 13 | import { ciRuns } from "./commands/ci-runs.js"; |
| 69d1a72 | | | 14 | import { ciStatus } from "./commands/ci-status.js"; |
| 69d1a72 | | | 15 | import { ciLogs } from "./commands/ci-logs.js"; |
| 69d1a72 | | | 16 | import { ciTrigger } from "./commands/ci-trigger.js"; |
| 69d1a72 | | | 17 | import { ciCancel } from "./commands/ci-cancel.js"; |
| 17fe7cd | | | 18 | import { doctor } from "./commands/doctor.js"; |
| e93a978 | | | 19 | |
| e93a978 | | | 20 | const USAGE = `Usage: grove <command> |
| e93a978 | | | 21 | |
| e93a978 | | | 22 | Commands: |
| 8d8e815 | | | 23 | init Create a Grove repo and initialize Sapling in current directory |
| 8d8e815 | | | 24 | clone Clone a Grove repository |
| 0d9d723 | | | 25 | status Show current repository info |
| e93a978 | | | 26 | auth login Authenticate with Grove (opens browser) |
| e93a978 | | | 27 | auth status Show current authentication status |
| e93a978 | | | 28 | whoami Show current user |
| e93a978 | | | 29 | repo list List repositories |
| e93a978 | | | 30 | repo create Create a repository |
| e93a978 | | | 31 | instance create Register a Grove instance |
| 17fe7cd | | | 32 | doctor Check your Grove setup for common issues |
| 69d1a72 | | | 33 | ci runs List pipeline runs |
| 69d1a72 | | | 34 | ci status Show pipeline run details |
| 69d1a72 | | | 35 | ci logs Show step logs |
| 69d1a72 | | | 36 | ci trigger Manually trigger pipelines |
| 69d1a72 | | | 37 | ci cancel Cancel a running pipeline |
| e93a978 | | | 38 | |
| e93a978 | | | 39 | Options: |
| e93a978 | | | 40 | --help Show this help message |
| e93a978 | | | 41 | --version Show version`; |
| e93a978 | | | 42 | |
| 0d9d723 | | | 43 | const COMMAND_HELP: Record<string, string> = { |
| 0d9d723 | | | 44 | init: `Usage: grove init [directory] [options] |
| 0d9d723 | | | 45 | |
| 0d9d723 | | | 46 | Create a Grove repository and initialize Sapling in the current directory. |
| 0d9d723 | | | 47 | If the directory contains a .git repo, imports full git history. |
| 0d9d723 | | | 48 | |
| 0d9d723 | | | 49 | Options: |
| 0d9d723 | | | 50 | --owner <org> Set the repository owner (org name) |
| 0d9d723 | | | 51 | --description <desc> Set the repository description |
| 0d9d723 | | | 52 | --private Create a private repository |
| 0d9d723 | | | 53 | --branch <branch> Set the default branch name`, |
| 0d9d723 | | | 54 | |
| 0d9d723 | | | 55 | clone: `Usage: grove clone <owner/repo|repo> [destination] |
| 0d9d723 | | | 56 | |
| 0d9d723 | | | 57 | Clone a Grove repository into a new directory. |
| 0d9d723 | | | 58 | If only a repo name is given (no owner/), looks up the owner via the API.`, |
| 0d9d723 | | | 59 | |
| 0d9d723 | | | 60 | status: `Usage: grove status |
| 0d9d723 | | | 61 | |
| 0d9d723 | | | 62 | Show info about the current Grove repository (name, owner, branch, URL).`, |
| 0d9d723 | | | 63 | |
| 0d9d723 | | | 64 | whoami: `Usage: grove whoami |
| 0d9d723 | | | 65 | |
| 0d9d723 | | | 66 | Show the currently authenticated user.`, |
| 0d9d723 | | | 67 | |
| 0d9d723 | | | 68 | "auth login": `Usage: grove auth login [--hub <url>] |
| 0d9d723 | | | 69 | |
| 0d9d723 | | | 70 | Authenticate with Grove by opening the browser. |
| 0d9d723 | | | 71 | |
| 0d9d723 | | | 72 | Options: |
| 0d9d723 | | | 73 | --hub <url> Override the Grove hub URL`, |
| 0d9d723 | | | 74 | |
| 0d9d723 | | | 75 | "auth status": `Usage: grove auth status |
| 0d9d723 | | | 76 | |
| 0d9d723 | | | 77 | Show current authentication status, token expiry, and hub URL.`, |
| 0d9d723 | | | 78 | |
| 0d9d723 | | | 79 | "repo list": `Usage: grove repo list |
| 0d9d723 | | | 80 | |
| 0d9d723 | | | 81 | List all repositories you have access to.`, |
| 0d9d723 | | | 82 | |
| 0d9d723 | | | 83 | "repo create": `Usage: grove repo create <name> [options] |
| 0d9d723 | | | 84 | |
| 0d9d723 | | | 85 | Create a new repository on Grove without initializing a local directory. |
| 0d9d723 | | | 86 | Use 'grove init' to create and initialize in one step. |
| 0d9d723 | | | 87 | |
| 0d9d723 | | | 88 | Options: |
| 0d9d723 | | | 89 | --owner <org> Set the repository owner (org name) |
| 0d9d723 | | | 90 | --description <desc> Set the repository description |
| 0d9d723 | | | 91 | --branch <branch> Set the default branch name |
| 0d9d723 | | | 92 | --private Create a private repository |
| 0d9d723 | | | 93 | --no-seed Skip creating an initial commit`, |
| 0d9d723 | | | 94 | |
| 0d9d723 | | | 95 | "instance create": `Usage: grove instance create --region <region> --size <size> [options] |
| 0d9d723 | | | 96 | |
| 0d9d723 | | | 97 | Register a new Grove instance. |
| 0d9d723 | | | 98 | |
| 0d9d723 | | | 99 | Options: |
| 0d9d723 | | | 100 | --name <name> Instance name (default: "grove") |
| 0d9d723 | | | 101 | --region <region> Region (required) |
| 0d9d723 | | | 102 | --size <size> Instance size (required) |
| 0d9d723 | | | 103 | --ip <ip> IP address |
| 0d9d723 | | | 104 | --domain <domain> Domain name`, |
| 0d9d723 | | | 105 | |
| 0d9d723 | | | 106 | "ci runs": `Usage: grove ci runs [options] |
| 0d9d723 | | | 107 | |
| 0d9d723 | | | 108 | List pipeline runs for the current repository. |
| 0d9d723 | | | 109 | |
| 0d9d723 | | | 110 | Options: |
| 0d9d723 | | | 111 | --repo <owner/repo> Specify repository (default: inferred from .sl/config) |
| 0d9d723 | | | 112 | --status <status> Filter by status (passed, failed, running, pending) |
| 0d9d723 | | | 113 | --limit <n> Number of runs to show (default: 10)`, |
| 0d9d723 | | | 114 | |
| 0d9d723 | | | 115 | "ci status": `Usage: grove ci status [run-id] [options] |
| 0d9d723 | | | 116 | |
| 0d9d723 | | | 117 | Show details and steps for a pipeline run. |
| 0d9d723 | | | 118 | If no run ID is given, shows the latest run. |
| 0d9d723 | | | 119 | |
| 0d9d723 | | | 120 | Options: |
| 0d9d723 | | | 121 | --repo <owner/repo> Specify repository (default: inferred from .sl/config)`, |
| 0d9d723 | | | 122 | |
| 0d9d723 | | | 123 | "ci logs": `Usage: grove ci logs <run-id> <step-index> [options] |
| 0d9d723 | | | 124 | |
| 0d9d723 | | | 125 | Show logs for a specific step in a pipeline run. |
| 0d9d723 | | | 126 | |
| 0d9d723 | | | 127 | Options: |
| 0d9d723 | | | 128 | --repo <owner/repo> Specify repository (default: inferred from .sl/config)`, |
| 0d9d723 | | | 129 | |
| 0d9d723 | | | 130 | "ci trigger": `Usage: grove ci trigger [options] |
| 0d9d723 | | | 131 | |
| 0d9d723 | | | 132 | Manually trigger pipelines for the current repository. |
| 0d9d723 | | | 133 | |
| 0d9d723 | | | 134 | Options: |
| 0d9d723 | | | 135 | --repo <owner/repo> Specify repository (default: inferred from .sl/config) |
| 0d9d723 | | | 136 | --ref <branch> Branch to trigger on`, |
| 0d9d723 | | | 137 | |
| 0d9d723 | | | 138 | "ci cancel": `Usage: grove ci cancel <run-id> [options] |
| 0d9d723 | | | 139 | |
| 0d9d723 | | | 140 | Cancel a running pipeline. |
| 0d9d723 | | | 141 | |
| 0d9d723 | | | 142 | Options: |
| 0d9d723 | | | 143 | --repo <owner/repo> Specify repository (default: inferred from .sl/config)`, |
| 17fe7cd | | | 144 | |
| 17fe7cd | | | 145 | doctor: `Usage: grove doctor |
| 17fe7cd | | | 146 | |
| 17fe7cd | | | 147 | Check your Grove setup for common issues. |
| 17fe7cd | | | 148 | |
| 17fe7cd | | | 149 | Checks: |
| 17fe7cd | | | 150 | - Grove config and authentication |
| 17fe7cd | | | 151 | - Auth token validity |
| 17fe7cd | | | 152 | - TLS certificates |
| 17fe7cd | | | 153 | - Sapling (sl) installation and PATH |
| 17fe7cd | | | 154 | - Hub reachability |
| 17fe7cd | | | 155 | - Repository config (if inside a repo)`, |
| 0d9d723 | | | 156 | }; |
| 0d9d723 | | | 157 | |
| 0d9d723 | | | 158 | function showHelp(command: string): boolean { |
| 0d9d723 | | | 159 | const help = COMMAND_HELP[command]; |
| 0d9d723 | | | 160 | if (help) { |
| 0d9d723 | | | 161 | console.log(help); |
| 0d9d723 | | | 162 | return true; |
| 0d9d723 | | | 163 | } |
| 0d9d723 | | | 164 | return false; |
| 0d9d723 | | | 165 | } |
| 0d9d723 | | | 166 | |
| e93a978 | | | 167 | const args = process.argv.slice(2); |
| e93a978 | | | 168 | const cmd = args[0]; |
| e93a978 | | | 169 | const sub = args[1]; |
| e93a978 | | | 170 | const rest = args.slice(2); |
| e93a978 | | | 171 | |
| 0d9d723 | | | 172 | function wantsHelp(a: string[]): boolean { |
| 0d9d723 | | | 173 | return a.includes("--help") || a.includes("-h"); |
| 0d9d723 | | | 174 | } |
| 0d9d723 | | | 175 | |
| e93a978 | | | 176 | async function main() { |
| e93a978 | | | 177 | if (!cmd || cmd === "--help" || cmd === "-h") { |
| e93a978 | | | 178 | console.log(USAGE); |
| e93a978 | | | 179 | process.exit(0); |
| e93a978 | | | 180 | } |
| e93a978 | | | 181 | |
| e93a978 | | | 182 | if (cmd === "--version" || cmd === "-v") { |
| e93a978 | | | 183 | const { readFile } = await import("node:fs/promises"); |
| e93a978 | | | 184 | const { fileURLToPath } = await import("node:url"); |
| e93a978 | | | 185 | const { dirname, join } = await import("node:path"); |
| e93a978 | | | 186 | const dir = dirname(fileURLToPath(import.meta.url)); |
| e93a978 | | | 187 | const pkg = JSON.parse(await readFile(join(dir, "..", "package.json"), "utf-8")); |
| e93a978 | | | 188 | console.log(`grove ${pkg.version}`); |
| e93a978 | | | 189 | process.exit(0); |
| e93a978 | | | 190 | } |
| e93a978 | | | 191 | |
| e93a978 | | | 192 | if (cmd === "auth") { |
| 0d9d723 | | | 193 | if (wantsHelp(args)) { |
| 0d9d723 | | | 194 | const key = sub && !sub.startsWith("-") ? `auth ${sub}` : undefined; |
| 0d9d723 | | | 195 | if (key && showHelp(key)) process.exit(0); |
| 0d9d723 | | | 196 | console.log(`Usage: grove auth <login|status>\n\nSubcommands:\n login Authenticate with Grove (opens browser)\n status Show current authentication status`); |
| 0d9d723 | | | 197 | process.exit(0); |
| 0d9d723 | | | 198 | } |
| e93a978 | | | 199 | if (sub === "login") return authLogin(rest); |
| e93a978 | | | 200 | if (sub === "status") return authStatus(); |
| 59e6667 | | | 201 | log.error(`Unknown command: auth ${sub || ""}\nRun: grove auth --help`); |
| e93a978 | | | 202 | process.exit(1); |
| e93a978 | | | 203 | } |
| e93a978 | | | 204 | |
| 0d9d723 | | | 205 | if (cmd === "init") { |
| 0d9d723 | | | 206 | if (wantsHelp(args)) { showHelp("init"); process.exit(0); } |
| 0d9d723 | | | 207 | return init(args.slice(1)); |
| 0d9d723 | | | 208 | } |
| 0d9d723 | | | 209 | if (cmd === "clone") { |
| 0d9d723 | | | 210 | if (wantsHelp(args)) { showHelp("clone"); process.exit(0); } |
| 0d9d723 | | | 211 | return clone(args.slice(1)); |
| 0d9d723 | | | 212 | } |
| 0d9d723 | | | 213 | if (cmd === "status") { |
| 0d9d723 | | | 214 | if (wantsHelp(args)) { showHelp("status"); process.exit(0); } |
| 0d9d723 | | | 215 | return status(); |
| 0d9d723 | | | 216 | } |
| 0d9d723 | | | 217 | if (cmd === "whoami") { |
| 0d9d723 | | | 218 | if (wantsHelp(args)) { showHelp("whoami"); process.exit(0); } |
| 0d9d723 | | | 219 | return whoami(); |
| 0d9d723 | | | 220 | } |
| e93a978 | | | 221 | |
| e93a978 | | | 222 | if (cmd === "repo") { |
| 0d9d723 | | | 223 | if (wantsHelp(args)) { |
| 0d9d723 | | | 224 | const key = sub && !sub.startsWith("-") ? `repo ${sub}` : undefined; |
| 0d9d723 | | | 225 | if (key && showHelp(key)) process.exit(0); |
| 0d9d723 | | | 226 | console.log(`Usage: grove repo <list|create>\n\nSubcommands:\n list List repositories\n create Create a repository`); |
| 0d9d723 | | | 227 | process.exit(0); |
| 0d9d723 | | | 228 | } |
| e93a978 | | | 229 | if (sub === "list" || sub === "ls") return repoList(); |
| e93a978 | | | 230 | if (sub === "create") return repoCreate(rest); |
| 59e6667 | | | 231 | log.error(`Unknown command: repo ${sub || ""}\nRun: grove repo --help`); |
| e93a978 | | | 232 | process.exit(1); |
| e93a978 | | | 233 | } |
| e93a978 | | | 234 | |
| e93a978 | | | 235 | if (cmd === "instance") { |
| 0d9d723 | | | 236 | if (wantsHelp(args)) { |
| 0d9d723 | | | 237 | const key = sub && !sub.startsWith("-") ? `instance ${sub}` : undefined; |
| 0d9d723 | | | 238 | if (key && showHelp(key)) process.exit(0); |
| 0d9d723 | | | 239 | console.log(`Usage: grove instance <create>\n\nSubcommands:\n create Register a Grove instance`); |
| 0d9d723 | | | 240 | process.exit(0); |
| 0d9d723 | | | 241 | } |
| e93a978 | | | 242 | if (sub === "create") return instanceCreate(rest); |
| 59e6667 | | | 243 | log.error(`Unknown command: instance ${sub || ""}\nRun: grove instance --help`); |
| e93a978 | | | 244 | process.exit(1); |
| e93a978 | | | 245 | } |
| e93a978 | | | 246 | |
| 17fe7cd | | | 247 | if (cmd === "doctor") { |
| 17fe7cd | | | 248 | if (wantsHelp(args)) { showHelp("doctor"); process.exit(0); } |
| 17fe7cd | | | 249 | return doctor(); |
| 17fe7cd | | | 250 | } |
| 17fe7cd | | | 251 | |
| 69d1a72 | | | 252 | if (cmd === "ci") { |
| 0d9d723 | | | 253 | if (wantsHelp(args)) { |
| 0d9d723 | | | 254 | const key = sub && !sub.startsWith("-") ? `ci ${sub}` : undefined; |
| 0d9d723 | | | 255 | if (key && showHelp(key)) process.exit(0); |
| 0d9d723 | | | 256 | console.log(`Usage: grove ci <runs|status|logs|trigger|cancel>\n\nSubcommands:\n runs List pipeline runs\n status Show pipeline run details\n logs Show step logs\n trigger Manually trigger pipelines\n cancel Cancel a running pipeline`); |
| 0d9d723 | | | 257 | process.exit(0); |
| 0d9d723 | | | 258 | } |
| 69d1a72 | | | 259 | if (sub === "runs" || sub === "ls") return ciRuns(rest); |
| 69d1a72 | | | 260 | if (sub === "status" || sub === "show") return ciStatus(rest); |
| 69d1a72 | | | 261 | if (sub === "logs" || sub === "log") return ciLogs(rest); |
| 69d1a72 | | | 262 | if (sub === "trigger" || sub === "run") return ciTrigger(rest); |
| 69d1a72 | | | 263 | if (sub === "cancel") return ciCancel(rest); |
| 59e6667 | | | 264 | log.error(`Unknown command: ci ${sub || ""}\nRun: grove ci --help`); |
| 69d1a72 | | | 265 | process.exit(1); |
| 69d1a72 | | | 266 | } |
| 69d1a72 | | | 267 | |
| 59e6667 | | | 268 | log.error(`Unknown command: ${cmd}\nRun: grove --help`); |
| e93a978 | | | 269 | process.exit(1); |
| e93a978 | | | 270 | } |
| e93a978 | | | 271 | |
| e93a978 | | | 272 | main().catch((err) => { |
| 59e6667 | | | 273 | log.error(err.message || String(err)); |
| e93a978 | | | 274 | process.exit(1); |
| e93a978 | | | 275 | }); |