|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { Command, Flags } from '@oclif/core'; |
| 4 | +import { printError } from '../../utils/format.js'; |
| 5 | +import { createApiClient, requireAuth } from '../../utils/api-client.js'; |
| 6 | +import { formatOutput } from '../../utils/output-formatter.js'; |
| 7 | +import { readAuthConfig, writeAuthConfig } from '../../utils/auth-config.js'; |
| 8 | + |
| 9 | +/** |
| 10 | + * `os projects create` — provision a new project. |
| 11 | + * |
| 12 | + * Delegates to `ProjectProvisioningService.provisionProject` on the server. |
| 13 | + * On success, optionally activates the new project for the current session |
| 14 | + * and persists `activeProjectId` into `~/.objectstack/credentials.json` |
| 15 | + * (unless `--no-activate` is passed). |
| 16 | + */ |
| 17 | +export default class ProjectsCreate extends Command { |
| 18 | + static override description = 'Provision a new project'; |
| 19 | + |
| 20 | + static override examples = [ |
| 21 | + '$ os projects create --org 00000000-0000-0000-0000-000000000000 --name Staging', |
| 22 | + '$ os projects create --org $ORG --name Dev --plan free', |
| 23 | + '$ os projects create --org $ORG --name "Clone" --clone-from <source-id> --no-activate', |
| 24 | + ]; |
| 25 | + |
| 26 | + static override flags = { |
| 27 | + url: Flags.string({ char: 'u', description: 'Server URL', env: 'OBJECTSTACK_URL' }), |
| 28 | + token: Flags.string({ char: 't', description: 'Authentication token', env: 'OBJECTSTACK_TOKEN' }), |
| 29 | + org: Flags.string({ description: 'Organization id', required: true }), |
| 30 | + name: Flags.string({ description: 'Display name', required: true }), |
| 31 | + plan: Flags.string({ description: 'Billing plan', default: 'free' }), |
| 32 | + driver: Flags.string({ description: 'Data-plane driver id' }), |
| 33 | + 'clone-from': Flags.string({ description: 'Clone schema from an existing project id' }), |
| 34 | + activate: Flags.boolean({ |
| 35 | + description: 'Activate the new project for subsequent CLI calls', |
| 36 | + default: true, |
| 37 | + allowNo: true, |
| 38 | + }), |
| 39 | + format: Flags.string({ |
| 40 | + char: 'f', |
| 41 | + description: 'Output format', |
| 42 | + options: ['json', 'table', 'yaml'], |
| 43 | + default: 'table', |
| 44 | + }), |
| 45 | + }; |
| 46 | + |
| 47 | + async run(): Promise<void> { |
| 48 | + const { flags } = await this.parse(ProjectsCreate); |
| 49 | + |
| 50 | + try { |
| 51 | + const { client, token } = await createApiClient({ url: flags.url, token: flags.token }); |
| 52 | + requireAuth(token); |
| 53 | + |
| 54 | + const res = await client.projects.create({ |
| 55 | + organizationId: flags.org, |
| 56 | + displayName: flags.name, |
| 57 | + plan: flags.plan, |
| 58 | + driver: flags.driver, |
| 59 | + cloneFromProjectId: flags['clone-from'], |
| 60 | + }); |
| 61 | + |
| 62 | + if (flags.activate && res?.project?.id) { |
| 63 | + try { |
| 64 | + await client.projects.activate(res.project.id); |
| 65 | + const cfg = await readAuthConfig().catch(() => null); |
| 66 | + if (cfg) { |
| 67 | + cfg.activeProjectId = res.project.id; |
| 68 | + cfg.lastUsedAt = new Date().toISOString(); |
| 69 | + await writeAuthConfig(cfg); |
| 70 | + } |
| 71 | + } catch (activateError: any) { |
| 72 | + // Creation succeeded — surface activation failure as a warning |
| 73 | + console.error(` ⚠ activation failed: ${activateError.message}`); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + if (flags.format === 'json') { |
| 78 | + formatOutput(res, 'json'); |
| 79 | + } else if (flags.format === 'yaml') { |
| 80 | + formatOutput(res, 'yaml'); |
| 81 | + } else { |
| 82 | + const p = res?.project ?? {}; |
| 83 | + console.log(`\n✓ Project created: ${p.displayName ?? p.id} (${p.id})`); |
| 84 | + if (flags.activate) { |
| 85 | + console.log(` active project set to ${p.id}`); |
| 86 | + } |
| 87 | + console.log(''); |
| 88 | + } |
| 89 | + } catch (error: any) { |
| 90 | + if (flags.format === 'json') { |
| 91 | + console.log(JSON.stringify({ success: false, error: error.message }, null, 2)); |
| 92 | + this.exit(1); |
| 93 | + } |
| 94 | + printError(error.message || String(error)); |
| 95 | + this.exit(1); |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments