forked from redhat-developer/vscode-openshift-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.ts
More file actions
88 lines (75 loc) · 2.84 KB
/
cli.ts
File metadata and controls
88 lines (75 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*-----------------------------------------------------------------------------------------------
* Copyright (c) Red Hat, Inc. All rights reserved.
* Licensed under the MIT License. See LICENSE file in the project root for license information.
*-----------------------------------------------------------------------------------------------*/
import * as childProcess from 'child_process';
import * as vscode from 'vscode';
import { ExecException, ExecOptions } from 'child_process';
export interface CliExitData {
readonly error: ExecException;
readonly stdout: string;
readonly stderr: string;
}
export class Cli implements ICli {
private static instance: Cli;
private odoChannel: OdoChannel = new OdoChannelImpl();
private constructor() {}
static getInstance(): Cli {
if (!Cli.instance) {
Cli.instance = new Cli();
}
return Cli.instance;
}
async showOutputChannel(): Promise<void> {
this.odoChannel.show();
}
async execute(cmd: string, opts: ExecOptions = {}): Promise<CliExitData> {
return new Promise<CliExitData>(async (resolve, reject) => {
this.odoChannel.print(cmd);
if (opts.maxBuffer === undefined) {
opts.maxBuffer = 2*1024*1024;
}
childProcess.exec(cmd, opts, (error: ExecException, stdout: string, stderr: string) => {
const stdoutFiltered = stdout.replace(/---[\s\S]*$/g, '').trim();
this.odoChannel.print(stdoutFiltered);
this.odoChannel.print(stderr);
// do not reject it here, because caller in some cases need the error and the streams
// to make a decision
// Filter update message text which starts with `---`
resolve({ error, stdout: stdoutFiltered, stderr });
});
});
}
}
export interface ICli {
execute(cmd: string, opts?: ExecOptions): Promise<CliExitData>;
}
export interface OdoChannel {
print(text: string): void;
show(): void;
}
class OdoChannelImpl implements OdoChannel {
private readonly channel: vscode.OutputChannel = vscode.window.createOutputChannel("OpenShift");
show(): void {
this.channel.show();
}
prettifyJson(str: string) {
let jsonData: string;
try {
jsonData = JSON.stringify(JSON.parse(str), null, 2);
} catch (ignore) {
return str;
}
return jsonData;
}
print(text: string): void {
const textData = this.prettifyJson(text);
this.channel.append(textData);
if (textData.charAt(textData.length - 1) !== '\n') {
this.channel.append('\n');
}
if (vscode.workspace.getConfiguration('openshiftConnector').get<boolean>('showChannelOnOutput')) {
this.channel.show();
}
}
}