|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, salesforce.com, inc. |
| 3 | + * All rights reserved. |
| 4 | + * Licensed under the BSD 3-Clause license. |
| 5 | + * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause |
| 6 | + */ |
| 7 | + |
| 8 | +import { EOL } from 'os'; |
| 9 | +import * as fs from 'fs'; |
| 10 | +import { flags, FlagsConfig, SfdxCommand } from '@salesforce/command'; |
| 11 | +import { |
| 12 | + SfdxError, |
| 13 | + SfdxErrorConfig, |
| 14 | + Config, |
| 15 | + Lifecycle, |
| 16 | + Messages, |
| 17 | + OrgTypes, |
| 18 | + Aliases, |
| 19 | + SandboxEvents, |
| 20 | + SandboxRequest, |
| 21 | + StatusEvent, |
| 22 | + ResultEvent, |
| 23 | + SandboxProcessObject, |
| 24 | +} from '@salesforce/core'; |
| 25 | +import { Duration, upperFirst } from '@salesforce/kit'; |
| 26 | +import { SandboxReporter } from '../../../shared/sandboxReporter'; |
| 27 | + |
| 28 | +Messages.importMessagesDirectory(__dirname); |
| 29 | +const messages = Messages.loadMessages('@salesforce/plugin-org', 'clone'); |
| 30 | + |
| 31 | +export class OrgCloneCommand extends SfdxCommand { |
| 32 | + public static readonly examples = messages.getMessage('examples').split(EOL); |
| 33 | + public static readonly description = messages.getMessage('description'); |
| 34 | + public static readonly requiresProject = false; |
| 35 | + public static readonly requiresUsername = true; |
| 36 | + public static readonly varargs = true; |
| 37 | + public static readonly SANDBOXDEF_SRC_SANDBOXNAME = 'SourceSandboxName'; |
| 38 | + |
| 39 | + public static readonly flagsConfig: FlagsConfig = { |
| 40 | + type: flags.enum({ |
| 41 | + char: 't', |
| 42 | + description: messages.getMessage('flags.type'), |
| 43 | + required: true, |
| 44 | + options: ['sandbox'], |
| 45 | + }), |
| 46 | + definitionfile: flags.filepath({ |
| 47 | + char: 'f', |
| 48 | + description: messages.getMessage('flags.definitionfile'), |
| 49 | + }), |
| 50 | + setdefaultusername: flags.boolean({ |
| 51 | + char: 's', |
| 52 | + description: messages.getMessage('flags.setdefaultusername'), |
| 53 | + }), |
| 54 | + setalias: flags.string({ |
| 55 | + char: 'a', |
| 56 | + description: messages.getMessage('flags.setalias'), |
| 57 | + }), |
| 58 | + wait: flags.minutes({ |
| 59 | + char: 'w', |
| 60 | + description: messages.getMessage('flags.wait'), |
| 61 | + min: Duration.minutes(2), |
| 62 | + default: Duration.minutes(6), |
| 63 | + }), |
| 64 | + }; |
| 65 | + |
| 66 | + public async run(): Promise<unknown> { |
| 67 | + const lifecycle = Lifecycle.getInstance(); |
| 68 | + if (this.flags.type === OrgTypes.Sandbox) { |
| 69 | + // eslint-disable-next-line @typescript-eslint/require-await |
| 70 | + lifecycle.on(SandboxEvents.EVENT_ASYNC_RESULT, async (results: SandboxProcessObject) => { |
| 71 | + // Keep all console output in the command |
| 72 | + this.ux.log(messages.getMessage('commandSuccess', [results.Id, results.SandboxName])); |
| 73 | + }); |
| 74 | + |
| 75 | + // eslint-disable-next-line @typescript-eslint/require-await |
| 76 | + lifecycle.on(SandboxEvents.EVENT_STATUS, async (results: StatusEvent) => { |
| 77 | + this.ux.log(SandboxReporter.sandboxProgress(results)); |
| 78 | + }); |
| 79 | + |
| 80 | + lifecycle.on(SandboxEvents.EVENT_RESULT, async (results: ResultEvent) => { |
| 81 | + const { sandboxReadyForUse, data } = SandboxReporter.logSandboxProcessResult(results); |
| 82 | + this.ux.log(sandboxReadyForUse); |
| 83 | + this.ux.styledHeader('Sandbox Org Cloning Status'); |
| 84 | + this.ux.table(data, { |
| 85 | + columns: [ |
| 86 | + { key: 'key', label: 'Name' }, |
| 87 | + { key: 'value', label: 'Value' }, |
| 88 | + ], |
| 89 | + }); |
| 90 | + |
| 91 | + if (results?.sandboxRes?.authUserName) { |
| 92 | + if (this.flags.setalias) { |
| 93 | + const alias = await Aliases.create({}); |
| 94 | + const result = alias.set(this.flags.setalias, results.sandboxRes.authUserName); |
| 95 | + this.logger.debug('Set Alias: %s result: %s', this.flags.setalias, result); |
| 96 | + } |
| 97 | + if (this.flags.setdefaultusername) { |
| 98 | + const globalConfig: Config = this.configAggregator.getGlobalConfig(); |
| 99 | + globalConfig.set(Config.DEFAULT_USERNAME, results.sandboxRes.authUserName); |
| 100 | + const result = await globalConfig.write(); |
| 101 | + this.logger.debug('Set defaultUsername: %s result: %s', this.flags.setdefaultusername, result); |
| 102 | + } |
| 103 | + } |
| 104 | + }); |
| 105 | + |
| 106 | + const { sandboxReq, srcSandboxName } = this.createSandboxRequest(); |
| 107 | + |
| 108 | + this.logger.debug('Calling clone with SandboxRequest: %s and SandboxName: %s ', sandboxReq, srcSandboxName); |
| 109 | + const wait = this.flags.wait as Duration; |
| 110 | + return this.org.cloneSandbox(sandboxReq, srcSandboxName, { wait }); |
| 111 | + } else { |
| 112 | + throw SfdxError.create( |
| 113 | + new SfdxErrorConfig('@salesforce/plugin-org', 'clone', 'commandOrganizationTypeNotSupport', [ |
| 114 | + OrgTypes.Sandbox, |
| 115 | + ]).addAction('commandOrganizationTypeNotSupportAction', [OrgTypes.Sandbox]) |
| 116 | + ); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + private createSandboxRequest(): { sandboxReq: SandboxRequest; srcSandboxName: string } { |
| 121 | + this.logger.debug('Clone started with args %s ', this.flags); |
| 122 | + this.logger.debug('Clone Varargs: %s ', this.varargs); |
| 123 | + let sandboxDefFileContents = this.readJsonDefFile(); |
| 124 | + let capitalizedVarArgs = {}; |
| 125 | + |
| 126 | + if (sandboxDefFileContents) { |
| 127 | + sandboxDefFileContents = this.lowerToUpper(sandboxDefFileContents); |
| 128 | + } |
| 129 | + if (this.varargs) { |
| 130 | + capitalizedVarArgs = this.lowerToUpper(this.varargs); |
| 131 | + } |
| 132 | + |
| 133 | + // varargs override file input |
| 134 | + const sandboxReq: SandboxRequest = { SandboxName: undefined, ...sandboxDefFileContents, ...capitalizedVarArgs }; |
| 135 | + |
| 136 | + this.logger.debug('SandboxRequest after merging DefFile and Varargs: %s ', sandboxReq); |
| 137 | + |
| 138 | + // try to find the source sandbox name either from the definition file or the commandline arg |
| 139 | + // NOTE the name and the case "SourceSandboxName" must match exactly |
| 140 | + const srcSandboxName = sandboxReq[OrgCloneCommand.SANDBOXDEF_SRC_SANDBOXNAME] as string; |
| 141 | + if (srcSandboxName) { |
| 142 | + // we have to delete this property from the sandboxRequest object, |
| 143 | + // because sandboxRequest object represent the POST request to create SandboxInfo bpo, |
| 144 | + // sandboxInfo does not have a column named SourceSandboxName, this field will be converted to sourceId in the clone call below |
| 145 | + delete sandboxReq[OrgCloneCommand.SANDBOXDEF_SRC_SANDBOXNAME]; |
| 146 | + } else { |
| 147 | + // error - we need SourceSandboxName to know which sandbox to clone from |
| 148 | + throw SfdxError.create( |
| 149 | + new SfdxErrorConfig('@salesforce/plugin-org', 'clone', 'missingSourceSandboxName', [ |
| 150 | + OrgCloneCommand.SANDBOXDEF_SRC_SANDBOXNAME, |
| 151 | + ]).addAction('missingSourceSandboxNameAction', [OrgCloneCommand.SANDBOXDEF_SRC_SANDBOXNAME]) |
| 152 | + ); |
| 153 | + } |
| 154 | + return { sandboxReq, srcSandboxName }; |
| 155 | + } |
| 156 | + |
| 157 | + private lowerToUpper(object: Record<string, unknown>): Record<string, unknown> { |
| 158 | + // the API has keys defined in capital camel case, while the definition schema has them as lower camel case |
| 159 | + // we need to convert lower camel case to upper before merging options so they will override properly |
| 160 | + return Object.fromEntries(Object.entries(object).map(([key, value]) => [upperFirst(key), value])); |
| 161 | + } |
| 162 | + |
| 163 | + private readJsonDefFile(): Record<string, unknown> { |
| 164 | + // the -f option |
| 165 | + if (this.flags.definitionfile) { |
| 166 | + this.logger.debug('Reading JSON DefFile %s ', this.flags.definitionfile); |
| 167 | + return JSON.parse(fs.readFileSync(this.flags.definitionfile, 'utf-8')) as Record<string, unknown>; |
| 168 | + } |
| 169 | + } |
| 170 | +} |
0 commit comments