Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit 807c9c7

Browse files
authored
Merge pull request #2 from che-incubator/fixUrlParse
Fix server url parse error
2 parents f940703 + 493aa79 commit 807c9c7

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

src/che-openshift-authentication.ts

+22-10
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,17 @@ export async function start(context: theia.PluginContext) {
5757
}
5858

5959
async function ocLogIn(): Promise<void> {
60+
const errorMessage = 'Failed to authenticated the OpenShift connector plugin: ';
6061
let error = '';
61-
const server = await getServerUrl();
62-
const token = await che.openshift.getToken();
62+
let server = '';
63+
let token = '';
64+
try {
65+
server = await getServerUrl();
66+
token = await che.openshift.getToken();
67+
} catch (e) {
68+
theia.window.showErrorMessage(errorMessage + e);
69+
return;
70+
}
6371
const osCommand = spawn('oc', ['login', server, '--certificate-authority=/var/run/secrets/kubernetes.io/serviceaccount/ca.crt', '--token', token]);
6472
osCommand.stderr.on('data', data => {
6573
error += data
@@ -72,26 +80,30 @@ export async function start(context: theia.PluginContext) {
7280
}
7381
theia.window.showInformationMessage('OpenShift connector plugin is successfully authenticated');
7482
} else {
75-
theia.window.showErrorMessage('Failed to authenticated the OpenShift connector plugin: ' + error);
83+
theia.window.showErrorMessage(errorMessage + error);
7684
}
7785
});
7886
}
7987

8088
function getServerUrl(): Promise<string> {
81-
return new Promise<string>(resolve => {
89+
return new Promise<string>((resolve, reject) => {
8290
let result = '';
8391
const versionCommand = spawn('odo', ['version']);
8492
// tslint:disable-next-line:no-any
85-
versionCommand.stdout.on('data', (data: any) => {
93+
versionCommand.stdout.on('data', data => {
8694
result += data.toString();
8795
});
8896
// tslint:disable-next-line:no-any
89-
versionCommand.stderr.on('data', (data: any) => {
90-
resolve('');
97+
versionCommand.stderr.on('data', data => {
98+
reject(data)
9199
});
92-
versionCommand.on('close', (code: number | null) => {
93-
const server: string = result.substring(result.indexOf('Server: ') + 8, result.indexOf('Kubernetes: ') - 1);
94-
resolve(server);
100+
versionCommand.on('close', () => {
101+
const match = result.match(/https?:\/\/(www.)?[-a-zA-Z0-9.[a-z]([-a-zA-Z0-9@:%_+.~#?&/=]*)/g);
102+
if (match && match.length === 1) {
103+
resolve(match[0]);
104+
} else {
105+
reject('Failed to get the server url');
106+
}
95107
});
96108
})
97109
}

0 commit comments

Comments
 (0)