Skip to content

Commit 6a44bdf

Browse files
committed
feat(cli): Push function variables
1 parent e22f439 commit 6a44bdf

File tree

1 file changed

+41
-4
lines changed

1 file changed

+41
-4
lines changed

templates/cli/lib/commands/push.js.twig

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ const awaitPools = {
340340
},
341341
}
342342

343-
const approveChanges = async (resource, resourceGetFunction, keys, resourceName, resourcePlural) => {
343+
const approveChanges = async (resource, resourceGetFunction, keys, resourceName, resourcePlural, skipKeys = []) => {
344344
log('Checking for changes');
345345
const changes = [];
346346

@@ -352,6 +352,9 @@ const approveChanges = async (resource, resourceGetFunction, keys, resourceName,
352352
});
353353

354354
for (let [key, value] of Object.entries(whitelistKeys(remoteResource, keys))) {
355+
if (skipKeys.includes(key)) {
356+
continue;
357+
}
355358
if (Array.isArray(value) && Array.isArray(localResource[key])) {
356359
if (JSON.stringify(value) !== JSON.stringify(localResource[key])) {
357360
changes.push({
@@ -399,7 +402,7 @@ const getObjectChanges = (remote, local, index, what) => {
399402
if (remote[index] && local[index]) {
400403
for (let [service, status] of Object.entries(remote[index])) {
401404
if (status !== local[index][service]) {
402-
changes.push({ group: what,setting: service, remote: chalk.red(status), local: chalk.green(local[index][service]) })
405+
changes.push({ group: what, setting: service, remote: chalk.red(status), local: chalk.green(local[index][service]) })
403406
}
404407
}
405408
}
@@ -960,7 +963,7 @@ const pushSettings = async () => {
960963
}
961964
}
962965

963-
const pushFunction = async ({ functionId, async, code } = { returnOnZero: false }) => {
966+
const pushFunction = async ({ functionId, async, code, withVariables } = { returnOnZero: false }) => {
964967
const functionIds = [];
965968

966969
if (functionId) {
@@ -1009,7 +1012,7 @@ const pushFunction = async ({ functionId, async, code } = { returnOnZero: false
10091012
}
10101013
}
10111014

1012-
if (!(await approveChanges(functions, functionsGet, KeysFunction, 'functionId', 'functions'))) {
1015+
if (!(await approveChanges(functions, functionsGet, KeysFunction, 'functionId', 'functions', ['vars']))) {
10131016
return;
10141017
}
10151018

@@ -1100,6 +1103,39 @@ const pushFunction = async ({ functionId, async, code } = { returnOnZero: false
11001103
}
11011104
}
11021105

1106+
if (withVariables) {
1107+
updaterRow.update({ status: 'Updating variables' }).replaceSpinner(SPINNER_ARC);
1108+
1109+
const { variables } = await paginate(functionsListVariables, {
1110+
functionId: func['$id'],
1111+
parseOutput: false
1112+
}, 100, 'variables');
1113+
1114+
await Promise.all(variables.map(async variable => {
1115+
await functionsDeleteVariable({
1116+
functionId: func['$id'],
1117+
variableId: variable['$id'],
1118+
parseOutput: false
1119+
});
1120+
}));
1121+
1122+
let result = await awaitPools.wipeVariables(func['$id']);
1123+
if (!result) {
1124+
updaterRow.fail({ errorMessage: `Variable deletion timed out.` })
1125+
return;
1126+
}
1127+
1128+
// Deploy local variables
1129+
await Promise.all((func['vars'] ?? []).map(async variable => {
1130+
await functionsCreateVariable({
1131+
functionId: func['$id'],
1132+
key: variable['key'],
1133+
value: variable['value'],
1134+
parseOutput: false
1135+
});
1136+
}));
1137+
}
1138+
11031139
if (code === false) {
11041140
successfullyPushed++;
11051141
successfullyDeployed++;
@@ -1615,6 +1651,7 @@ push
16151651
.option(`-f, --function-id <function-id>`, `ID of function to run`)
16161652
.option(`-A, --async`, `Don't wait for functions deployments status`)
16171653
.option("--no-code", "Don't push the function's code")
1654+
.option("--with-variables", `Push function variables.`)
16181655
.action(actionRunner(pushFunction));
16191656

16201657
push

0 commit comments

Comments
 (0)