Skip to content

Commit

Permalink
Merge pull request #698 from particle-iot/feature/sc-123399/delete-an…
Browse files Browse the repository at this point in the history
…d-disable

Feature/sc 123399/delete and disable
  • Loading branch information
keeramis authored Dec 12, 2023
2 parents 6ec5118 + 4b027c9 commit fd4d874
Show file tree
Hide file tree
Showing 3 changed files with 274 additions and 21 deletions.
17 changes: 17 additions & 0 deletions src/cmd/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,23 @@ module.exports = class ParticleApi {
}));
}

deleteLogicFunction({ org, id }) {
return this._wrap(this.api.deleteLogicFunction({
org: org,
logicFunctionId: id,
auth: this.accessToken,
}));
}

updateLogicFunction({ org, id, logicFunctionData }) {
return this._wrap(this.api.updateLogicFunction({
org: org,
logicFunctionId: id,
logicFunction: logicFunctionData,
auth: this.accessToken
}));
}

_wrap(promise){
return Promise.resolve(promise)
.then(result => result.body || result)
Expand Down
104 changes: 89 additions & 15 deletions src/cmd/logic-function.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module.exports = class LogicFunctionsCommand extends CLICommandBase {
}

async list({ org }) {
await this._setOrg(org);
this._setOrg(org);

await this._getLogicFunctionList();

Expand Down Expand Up @@ -54,7 +54,7 @@ module.exports = class LogicFunctionsCommand extends CLICommandBase {
}

async get({ org, name, id }) {
await this._setOrg(org);
this._setOrg(org);

await this._getLogicFunctionList();

Expand Down Expand Up @@ -85,7 +85,7 @@ module.exports = class LogicFunctionsCommand extends CLICommandBase {

async create({ org, name, params : { filepath } } = { params: { } }) {

await this._setOrg(org);
this._setOrg(org);

await this._getLogicFunctionList();

Expand Down Expand Up @@ -165,7 +165,7 @@ module.exports = class LogicFunctionsCommand extends CLICommandBase {

// Prompts the user to overwrite if any files exist
// If user says no, we exit the process
async _validatePaths({ dirPath, jsonPath, jsPath }) {
async _validatePaths({ dirPath, jsonPath, jsPath, _exit = () => process.exit(0) }) {
let exists = false;
const pathsToCheck = [dirPath, jsonPath, jsPath];
for (const p of pathsToCheck) {
Expand All @@ -176,13 +176,14 @@ module.exports = class LogicFunctionsCommand extends CLICommandBase {

if (exists) {
const overwrite = await this._promptOverwrite({
message: 'This Logic Function was previously downloaded. Overwrite?',
message: 'This Logic Function was previously downloaded locally. Overwrite?',
});
if (!overwrite) {
this.ui.stdout.write(`Aborted.${os.EOL}`);
process.exit(0);
_exit();
}
}
return exists;
}

// Prompts the user to overwrite if any files exist
Expand Down Expand Up @@ -334,21 +335,88 @@ module.exports = class LogicFunctionsCommand extends CLICommandBase {
}

async disable({ org, name, id }) {
// TODO
console.log(org, name, id);
this._setOrg(org);

await this._getLogicFunctionList();

({ name, id } = await this._getLogicFunctionIdAndName(name, id));

let logicFunctionJson = await this._getLogicFunctionData(id);
logicFunctionJson.logic_function.enabled = false;

try {
await this.api.updateLogicFunction({ org, id, logicFunctionData: logicFunctionJson.logic_function });
this._printDisableOutput(name, id);
} catch (err) {
throw new Error(`Error disabling Logic Function ${name}: ${err.message}`);
}

// Overwrite logic function if found locally since it is now disabled
await this._overwriteIfLFExistsLocally(name, id);
}

async _overwriteIfLFExistsLocally(name, id) {
const { dirPath, jsonPath, jsPath } = this._getLocalLFPathNames(name);

const exist = await this._validatePaths({ dirPath, jsonPath, jsPath });

if (!exist) {
return;
}

const logicFunctionData = await this._getLogicFunctionData(id);

const { logicFunctionConfigData, logicFunctionCode } = this._serializeLogicFunction(logicFunctionData);

const { genDirPath, genJsonPath, genJsPath } = await this._generateFiles({ logicFunctionConfigData, logicFunctionCode, name });

this._printDisableNewFilesOutput({ dirPath: genDirPath, jsonPath: genJsonPath, jsPath: genJsPath });
}

_printDisableOutput(name, id) {
this.ui.stdout.write(`Logic Function ${name}(${id}) is now disabled.${os.EOL}`);
}

_printDisableNewFilesOutput({ dirPath, jsonPath, jsPath }) {
this.ui.stdout.write(`${os.EOL}`);
this.ui.stdout.write(`The following files were overwritten after disabling the Logic Function:${os.EOL}`);
this.ui.stdout.write(` - ${path.basename(dirPath) + '/' + path.basename(jsonPath)}${os.EOL}`);
this.ui.stdout.write(` - ${path.basename(dirPath) + '/' + path.basename(jsPath)}${os.EOL}`);
this.ui.stdout.write(`${os.EOL}`);
}

async delete({ org, name, id }) {
// TODO
console.log(org, name, id);
this._setOrg(org);

await this._getLogicFunctionList();

({ name, id } = await this._getLogicFunctionIdAndName(name, id));

const confirm = await this._prompt({
type: 'confirm',
name: 'delete',
message: `Are you sure you want to delete Logic Function ${name}? This action cannot be undone.`,
choices: Boolean
});

if (confirm.delete) {
try {
await this.api.deleteLogicFunction({ org: this.org, id });
this.ui.stdout.write(`Logic Function ${name}(${id}) has been successfully deleted.${os.EOL}`);
} catch (err) {
throw new Error(`Error deleting Logic Function ${name}: ${err.message}`);
}
} else {
this.ui.stdout.write(`Aborted.${os.EOL}`);
}
}

async logs({ org, name, id, saveTo }) {
// TODO
console.log(org, name, id, saveTo);
}

async _setOrg(org) {
_setOrg(org) {
if (this.org === null) {
this.org = org;
}
Expand Down Expand Up @@ -383,10 +451,7 @@ module.exports = class LogicFunctionsCommand extends CLICommandBase {
}

async _generateFiles({ logicFunctionConfigData, logicFunctionCode, name }) {
const slugName = slugify(name);
const dirPath = path.join(process.cwd(), `${slugName}`);
const jsonPath = path.join(dirPath, `${slugName}.logic.json`);
const jsPath = path.join(dirPath, `${slugName}.js`);
const { dirPath, jsonPath, jsPath } = this._getLocalLFPathNames(name);

await this._validatePaths({ dirPath, jsonPath, jsPath });

Expand All @@ -397,6 +462,15 @@ module.exports = class LogicFunctionsCommand extends CLICommandBase {
return { dirPath, jsonPath, jsPath };
}

_getLocalLFPathNames(name) {
const slugName = slugify(name);
const dirPath = path.join(process.cwd(), `${slugName}`);
const jsonPath = path.join(dirPath, `${slugName}.logic.json`);
const jsPath = path.join(dirPath, `${slugName}.js`);

return { dirPath, jsonPath, jsPath };
}

async _getLogicFunctionIdAndName(name, id) {
if (!id && !name) {
name = await this._selectLogicFunction(this.logicFuncList);
Expand Down
Loading

0 comments on commit fd4d874

Please sign in to comment.