Skip to content

Add option to force logout when server is unreachable #77

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions cli/script/command-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ export function execute(command: cli.ICommand) {
return login(<cli.ILoginCommand>command);

case cli.CommandType.logout:
return logout(command);
return logout(<cli.ILogoutCommand>command);

case cli.CommandType.patch:
return patch(<cli.IPatchCommand>command);
Expand Down Expand Up @@ -626,7 +626,7 @@ function loginWithExternalAuthentication(action: string, serverUrl?: string): Pr
});
}

function logout(command: cli.ICommand): Promise<void> {
function logout(command: cli.ILogoutCommand): Promise<void> {
return Q(<void>null)
.then((): Promise<void> => {
if (!connectionInfo.preserveAccessKeyOnLogout) {
Expand All @@ -642,6 +642,15 @@ function logout(command: cli.ICommand): Promise<void> {
.then((): void => {
sdk = null;
deleteConnectionInfoCache();
})
.catch((error: any) => {
if (command.force) {
log(chalk.redBright("\nThere was an issue logging out from the server. Forcing logout by deleting local connection info.\n"));
deleteConnectionInfoCache();
log(chalk.yellowBright("Notice: Local session file was deleted, but the session ID might still exist on the server.\n"));
} else {
throw error;
}
});
}

Expand Down Expand Up @@ -1506,7 +1515,7 @@ function serializeConnectionInfo(accessKey: string, preserveAccessKeyOnLogout: b

log(
`\r\nSuccessfully logged-in. Your session file was written to ${chalk.cyan(configFilePath)}. You can run the ${chalk.cyan(
"code-push logout"
"code-push-standalone logout"
)} command at any time to delete this file and terminate your session.\r\n`
);
}
Expand Down
22 changes: 18 additions & 4 deletions cli/script/command-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as cli from "../script/types/cli";
import * as chalk from "chalk";
import backslash = require("backslash");
import parseDuration = require("parse-duration");
import AccountManager = require("./management-sdk");

const packageJson = require("../../package.json");
const ROLLOUT_PERCENTAGE_REGEX: RegExp = /^(100|[1-9][0-9]|[1-9])%?$/;
Expand Down Expand Up @@ -428,10 +429,13 @@ yargs
isValidCommandCategory = true;
isValidCommand = true;
yargs
.usage(USAGE_PREFIX + " login [options]")
.demand(/*count*/ 0, /*max*/ 1) //set 'max' to one to allow usage of serverUrl undocument parameter for testing
.example("login", "Logs in to the CodePush server")
.example("login --accessKey mykey", 'Logs in on behalf of the user who owns and created the access key "mykey"')
.usage(USAGE_PREFIX + " login <serverUrl> [options]")
.demand(/*count*/ 0, /*max*/ 1) //set 'max' to one to allow usage of serverUrl
.example("login", `Logs in to the CodePush server on default serverUrl ${AccountManager.SERVER_URL}`)
.example(
"login https://codepush.example.com --accessKey mykey",
'Logs in on behalf of the user who owns and created the access key "mykey", on the server running at https://codepush.example.com'
)
.option("accessKey", {
alias: "key",
default: null,
Expand All @@ -450,6 +454,13 @@ yargs
yargs
.usage(USAGE_PREFIX + " logout")
.demand(/*count*/ 0, /*max*/ 0)
.option("force", {
alias: "f",
default: null,
demand: false,
description: "Force logout, even if server is unreachable.",
type: "boolean",
})
.example("logout", "Logs out and ends your current session");
addCommonConfiguration(yargs);
})
Expand Down Expand Up @@ -1142,6 +1153,9 @@ export function createCommand(): cli.ICommand {

case "logout":
cmd = { type: cli.CommandType.logout };

const logoutCommand = <cli.ILogoutCommand>cmd;
logoutCommand.force = argv["force"] as boolean;
break;

case "patch":
Expand Down
4 changes: 4 additions & 0 deletions cli/script/types/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ export interface ILoginCommand extends ICommand {
accessKey: string;
}

export interface ILogoutCommand extends ICommand {
force: boolean
}

export interface IPackageInfo {
description?: string;
label?: string;
Expand Down