Skip to content

Commit

Permalink
fix: add chomp option for output method
Browse files Browse the repository at this point in the history
  • Loading branch information
initdc committed Mar 4, 2024
1 parent 69ae424 commit 565355a
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
2 changes: 1 addition & 1 deletion shard.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: exec
version: 0.1.0
version: 0.1.1

authors:
- initdc <[email protected]>
Expand Down
4 changes: 4 additions & 0 deletions spec/exec_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ describe Exec do
Exec.output("uname").should eq "Linux"
end

it "get Linux\n" do
Exec.output("uname", false).should eq "Linux\n"
end

it "log to file" do
tempfile = File.tempfile("test_", ".log")
Exec.run("uname", output: File.open(tempfile.path, "a+"))
Expand Down
7 changes: 4 additions & 3 deletions src/exec.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Exec < Process
VERSION = "0.1.0"
VERSION = "0.1.1"

def self.run(command : String, args = nil, env : Env = nil, clear_env : Bool = false, shell : Bool = true,
input : Stdio = Redirect::Inherit, output : Stdio = Redirect::Inherit, error : Stdio = Redirect::Inherit, chdir : Path | String? = nil) : Process::Status
Expand All @@ -17,12 +17,13 @@ class Exec < Process
127
end

def self.output(command) : String
def self.output(command, chomp = true) : String
process = new(command, shell: true, input: Redirect::Inherit, output: Redirect::Pipe, error: Redirect::Inherit)
output = process.output.gets_to_end
status = process.wait
$? = status
output.chomp
return output.chomp if chomp
output
end

def self.each_line(command, chomp = false, &block : String ->) : Nil
Expand Down

0 comments on commit 565355a

Please sign in to comment.