|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, Salesforce, Inc. |
| 3 | + * SPDX-License-Identifier: Apache-2 |
| 4 | + * For full license text, see the license.txt file in the repo root or http://www.apache.org/licenses/LICENSE-2.0 |
| 5 | + */ |
| 6 | +import {Args, Flags} from '@oclif/core'; |
| 7 | +import {OdsCommand, TableRenderer} from '@salesforce/b2c-tooling-sdk/cli'; |
| 8 | +import {getApiErrorMessage, type OdsComponents} from '@salesforce/b2c-tooling-sdk'; |
| 9 | +import {t, withDocs} from '../../../i18n/index.js'; |
| 10 | + |
| 11 | +type SandboxAliasModel = OdsComponents['schemas']['SandboxAliasModel']; |
| 12 | + |
| 13 | +/** |
| 14 | + * Command to list sandbox aliases. |
| 15 | + */ |
| 16 | +export default class SandboxAliasList extends OdsCommand<typeof SandboxAliasList> { |
| 17 | + static aliases = ['ods:alias:list']; |
| 18 | + |
| 19 | + static args = { |
| 20 | + sandboxId: Args.string({ |
| 21 | + description: 'Sandbox ID (UUID or realm-instance, e.g., abcd-123)', |
| 22 | + required: true, |
| 23 | + }), |
| 24 | + }; |
| 25 | + |
| 26 | + static description = withDocs( |
| 27 | + t('commands.sandbox.alias.list.description', 'List all hostname aliases for a sandbox'), |
| 28 | + '/cli/sandbox.html#b2c-sandbox-alias-list', |
| 29 | + ); |
| 30 | + |
| 31 | + static enableJsonFlag = true; |
| 32 | + |
| 33 | + static examples = [ |
| 34 | + '<%= config.bin %> <%= command.id %> abc12345-1234-1234-1234-abc123456789', |
| 35 | + '<%= config.bin %> <%= command.id %> zzzv-123', |
| 36 | + '<%= config.bin %> <%= command.id %> zzzv-123 --alias-id some-alias-uuid', |
| 37 | + '<%= config.bin %> <%= command.id %> zzzv-123 --json', |
| 38 | + ]; |
| 39 | + |
| 40 | + static flags = { |
| 41 | + 'alias-id': Flags.string({ |
| 42 | + description: 'Specific alias ID to retrieve (if omitted, lists all aliases)', |
| 43 | + required: false, |
| 44 | + }), |
| 45 | + }; |
| 46 | + |
| 47 | + async run(): Promise<SandboxAliasModel | SandboxAliasModel[]> { |
| 48 | + const {sandboxId} = this.args; |
| 49 | + const {'alias-id': aliasId} = this.flags; |
| 50 | + |
| 51 | + const resolvedSandboxId = await this.resolveSandboxId(sandboxId); |
| 52 | + |
| 53 | + // If alias ID provided, get specific alias; otherwise list all |
| 54 | + if (aliasId) { |
| 55 | + return this.showAlias(resolvedSandboxId, aliasId); |
| 56 | + } |
| 57 | + return this.listAllAliases(resolvedSandboxId); |
| 58 | + } |
| 59 | + |
| 60 | + private async listAllAliases(sandboxId: string): Promise<SandboxAliasModel[]> { |
| 61 | + this.log( |
| 62 | + t('commands.sandbox.alias.list.fetching', 'Fetching aliases for sandbox {{sandboxId}}...', { |
| 63 | + sandboxId: this.args.sandboxId, |
| 64 | + }), |
| 65 | + ); |
| 66 | + |
| 67 | + const result = await this.odsClient.GET('/sandboxes/{sandboxId}/aliases', { |
| 68 | + params: { |
| 69 | + path: {sandboxId}, |
| 70 | + }, |
| 71 | + }); |
| 72 | + |
| 73 | + if (!result.data?.data) { |
| 74 | + const message = getApiErrorMessage(result.error, result.response); |
| 75 | + this.error( |
| 76 | + t('commands.sandbox.alias.list.error', 'Failed to fetch aliases: {{message}}', { |
| 77 | + message, |
| 78 | + }), |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + const aliases = (result.data?.data ?? []) as SandboxAliasModel[]; |
| 83 | + |
| 84 | + if (!this.jsonEnabled()) { |
| 85 | + if (aliases.length === 0) { |
| 86 | + this.log(t('commands.sandbox.alias.list.no_aliases', 'No aliases found')); |
| 87 | + } else { |
| 88 | + this.log(t('commands.sandbox.alias.list.count', 'Found {{count}} alias(es):', {count: aliases.length})); |
| 89 | + const columns = { |
| 90 | + id: { |
| 91 | + header: 'Alias ID', |
| 92 | + get: (row: SandboxAliasModel) => row.id || '-', |
| 93 | + }, |
| 94 | + name: { |
| 95 | + header: 'Hostname', |
| 96 | + get: (row: SandboxAliasModel) => row.name, |
| 97 | + }, |
| 98 | + status: { |
| 99 | + header: 'Status', |
| 100 | + get: (row: SandboxAliasModel) => row.status || '-', |
| 101 | + }, |
| 102 | + unique: { |
| 103 | + header: 'Unique', |
| 104 | + get: (row: SandboxAliasModel) => (row.unique ? 'Yes' : 'No'), |
| 105 | + }, |
| 106 | + domainVerificationRecord: { |
| 107 | + header: 'Verification Record', |
| 108 | + get: (row: SandboxAliasModel) => row.domainVerificationRecord || '-', |
| 109 | + }, |
| 110 | + }; |
| 111 | + const table = new TableRenderer(columns); |
| 112 | + table.render(aliases, ['id', 'name', 'status', 'unique', 'domainVerificationRecord']); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + return aliases; |
| 117 | + } |
| 118 | + |
| 119 | + private async showAlias(sandboxId: string, aliasId: string): Promise<SandboxAliasModel> { |
| 120 | + this.log( |
| 121 | + t('commands.sandbox.alias.list.fetching_one', 'Fetching alias {{aliasId}} for sandbox {{sandboxId}}...', { |
| 122 | + aliasId, |
| 123 | + sandboxId, |
| 124 | + }), |
| 125 | + ); |
| 126 | + |
| 127 | + const result = await this.odsClient.GET('/sandboxes/{sandboxId}/aliases/{sandboxAliasId}', { |
| 128 | + params: { |
| 129 | + path: {sandboxId, sandboxAliasId: aliasId}, |
| 130 | + }, |
| 131 | + }); |
| 132 | + |
| 133 | + if (!result.data?.data) { |
| 134 | + const message = getApiErrorMessage(result.error, result.response); |
| 135 | + this.error( |
| 136 | + t('commands.sandbox.alias.list.error_one', 'Failed to fetch alias: {{message}}', { |
| 137 | + message, |
| 138 | + }), |
| 139 | + ); |
| 140 | + } |
| 141 | + |
| 142 | + const alias = result.data.data as SandboxAliasModel; |
| 143 | + |
| 144 | + if (!this.jsonEnabled()) { |
| 145 | + this.log(''); |
| 146 | + this.log(t('commands.sandbox.alias.list.alias_details', 'Alias Details:')); |
| 147 | + this.log('─'.repeat(60)); |
| 148 | + this.log(`ID: ${alias.id}`); |
| 149 | + this.log(`Name: ${alias.name}`); |
| 150 | + this.log(`Status: ${alias.status}`); |
| 151 | + if (alias.unique) { |
| 152 | + this.log(`Unique: ${alias.unique}`); |
| 153 | + } |
| 154 | + if (alias.domainVerificationRecord) { |
| 155 | + this.log(`Verification Record: ${alias.domainVerificationRecord}`); |
| 156 | + } |
| 157 | + if (alias.registration) { |
| 158 | + this.log(`Registration URL: ${alias.registration}`); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + return alias; |
| 163 | + } |
| 164 | +} |
0 commit comments