generated from salesforcecli/plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsnapshot.ts
102 lines (95 loc) · 3.53 KB
/
snapshot.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
* Copyright (c) 2020, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import {
Flags,
SfCommand,
loglevel,
orgApiVersionFlagWithDeprecations,
requiredHubFlagWithDeprecations,
} from '@salesforce/sf-plugins-core';
import { StateAggregator, Messages, SfError } from '@salesforce/core';
import {
OrgSnapshot,
queryByNameOrId,
printSingleRecordTable,
invalidTypeErrorHandler,
} from '../../../shared/snapshot.js';
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/plugin-signups', 'snapshot.create');
const snapshotMessages = Messages.loadMessages('@salesforce/plugin-signups', 'snapshot');
export class SnapshotCreate extends SfCommand<OrgSnapshot> {
public static readonly summary = messages.getMessage('summary');
public static readonly description = messages.getMessage('description');
public static readonly examples = messages.getMessages('examples');
public static readonly aliases = ['force:org:snapshot:create'];
public static readonly deprecateAliases = true;
public static readonly flags = {
'target-dev-hub': requiredHubFlagWithDeprecations,
'api-version': orgApiVersionFlagWithDeprecations,
loglevel,
'source-org': Flags.string({
// command doesn't use target-org, so dash-o is fine
// eslint-disable-next-line sf-plugin/dash-o
char: 'o',
summary: messages.getMessage('flags.source-org.summary'),
required: true,
aliases: ['sourceorg'],
deprecateAliases: true,
parse: async (input) => (input.startsWith('00D') ? input : resolveSourceOrgId(input)),
}),
name: Flags.string({
char: 'n',
summary: messages.getMessage('flags.name.summary'),
required: true,
aliases: ['snapshotname'],
deprecateAliases: true,
}),
description: Flags.string({
char: 'd',
summary: messages.getMessage('flags.description.summary'),
description: messages.getMessage('flags.description.description'),
}),
};
public async run(): Promise<OrgSnapshot> {
const { flags } = await this.parse(SnapshotCreate);
const conn = flags['target-dev-hub'].getConnection(flags['api-version']);
const createResponse = await conn
.sobject('OrgSnapshot')
.create({
SourceOrg: flags['source-org'],
Description: flags.description,
SnapshotName: flags.name,
Content: 'metadatadata',
})
.catch((error: Error) => {
// dev hub does not have snapshot pref enabled
if (error.name === 'NOT_FOUND') {
error.message = snapshotMessages.getMessage('snapshotNotEnabled');
return invalidTypeErrorHandler(error);
} else {
throw error;
}
});
if (createResponse.success === false) {
throw new SfError('An error while created the org snapshot');
}
const result = await queryByNameOrId(conn, createResponse.id);
if (!flags.json) {
printSingleRecordTable(result);
}
return result;
}
}
const resolveSourceOrgId = async (sourceOrgUsernameOrId: string): Promise<string> => {
const stateAggregator = await StateAggregator.create();
const username = stateAggregator.aliases.getValue(sourceOrgUsernameOrId) ?? sourceOrgUsernameOrId;
const org = await stateAggregator.orgs.read(username);
if (!org?.orgId) {
throw new Error(`No org found for ${sourceOrgUsernameOrId}`);
}
return org.orgId;
};