question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

allow Vorpal to run arbitrary bash commands and pipe back to shell

See original GitHub issue

My Vorpal options are like so:

  Commands:

    help [command...]  Provides help for a given command.
    exit               Exits application.
    pwd                echo present working directory
    run [file]         run a single test script
    find [options]     find test files to run

I’d like to be able to type a command - if it’s not a recognized option - to send it to bash and have bash interpret it.

One thing I could do is create a new option

bash [command...]

and then I could send all the arguments to bash that way…

however that requires users to type in something like:

> bash "ls -a"

or

> bash "ps aux | grep node"

like I said, it would be cool to be able to give Vorpal the option of sending unrecognized commands to bash and then piping the results back to vorpal terminal.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:15 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
uri-chandlercommented, Jan 27, 2018

@ORESoftware Hmm… maybe I’m missing something - but I think exec is the one to use if we don’t want to parse the string, since the signature for spawn requires the first param to be the command-to-run, and then an array of arguments to pass into that command.

For example: Command: ls -a "dog bear cat" | grep "sally road"

Using spawn: spawn(ls, ['-a', '"dog bear cat"', '|', 'grep', '"sally road"'])

Using exec: exec('ls -a "dog bear cat" | grep "sally road"')

Plus, spawn requires us to read from stdout and\or stderr to get the result, where as exec will provide the results to the callback - so maybe it’s a better fit in this specific use case?

Back to the raw-string issue Yes, a full solution will need to keep track of control-characters - such as line-feed, carriage-return, backspace, cursor-moves etc… - BUT - it’s not that hard to implement - definitely doable (perhaps I’ll have time for that next weekend).

Also, we could publish it as a vorpal extension \ plugin - something like vorpal-shell-fallback. This will be a cleaner way to use, and will allow anyone to add it just by doing vorpal.use('vorpal-shell-fallback')

0reactions
jeremygiberson-at-privorocommented, Feb 15, 2021

Here is an updated version that works well. It gets the raw input from the user and passes it to the shell.

const {spawn} = require('child_process');

const passthru = function(args, done){
  let vorpal = this;
  // get the unparsed raw value typed in to the prompt
  let raw = vorpal.commandWrapper.command; // not an official API method but works until a significant change to the lib

  // shell:true option https://stackoverflow.com/questions/23487363/how-can-i-parse-a-string-into-appropriate-arguments-for-child-process-spawn
  let passthru_process = spawn(raw, [], {shell:true});
  // using vorpal.log instead of process.stdout because I want to be able to pipe output to other vorpal commands
  passthru_process.stdout.on('data', buffer => {
    vorpal.log(buffer.toString('utf8'))
  })
  // perhaps this could go to vorpal.log as well, your use may vary
  passthru_process.stderr.pipe(process.stderr);

  passthru_process.on('close', done)
}



  vorpal
    .catch('[cmd...]')
    .action(passthru)
    .allowUnknownOptions(true)

Seems to handle opts and args gracefully:

tool$ ls -laR ./logs
total 16
drwxr-xr-x   4 me  staff   128 Feb 13 14:35 .
drwxr-xr-x@ 40 me  staff  1280 Feb 12 13:30 ..
-rw-r--r--   1 me  staff  2027 Feb 13 14:35 AppRegistration.mocha.log
-rw-r--r--   1 me  staff  2145 Feb 13 14:35 AppRegistration.mocha.server.log
Read more comments on GitHub >

github_iconTop Results From Across the Web

bash - Execute a command once per line of piped input?
The accepted answer has the right idea, but the key is to pass xargs the -n1 switch, which means "Use at most 1...
Read more >
vorpal - Bountysource
I'm emulating a virtual real-world device that does a thing on an interval. When I ctrl-c ctrl-c to exit that device, that interval...
Read more >
How to pipe multiple commands into a single command in the ...
Use parentheses ()'s to combine the commands into a single process, which will concatenate the stdout of each of them. Example 1 (note...
Read more >
Jari's Procmail Tips Page - Howtoforge
8.7 How to run an extra shell command as a side effect? ... Procmail manual pages exists primarily on Unix/Linux platform, If You're...
Read more >
Awkward – A Node.js-based terminal emulator | Hacker News
(you could of course alias those to make 'pidof node' like Linux ... use case (ps -ef printing a nice text table) and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found