Skip to content

Commit a8dcd0d

Browse files
committed
feat: use Err type rather than a tuple
- No need override IO::MultiWriter IO type
1 parent 5fd442b commit a8dcd0d

File tree

4 files changed

+14
-22
lines changed

4 files changed

+14
-22
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.cr
13-
version: 0.2.1
13+
version: 0.2.2
1414
```
1515
1616
2. Run `shards install`

shard.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: exec
2-
version: 0.2.1
2+
version: 0.2.2
33

44
authors:
55
- initdc <initd@outlook.com>

spec/exec_spec.cr

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,9 @@ describe Exec do
3535
when String
3636
r.chomp.should eq "good"
3737
else
38-
o, e, s = r
39-
o.chomp.should eq "good"
40-
e.chomp.should eq "bad"
41-
s.exit_code.should eq 1
38+
r.stdout.chomp.should eq "good"
39+
r.stderr.chomp.should eq "bad"
40+
r.status.exit_code.should eq 1
4241
end
4342
end
4443

src/exec.cr

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,24 @@
11
class Exec < Process
2-
VERSION = "0.2.1"
2+
VERSION = "0.2.2"
33

4-
class IO
5-
class MultiWriter < ::IO::MultiWriter
6-
alias IO = ::IO | ::IO::FileDescriptor | ::String::Builder
7-
8-
def read(slice : Bytes) : NoReturn
9-
raise ::IO::Error.new("Can't read from IO::MultiWriter")
10-
end
11-
end
12-
end
4+
# https://github.com/crystal-lang/crystal/blob/1.19.1/src/compiler/crystal/macros/macros.cr#L68
5+
record Err, stdout : String, stderr : String, status : Process::Status
136

147
def self.run(command : String, args = nil, env : Env = nil, clear_env : Bool = false, shell : Bool = true,
15-
input : Stdio = Redirect::Inherit, output : Stdio = Redirect::Inherit, error : Stdio = Redirect::Inherit, chdir : Path | String? = nil) : String | {String, String, Process::Status}
8+
input : Stdio = Redirect::Inherit, output : Stdio = Redirect::Inherit, error : Stdio = Redirect::Inherit, chdir : Path | String? = nil) : String | Err
169
output_strio = String::Builder.new
1710
error_strio = String::Builder.new
1811

1912
output_writer = if output.is_a?(Redirect)
20-
output == Redirect::Close ? output : Exec::IO::MultiWriter.new(STDOUT, output_strio)
13+
output == Redirect::Close ? output : IO::MultiWriter.new(STDOUT, output_strio)
2114
else
22-
output != STDOUT ? Exec::IO::MultiWriter.new(STDOUT, output, output_strio) : Exec::IO::MultiWriter.new(STDOUT, output_strio)
15+
output != STDOUT ? IO::MultiWriter.new(STDOUT, output, output_strio) : IO::MultiWriter.new(STDOUT, output_strio)
2316
end
2417

2518
error_writer = if error.is_a?(Redirect)
26-
error == Redirect::Close ? error : Exec::IO::MultiWriter.new(STDERR, error_strio)
19+
error == Redirect::Close ? error : IO::MultiWriter.new(STDERR, error_strio)
2720
else
28-
error != STDERR ? Exec::IO::MultiWriter.new(STDERR, error, error_strio) : Exec::IO::MultiWriter.new(STDERR, error_strio)
21+
error != STDERR ? IO::MultiWriter.new(STDERR, error, error_strio) : IO::MultiWriter.new(STDERR, error_strio)
2922
end
3023

3124
status = new(command, args, env, clear_env, shell, input, output_writer, error_writer, chdir).wait
@@ -40,7 +33,7 @@ class Exec < Process
4033
when true
4134
output_strio.to_s
4235
else
43-
{output_strio.to_s, error_strio.to_s, status}
36+
Err.new(output_strio.to_s, error_strio.to_s, status)
4437
end
4538
end
4639

0 commit comments

Comments
 (0)