Small simplistic helper class for executing shell commands in Crystal:
- run command,
- exit if it fails,
- return STDOUT of the command;
- allow to ignore failure
- return STDERR of failed command.
Add this to your application's
dependencies: shell: github: maiha/shell.cr version: 0.4.2
Output of the shell command is available through
methods, numeric value of the exit status returned by the function
Simplest usage of the
If command fails
Shell raises exception, but this behaviour can be
puts Shell.run("command_with_non_zero_status", fail_on_error: false).stderr
Shell::Seq accumulates shell commands and those results.
When an error occur, '#run' doesn't raise any errors and just skips latter commands.
This is similar to Try monad concatinations.
shell = Shell::Seq.new shell.run("mkdir /tmp/shell") if !Dir.exists?("/tmp/shell") shell.run("git clone https://github.com/maiha/shell.cr.git", chdir: "/tmp/shell") shell.run("ls /xxxxx") shell.run("date") shell.success? # => false shell.last.stderr # => "ls: cannot access '/xxxxx': No such file or directory\n" shell.map(&.cmd.split) # => ["mkdir", "git", "ls"] shell.log # shows `cmd`, `stdout`, `stderr` of all executed commands # % mkdir /tmp/shell # % git clone https://github.com/maiha/shell.cr.git # Cloning into 'shell.cr'... # % ls /xxxxx # ls: cannot access '/xxxxx': No such file or directory
See test for further usage: spec/seq_spec.cr
- Fork it ( https://github.com/dmytro/shell/fork )
- Create your feature branch (git checkout -b my-new-feature)
- Commit your changes (git commit -am 'Add some feature')
- Push to the branch (git push origin my-new-feature)
- Create a new Pull Request
- dmytro Dmytro Kovalov - creator, maintainer