|
| 1 | +class Exec < Process |
| 2 | + VERSION = "0.1.0" |
| 3 | + |
| 4 | + def self.run(command : String, args = nil, env : Env = nil, clear_env : Bool = false, shell : Bool = true, |
| 5 | + input : Stdio = Redirect::Inherit, output : Stdio = Redirect::Inherit, error : Stdio = Redirect::Inherit, chdir : Path | String? = nil) : Process::Status |
| 6 | + status = new(command, args, env, clear_env, shell, input, output, error, chdir).wait |
| 7 | + $? = status |
| 8 | + status |
| 9 | + end |
| 10 | + |
| 11 | + def self.code(command : String, args = nil, env : Env = nil, clear_env : Bool = false, shell : Bool = true, |
| 12 | + input : Stdio = Redirect::Inherit, output : Stdio = Redirect::Inherit, error : Stdio = Redirect::Inherit, chdir : Path | String? = nil) : Int32 |
| 13 | + status = new(command, args, env, clear_env, shell, input, output, error, chdir).wait |
| 14 | + $? = status |
| 15 | + status.exit_code |
| 16 | + rescue File::NotFoundError |
| 17 | + 127 |
| 18 | + end |
| 19 | + |
| 20 | + def self.output(command) : String |
| 21 | + process = new(command, shell: true, input: Redirect::Inherit, output: Redirect::Pipe, error: Redirect::Inherit) |
| 22 | + output = process.output.gets_to_end |
| 23 | + status = process.wait |
| 24 | + $? = status |
| 25 | + output.chomp |
| 26 | + end |
| 27 | + |
| 28 | + def self.each_line(command, chomp = false, &block : String ->) : Nil |
| 29 | + process = new(command, shell: true, input: Redirect::Inherit, output: Redirect::Pipe, error: Redirect::Inherit) |
| 30 | + output = process.output.gets_to_end |
| 31 | + status = process.wait |
| 32 | + $? = status |
| 33 | + output.each_line(chomp) do |line| |
| 34 | + yield line |
| 35 | + end |
| 36 | + end |
| 37 | + |
| 38 | + def self.each_line(command, chomp = false) |
| 39 | + process = new(command, shell: true, input: Redirect::Inherit, output: Redirect::Pipe, error: Redirect::Inherit) |
| 40 | + output = process.output.gets_to_end |
| 41 | + status = process.wait |
| 42 | + $? = status |
| 43 | + output.each_line(chomp).to_a |
| 44 | + end |
| 45 | +end |
0 commit comments