Skip to content

Commit

Permalink
Merge pull request #60 from zapier/logout-command
Browse files Browse the repository at this point in the history
BREAKING CHANGE(cli): Port Logout Command
  • Loading branch information
xavdid authored Aug 30, 2019
2 parents 38dcc79 + 50feea2 commit 43044f1
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 68 deletions.
1 change: 0 additions & 1 deletion packages/cli/src/commands/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ module.exports = {
history: require('./history'),
invite: require('./invite'),
link: require('./link'),
logout: require('./logout'),
logs: require('./logs'),
migrate: require('./migrate'),
promote: require('./promote'),
Expand Down
52 changes: 0 additions & 52 deletions packages/cli/src/commands/logout.js

This file was deleted.

2 changes: 1 addition & 1 deletion packages/cli/src/oclif/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ Some notes to help an ongoing project
- [ ] history
- [ ] invitees
- [ ] link
- [x] logout
- [x] login
- [ ] logout
- [ ] logs
- [ ] migrate
- [ ] migrate
Expand Down
39 changes: 39 additions & 0 deletions packages/cli/src/oclif/commands/logout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const BaseCommand = require('../ZapierBaseCommand');
const { buildFlags } = require('../buildFlags');

const { callAPI } = require('../../utils/api');
const { deleteFile } = require('../../utils/files');
const { AUTH_LOCATION, AUTH_LOCATION_RAW } = require('../../constants');

class LogoutCommand extends BaseCommand {
async perform() {
let success = true;
this.log(
'Preparing to deactivate local deploy key and reset local configs.'
);
this.startSpinner('Deactivating local deploy key');
try {
await callAPI('/keys', { method: 'DELETE', body: { single: true } });
} catch (e) {
success = false;
this.error(
`Deletion API request failed. Is your ${AUTH_LOCATION} already empty or invalid? If so, feel free to ignore this error.`
);
} finally {
this.stopSpinner({ success });
}

this.startSpinner(`Destroying \`${AUTH_LOCATION}\``);
const deletedFileResult = deleteFile(AUTH_LOCATION);
this.debug(`file deletion success?: ${deletedFileResult}`);
this.stopSpinner();

this.log('The active deploy key was deactivated');
}
}

LogoutCommand.flags = buildFlags();
LogoutCommand.examples = ['zapier logout'];
LogoutCommand.description = `Deactivates your acive deploy key and resets \`${AUTH_LOCATION_RAW}\`.`;

module.exports = LogoutCommand;
1 change: 1 addition & 0 deletions packages/cli/src/oclif/oCommands.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

module.exports = {
init: require('./commands/init'),
logout: require('./commands/logout'),
login: require('./commands/login'),
versions: require('./commands/versions')
};
13 changes: 9 additions & 4 deletions packages/cli/src/utils/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,15 @@ const callAPI = (route, options, rawError = false) => {
debug(`>> ${requestOptions.method} ${requestOptions.url}`);
if (requestOptions.body) {
const replacementStr = 'raw zip removed in logs';
const cleanedBody = _.assign({}, JSON.parse(requestOptions.body), {
zip_file: replacementStr,
source_zip_file: replacementStr
});
const requestBody = JSON.parse(requestOptions.body);
const cleanedBody = {};
for (const k in requestBody) {
if (k.includes('zip_file')) {
cleanedBody[k] = replacementStr;
} else {
cleanedBody[k] = requestBody[k];
}
}
debug(`>> ${JSON.stringify(cleanedBody)}`);
}
debug(`<< ${res.status}`);
Expand Down
18 changes: 8 additions & 10 deletions packages/cli/src/utils/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,14 @@ const writeFile = (fileName, data) => {
return fse.writeFile(fixHome(fileName), data);
};

// Returns a promise that deletes the file.
const deleteFile = fileName => {
return new Promise(resolve => {
try {
fse.unlinkSync(fileName);
} catch (err) {
resolve();
}
resolve();
});
// deletes a file, eats the error
const deleteFile = path => {
try {
fse.unlinkSync(path);
return true;
} catch (err) {
return false;
}
};

// Returns a promise that ensures a directory exists.
Expand Down

0 comments on commit 43044f1

Please sign in to comment.