|
| 1 | +import { |
| 2 | + defineCommand, |
| 3 | + UsageError, |
| 4 | + profileSchemaItemPath, |
| 5 | + detectOutputFormat, |
| 6 | + type ProfileSchemaUpdateRequest, |
| 7 | +} from "bailian-cli-core"; |
| 8 | +import { emitResult, emitBare } from "bailian-cli-runtime"; |
| 9 | +import type { FlagsDef, ParsedFlags } from "bailian-cli-core"; |
| 10 | + |
| 11 | +const UPDATE_FLAGS = { |
| 12 | + schemaId: { |
| 13 | + type: "string", |
| 14 | + valueHint: "<id>", |
| 15 | + description: "Profile schema ID (required)", |
| 16 | + required: true, |
| 17 | + }, |
| 18 | + name: { type: "string", valueHint: "<name>", description: "New schema name" }, |
| 19 | + description: { type: "string", valueHint: "<text>", description: "New schema description" }, |
| 20 | + attributeOps: { |
| 21 | + type: "string", |
| 22 | + valueHint: "<json>", |
| 23 | + description: |
| 24 | + 'Attribute operations JSON array: [{"op":"add","name":"plan"},{"op":"delete","attribute_id":"attr_1"}]', |
| 25 | + }, |
| 26 | + memoryLibraryId: { type: "string", valueHint: "<id>", description: "Memory library ID" }, |
| 27 | +} satisfies FlagsDef; |
| 28 | +type UpdateFlags = ParsedFlags<typeof UPDATE_FLAGS>; |
| 29 | + |
| 30 | +export default defineCommand({ |
| 31 | + description: "Update a profile schema's name, description, or attributes", |
| 32 | + auth: "apiKey", |
| 33 | + usageArgs: "--schema-id <id> [--name <name>] [--attribute-ops <json>] [flags]", |
| 34 | + flags: UPDATE_FLAGS, |
| 35 | + notes: ["Attribute IDs for update/delete operations come from `memory profile detail`."], |
| 36 | + exampleArgs: [ |
| 37 | + '--schema-id schema_xxx --name "user_basic_v2"', |
| 38 | + '--schema-id schema_xxx --attribute-ops \'[{"op":"add","name":"plan","description":"subscription plan"}]\'', |
| 39 | + '--schema-id schema_xxx --attribute-ops \'[{"op":"delete","attribute_id":"attr_1"}]\'', |
| 40 | + ], |
| 41 | + validate: (f: UpdateFlags) => |
| 42 | + !f.name && !f.description && !f.attributeOps |
| 43 | + ? "Provide --name, --description, or --attribute-ops." |
| 44 | + : undefined, |
| 45 | + async run(ctx) { |
| 46 | + const { settings, flags } = ctx; |
| 47 | + const format = detectOutputFormat(settings.output); |
| 48 | + |
| 49 | + const body: ProfileSchemaUpdateRequest = {}; |
| 50 | + if (flags.name) body.name = flags.name; |
| 51 | + if (flags.description) body.description = flags.description; |
| 52 | + if (flags.memoryLibraryId) body.memory_library_id = flags.memoryLibraryId; |
| 53 | + |
| 54 | + if (flags.attributeOps) { |
| 55 | + try { |
| 56 | + body.attributes_operations = JSON.parse(flags.attributeOps); |
| 57 | + } catch { |
| 58 | + throw new UsageError("--attribute-ops must be valid JSON array"); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + const path = profileSchemaItemPath(flags.schemaId); |
| 63 | + |
| 64 | + if (settings.dryRun) { |
| 65 | + emitResult({ endpoint: ctx.client.url(path), method: "PATCH", request: body }, format); |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + const response = await ctx.client.requestJson<{ request_id: string }>({ |
| 70 | + path, |
| 71 | + method: "PATCH", |
| 72 | + body, |
| 73 | + }); |
| 74 | + |
| 75 | + if (settings.quiet || format === "text") { |
| 76 | + emitBare(`Profile schema ${flags.schemaId} updated.`); |
| 77 | + } else { |
| 78 | + emitResult(response, format); |
| 79 | + } |
| 80 | + }, |
| 81 | +}); |
0 commit comments