Skip to content

Instantly share code, notes, and snippets.

@theycallmeloki
Last active June 3, 2022 12:00
Show Gist options
  • Save theycallmeloki/a602f69d343babbcbabb1f00bd8d4652 to your computer and use it in GitHub Desktop.
Save theycallmeloki/a602f69d343babbcbabb1f00bd8d4652 to your computer and use it in GitHub Desktop.
Quick way to use a CLI from a node express app
// the utility function to query pachyderm job completion data
const runPachctlCommand = (args: string[], callback: Function) => {
console.log('Running 🐘🐘🐘 Pachctl 🐘🐘🐘');
args.push('--raw');
args.push('|');
args.push('jq');
// might need to make this next line `-sr` depending on
// whether you want to slurp or not
args.push('-r');
args.push('.');
const line = args.join(' ');
// spawn a pachctl line
const child = spawn('sh', ['-c', `pachctl ${line}`]);
let scriptOutput = '';
child.stdout.setEncoding('utf8');
// Read pachctl output -> in json
child.stdout.on('data', function (data: any) {
data = data.toString();
scriptOutput += data;
});
child.stderr.setEncoding('utf8');
// Errors, if any
child.stderr.on('data', function (data: any) {
data = data.toString();
scriptOutput += data;
});
// Send the output back to the caller
child.on('close', function (code: number) {
// code is typically the exit code of the execution,
// should be 0 if everything went well
callback(scriptOutput, code);
});
};
// an example express GET request handler
app.get('/getEncodingJobStats', async (req, res) => {
runPachctlCommand(['list', 'job'], function (output: any, exitCode: any) {
res.send(output);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment