Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sl/w 17246841 #1297

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions src/commands/org/create/sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ export default class CreateSandbox extends SandboxCommandBase<SandboxCommandResp

this.debug('Create started with args %s ', this.flags);
this.validateFlags();

return this.createSandbox();
}

Expand Down Expand Up @@ -187,25 +188,21 @@ export default class CreateSandbox extends SandboxCommandBase<SandboxCommandResp

private async createSandbox(): Promise<SandboxCommandResponse> {
const lifecycle = Lifecycle.getInstance();

this.prodOrg = this.flags['target-org'];

const sandboxReq = await this.createSandboxRequest();
await this.confirmSandboxReq({
...sandboxReq,
});
this.initSandboxProcessData(sandboxReq);

this.registerLifecycleListeners(lifecycle, {
isAsync: this.flags.async,
setDefault: this.flags['set-default'],
alias: this.flags.alias,
prodOrg: this.prodOrg,
tracksSource: this.flags['no-track-source'] === true ? false : undefined,
});
const sandboxReq = await this.createSandboxRequest();
await this.confirmSandboxReq({
...sandboxReq,
});
this.initSandboxProcessData(sandboxReq);

if (!this.flags.async) {
this.spinner.start('Sandbox Create');
}

this.debug('Calling create with SandboxRequest: %s ', sandboxReq);

Expand All @@ -217,12 +214,12 @@ export default class CreateSandbox extends SandboxCommandBase<SandboxCommandResp
});
this.latestSandboxProgressObj = sandboxProcessObject;
this.saveSandboxProgressConfig();

