CLITaskRunner
, (*1)
Run a CLI command in the background and get updates via callbacks on events including buffer and line updates. You can subscribe to any writable stream and get callbacks triggered for new lines or on each buffer update., (*2)
Example
$cmd = "mysqldump -uroot -proot -v dbname | mysql -uroot -proot dbname2";
$CLITR = new \Hambrook\CLITaskRunner\CLITaskRunner($cmd);
$CLITR->onLine(
"stdErr",
function($v) {
echo "\nCopying table structure: ".$v
},
"/\-\- Retrieving table.*? ([^ ]+)\.\.\./"
);
$CLITR->onLine(
"stdErr",
function() {
echo "\nCopying table rows..."
},
"/\-\- Retrieving rows/"
);
$CLITR->process();
Who is it for?
Probably mostly CLI application developers, but there will be usecases I haven't thought of., (*3)
Why use this?
I built this because I use mysqldump and rsync from within a CLI application and wanted to customise the output from those commands. It couldn't be done with a simple popen because mysqldump puts its verbose output into the stdErr stream instead of stdOut. Then, naturally, I made it as versatile as possible., (*4)
When you could use this?
When you need to run a CLI command and be notified when specific output occurs on a particular stream., (*5)
What is this not?
This library sits between your software and CLI commands that it runs. It does not sit between your software and the user. It doesn't do any user input management, argument management, output formatting, or anything of the sort. It helps your software get updates from long-ish running CLI commands., (*6)
Functions
__construct()
CLITaskRunner__construct(string$command), (*7)
Create a new instance with a command ready to run, (*8)
onLine()
boolonLine(string$stream, callable$callback,[bool|string$pattern=false]), (*9)
Add a callback for when a new line is received, with optional regex pattern., (*10)
onBuffer()
boolonBuffer(string$stream, callable$callback,[bool|string$pattern=false]), (*11)
Add a callback for when the buffer is received, with optional regex pattern., (*12)
onSuccess()
boolonSuccess(callable$callback), (*13)
Add a callback for when the command succeeds., (*14)
onFailure()
boolonFailure(callable$callback), (*15)
Add a callback for when the command fails., (*16)
onComplete()
boolonComplete(callable$callback), (*17)
Add a callback for when the process is complete. Gets called after onSuccess and onFailure., (*18)
bufferSize()
bool|intbufferSize([int$bufferSize=10]), (*19)
Gets or sets the buffer size. Buffer size is currently common across all streams., (*20)
streams()
boolstreams([array$streams=[]]), (*21)
Get or set the array of streams. Each stream will use its array key as the stream key when adding callbacks., (*22)
Testing
Tests coming soon., (*23)
Feedback
Tell me if you loved it. Tell me if you hated it. Tell me if you used it and thought "meh". I'm keen to hear your feedback., (*24)
Contributing
Feel free to fork this project and submit pull requests, or even just request features via the issue tracker. Please be descriptive with pull requests and match the existing code style., (*25)
Roadmap
- Add support for pushing to the stdIn stream
- Add type hinting for all functions
- Add documentation
- Add more examples
- Add more unit tests
- Add any other standard documentation that should be included
- If you have an idea, let me know.
License
Copyright © 2015 Rick Hambrook, (*26)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version., (*27)
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details., (*28)
You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses/., (*29)