-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathset.ts
More file actions
111 lines (93 loc) · 3.16 KB
/
set.ts
File metadata and controls
111 lines (93 loc) · 3.16 KB
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
103
104
105
106
107
108
109
110
111
import {Args} from '@oclif/core';
import {MrtCommand} from '@salesforce/b2c-tooling/cli';
import {setEnvVars} from '@salesforce/b2c-tooling/operations/mrt';
import {t} from '../../../../i18n/index.js';
/**
* Set environment variables on an MRT project environment.
*/
export default class MrtEnvVarSet extends MrtCommand<typeof MrtEnvVarSet> {
static args = {
variables: Args.string({
description: 'Environment variables in KEY=value format',
required: true,
}),
};
static description = t(
'commands.mrt.env.var.set.description',
'Set environment variables on a Managed Runtime environment',
);
static enableJsonFlag = true;
static examples = [
'<%= config.bin %> <%= command.id %> MY_VAR=value --project acme-storefront --environment production',
'<%= config.bin %> <%= command.id %> API_KEY=secret DEBUG=true -p my-project -e staging',
'<%= config.bin %> <%= command.id %> "MESSAGE=hello world" -p my-project -e production',
];
static flags = {
...MrtCommand.baseFlags,
};
// Allow multiple arguments
static strict = false;
async run(): Promise<{variables: Record<string, string>; project: string; environment: string}> {
this.requireMrtCredentials();
const {argv} = await this.parse(MrtEnvVarSet);
const {mrtProject: project, mrtEnvironment: environment} = this.resolvedConfig;
if (!project) {
this.error(
'MRT project is required. Provide --project flag, set SFCC_MRT_PROJECT, or set mrtProject in dw.json.',
);
}
if (!environment) {
this.error(
'MRT environment is required. Provide --environment flag, set SFCC_MRT_ENVIRONMENT, or set mrtEnvironment in dw.json.',
);
}
// Parse KEY=value arguments
const variables: Record<string, string> = {};
const keys: string[] = [];
for (const arg of argv as string[]) {
const eqIndex = arg.indexOf('=');
if (eqIndex === -1) {
this.error(
t('commands.mrt.env.var.set.invalidFormat', 'Invalid format: {{arg}}. Use KEY=value format.', {arg}),
);
}
const key = arg.slice(0, eqIndex);
const value = arg.slice(eqIndex + 1);
if (!key) {
this.error(t('commands.mrt.env.var.set.emptyKey', 'Empty key in: {{arg}}', {arg}));
}
variables[key] = value;
keys.push(key);
}
if (keys.length === 0) {
this.error(t('commands.mrt.env.var.set.noVariables', 'No environment variables provided. Use KEY=value format.'));
}
await setEnvVars(
{
projectSlug: project,
environment,
variables,
origin: this.resolvedConfig.mrtOrigin,
},
this.getMrtAuth(),
);
if (keys.length === 1) {
this.log(
t('commands.mrt.env.var.set.successSingle', 'Set {{key}} on {{project}}/{{environment}}', {
key: keys[0],
project,
environment,
}),
);
} else {
this.log(
t('commands.mrt.env.var.set.successMultiple', 'Set {{count}} variables on {{project}}/{{environment}}', {
count: keys.length,
project,
environment,
}),
);
}
return {variables, project, environment};
}
}