if (this.flags.async) {
process.exitCode = 68;
}
return this.getSandboxCommandResponse();
} catch (err) {
this.spinner.stop();
if (this.pollingTimeOut && this.latestSandboxProgressObj) {
void lifecycle.emit(SandboxEvents.EVENT_ASYNC_RESULT, undefined);
process.exitCode = 68;
Expand Down
4 changes: 0 additions & 4 deletions src/commands/org/refresh/sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,6 @@ export default class RefreshSandbox extends SandboxCommandBase<SandboxCommandRes
this.initSandboxProcessData(this.sbxConfig);

try {
if (!this.flags.async) {
this.spinner.start('Sandbox Refresh');
}

const sandboxProcessObject = await this.prodOrg.refreshSandbox(updateableSandboxInfo, {
wait: this.flags['wait'],
interval: this.flags['poll-interval'],
Expand Down
4 changes: 0 additions & 4 deletions src/commands/org/resume/sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,6 @@ export default class ResumeSandbox extends SandboxCommandBase<SandboxCommandResp

const sandboxReq = this.createResumeSandboxRequest();

if (this.flags.wait?.seconds && this.flags.wait.seconds > 0) {
this.spinner.start(`Resume ${this.sandboxRequestData.action ?? 'Create/Refresh'}`);
}

this.debug('Calling resume with ResumeSandboxRequest: %s ', sandboxReq);

try {
Expand Down
100 changes: 82 additions & 18 deletions src/shared/sandboxCommandBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import os from 'node:os';

import { MultiStageOutput } from '@oclif/multi-stage-output';
import { SfCommand } from '@salesforce/sf-plugins-core';
import { Config } from '@oclif/core';
import {
Expand All @@ -24,6 +24,13 @@ import {
import { SandboxProgress } from './sandboxProgress.js';
import { State } from './stagedProgress.js';

type SandboxData = {
sandboxStatus: StatusEvent;
status: string;
id: string;
copyProgress: number;
};

Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/plugin-org', 'sandboxbase');

Expand All @@ -50,6 +57,7 @@ export abstract class SandboxCommandBase<T> extends SfCommand<T> {
: this.constructor.name === 'CreateSandbox'
? 'Create'
: 'Create/Refresh';

this.sandboxProgress = new SandboxProgress({ action: this.action });
}
protected async getSandboxRequestConfig(): Promise<SandboxRequestCache> {
Expand Down Expand Up @@ -89,25 +97,57 @@ export abstract class SandboxCommandBase<T> extends SfCommand<T> {
lifecycle: Lifecycle,
options: { isAsync: boolean; alias?: string; setDefault?: boolean; prodOrg?: Org; tracksSource?: boolean }
): void {
const mso = new MultiStageOutput<SandboxData>({
stages: ['Creating new sandbox', 'Authenticating', 'Done'],
title: 'Sandbox Process',
jsonEnabled: false,
postStagesBlock: [
{
label: 'Status',
get: (data) => data?.status,
type: 'dynamic-key-value',
bold: true,
},
{
label: 'Sandbox ID',
get: (data) => data?.id,
type: 'static-key-value',
},
{
label: 'Copy Progress',
get: (data) => `${data?.copyProgress ?? 0}%`,
type: 'dynamic-key-value',
},
],
});
lifecycle.on('POLLING_TIME_OUT', async () => {
this.pollingTimeOut = true;
mso.updateData({ status: 'Polling Timeout' });
return Promise.resolve(this.updateSandboxRequestData());
});

lifecycle.on(SandboxEvents.EVENT_RESUME, async (results: SandboxProcessObject) => {
this.latestSandboxProgressObj = results;
this.sandboxProgress.markPreviousStagesAsCompleted(
results.Status !== 'Completed' ? results.Status : 'Authenticating'
);

if (results.Status === 'Activating') {
mso.goto('Authenticating');
mso.updateData({ status: results.Status, id: results.Id, copyProgress: results.CopyProgress });
} else if (results.Status === 'Completed') {
mso.goto('Done');
mso.updateData({ status: results.Status, id: results.Id, copyProgress: results.CopyProgress });
mso.stop();
}
return Promise.resolve(this.updateSandboxRequestData());
});

lifecycle.on(SandboxEvents.EVENT_ASYNC_RESULT, async (results?: SandboxProcessObject) => {
this.latestSandboxProgressObj = results ?? this.latestSandboxProgressObj;
this.updateSandboxRequestData();

if (!options.isAsync) {
this.spinner.stop();
mso.stop();
}

// things that require data on latestSandboxProgressObj
if (this.latestSandboxProgressObj) {
const progress = this.sandboxProgress.getSandboxProgress({
Expand All @@ -117,11 +157,15 @@ export abstract class SandboxCommandBase<T> extends SfCommand<T> {
const currentStage = progress.status;
this.sandboxProgress.markPreviousStagesAsCompleted(currentStage);
this.updateStage(currentStage, 'inProgress');
this.updateProgress(
{ sandboxProcessObj: this.latestSandboxProgressObj, sandboxRes: undefined },
options.isAsync
);
mso.goto('Creating new sandbox');
mso.updateData({
status: currentStage,
id: this.latestSandboxProgressObj?.Id,
copyProgress: this.latestSandboxProgressObj?.CopyProgress,
});
mso.goto(progress.status);
}

if (this.pollingTimeOut) {
this.warn(messages.getMessage('warning.ClientTimeoutWaitingForSandboxProcess', [this.action.toLowerCase()]));
}
Expand All @@ -135,7 +179,14 @@ export abstract class SandboxCommandBase<T> extends SfCommand<T> {
const progress = this.sandboxProgress.getSandboxProgress(results);
const currentStage = progress.status;
this.updateStage(currentStage, 'inProgress');
return Promise.resolve(this.updateProgress(results, options.isAsync));

mso.goto('Creating new sandbox');
mso.updateData({
status: currentStage,
id: results.sandboxProcessObj.Id,
copyProgress: results.sandboxProcessObj.CopyProgress,
});
return Promise.resolve(this.updateProgress(results));
});

lifecycle.on(SandboxEvents.EVENT_AUTH, async (results: SandboxUserAuthResponse) => {
Expand All @@ -147,10 +198,27 @@ export abstract class SandboxCommandBase<T> extends SfCommand<T> {
this.latestSandboxProgressObj = results.sandboxProcessObj;
this.updateSandboxRequestData();
this.sandboxProgress.markPreviousStagesAsCompleted();
this.updateProgress(results, options.isAsync);
this.updateProgress(results);

if (!options.isAsync) {
this.progress.stop();
mso.stop();
}

if (results.sandboxProcessObj.Status === 'Authenticating') {
mso.goto('Authenticating');
mso.updateData({
status: results.sandboxProcessObj.Status,
id: results.sandboxProcessObj.Id,
copyProgress: results.sandboxProcessObj.CopyProgress,
});
}
mso.goto('Done');
mso.updateData({
status: 'Completed',
id: results.sandboxProcessObj.Id,
copyProgress: results.sandboxProcessObj.CopyProgress,
});
mso.stop();
if (results.sandboxRes?.authUserName) {
const authInfo = await AuthInfo.create({ username: results.sandboxRes?.authUserName });
await authInfo.handleAliasAndDefaultSettings({
Expand All @@ -161,7 +229,7 @@ export abstract class SandboxCommandBase<T> extends SfCommand<T> {
});
}
this.removeSandboxProgressConfig();
this.updateProgress(results, options.isAsync);
this.updateProgress(results);
this.reportResults(results);
});

Expand Down Expand Up @@ -201,8 +269,7 @@ export abstract class SandboxCommandBase<T> extends SfCommand<T> {
}

protected updateProgress(
event: StatusEvent | (Omit<ResultEvent, 'sandboxRes'> & { sandboxRes?: ResultEvent['sandboxRes'] }),
isAsync: boolean
event: StatusEvent | (Omit<ResultEvent, 'sandboxRes'> & { sandboxRes?: ResultEvent['sandboxRes'] })
): void {
const sandboxProgress = this.sandboxProgress.getSandboxProgress(event);
this.sandboxUsername = (event as ResultEvent).sandboxRes?.authUserName;
Expand All @@ -211,9 +278,6 @@ export abstract class SandboxCommandBase<T> extends SfCommand<T> {
sandboxProgress,
sandboxProcessObj: event.sandboxProcessObj,
};
if (!isAsync) {
this.spinner.status = this.sandboxProgress.formatProgressStatus();
}
}

protected updateStage(stage: string | undefined, state: State): void {
Expand Down
9 changes: 1 addition & 8 deletions src/shared/sandboxProgress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,10 @@ export class SandboxProgress extends StagedProgress<SandboxStatusData> {
}

public formatProgressStatus(withClock = true): string {
const table = getSandboxTableAsText(undefined, this.statusData?.sandboxProcessObj);
return [
withClock && this.statusData
? `${getClockForSeconds(this.statusData.sandboxProgress.remainingWaitTime)} until timeout. ${
this.statusData.sandboxProgress.percentComplete ?? 0
}%`
? `${getClockForSeconds(this.statusData.sandboxProgress.remainingWaitTime)} until timeout.`
: undefined,
table,
'---------------------',
`Sandbox ${this.action ?? ''} Stages`,
this.formatStages(),
]
.filter(isDefined)
.join(os.EOL);
Expand Down
Loading