I am trying to process the output of a specific function that uses process.stdout / process.stdin to print the commands results to the terminal. To be more specific, this Kubernetes function https://github.com/kubernetes-client/javascript/blob/master/src/exec.ts with this usage:
const exec = new k8s.Exec(kc);
exec.exec('default', 'nginx-4217019353-9gl4s', 'nginx', command,
process.stdout, process.stderr, process.stdin,
true /* tty */,
(status) => {
console.log('Exited with status:');
console.log(JSON.stringify(status, null, 2));
});
While the function above may print something like that to the terminal:
Everything is up.
Time running: 5min 23sec.
Exited with status:
{
"metadata": {},
"status": "Success"
}
My goal is to capture
Everything is up.
Time running: 5min 23sec.
in a variable so that I can process it further.
Creating an own stream object instead of using the one from process / console works good.
var Stream = require('stream');
var ws = new Stream;
ws.writable = true;
ws.str = "";
ws.str = 0;
ws.string = function(){
return ws.str;
}
ws.clearStr = function(){
ws.str = "";
}
ws.write = function(buf) {
ws.str += buf.toString();
ws.bytes += buf.length;
}
ws.end = function(buf) {
if(arguments.length) ws.write(buf);
ws.writable = false;
}
async function kubeExec(ns, pod, container, cmd){
ws.clearStr();
ret = new Promise((resolve,reject) =>{
execV1Api.exec(ns,pod,container,cmd, ws, process.stderr , process.stdin,true /* tty */,
async function(ret){
if(ret.status == "Success"){
resolve(ws.str);
} else {
console.log(JSON.stringify(status, null, 2));
}
});
});
return ret.then( (str) => { return str;} );
}