Skip to content

Commit 92b385c

Browse files
committed
feat: Exec.run return Exec::Status type
- Exec::Status has getter stdout stderr
1 parent 90d4d26 commit 92b385c

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
dependencies:
1111
exec:
1212
github: initdc/exec
13-
version: 0.0.1
13+
version: 0.1.2
1414
```
1515
1616
2. Run `shards install`

spec/exec_spec.cr

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ describe Exec do
55
Exec.run("sudo apt update")
66
end
77

8+
it "not print lines" do
9+
r = Exec.run("sudo apt update", output: File.open(File::NULL, "w"))
10+
r.stdout.size.should_not eq 0
11+
end
12+
813
it "return 0" do
914
Exec.code("uname").should eq 0
1015
end

src/exec.cr

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,36 @@
11
class Exec < Process
2-
VERSION = "0.1.1"
2+
VERSION = "0.1.2"
3+
4+
class Status < ::Process::Status
5+
getter stdout : String
6+
getter stderr : String
7+
8+
{% if flag?(:win32) %}
9+
# :nodoc:
10+
def initialize(@exit_status : UInt32, @stdout : String, @stderr : String)
11+
end
12+
{% else %}
13+
# :nodoc:
14+
def initialize(@exit_status : Int32, @stdout : String, @stderr : String)
15+
end
16+
{% end %}
17+
end
318

419
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
20+
input : IO = STDIN, output : IO = STDOUT, error : IO = STDERR, chdir : Path | String? = nil) : Status
21+
output_strio = String::Builder.new
22+
error_strio = String::Builder.new
23+
output_writer = IO::MultiWriter.new(output, output_strio)
24+
error_writer = IO::MultiWriter.new(error, error_strio)
25+
26+
status = new(command, args, env, clear_env, shell, input, output_writer, error_writer, chdir).wait
727
$? = status
8-
status
28+
29+
output_writer.close
30+
error_writer.close
31+
output.close unless output.in?(STDOUT, STDERR)
32+
error.close unless error.in?(STDOUT, STDERR)
33+
Status.new(status.@exit_status, output_strio.to_s, error_strio.to_s)
934
end
1035

1136
def self.code(command : String, args = nil, env : Env = nil, clear_env : Bool = false, shell : Bool = true,

0 commit comments

Comments
 (0)