Skip to content

Commit 69ae424

Browse files
committed
feat: add exec lib for crystal
0 parents  commit 69ae424

File tree

8 files changed

+172
-0
lines changed

8 files changed

+172
-0
lines changed

.editorconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
root = true
2+
3+
[*.cr]
4+
charset = utf-8
5+
end_of_line = lf
6+
insert_final_newline = true
7+
indent_style = space
8+
indent_size = 2
9+
trim_trailing_whitespace = true

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/docs/
2+
/lib/
3+
/bin/
4+
/.shards/
5+
*.dwarf
6+
7+
# Libraries don't need dependency lock
8+
# Dependencies will be locked in applications that use them
9+
/shard.lock

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 initdc <initd@outlook.com>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# exec
2+
3+
> Subclass for `Process`
4+
5+
## Installation
6+
7+
1. Add the dependency to your `shard.yml`:
8+
9+
```yaml
10+
dependencies:
11+
exec:
12+
github: initdc/exec
13+
version: 0.0.1
14+
```
15+
16+
2. Run `shards install`
17+
18+
## Usage
19+
20+
```crystal
21+
require "exec"
22+
23+
Exec.run("sudo apt update")
24+
p Exec.code("unamea")
25+
p Exec.output("uname")
26+
```
27+
28+
## Contributing
29+
30+
1. Fork it (<https://github.com/initdc/exec/fork>)
31+
2. Create your feature branch (`git checkout -b my-new-feature`)
32+
3. Commit your changes (`git commit -am 'Add some feature'`)
33+
4. Push to the branch (`git push origin my-new-feature`)
34+
5. Create a new Pull Request
35+
36+
## Contributors
37+
38+
- [initdc](https://github.com/initdc) - creator and maintainer

shard.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name: exec
2+
version: 0.1.0
3+
4+
authors:
5+
- initdc <initd@outlook.com>
6+
7+
crystal: ">= 1.0.0, < 2.0.0"
8+
9+
license: MIT

spec/exec_spec.cr

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
require "./spec_helper"
2+
3+
describe Exec do
4+
it "print lines" do
5+
Exec.run("sudo apt update")
6+
end
7+
8+
it "return 0" do
9+
Exec.code("uname").should eq 0
10+
end
11+
12+
it "accept multi string" do
13+
Exec.code("uname -s").should eq 0
14+
end
15+
16+
it "return 127" do
17+
Exec.code("unamea").should eq 127
18+
end
19+
20+
it "get Linux" do
21+
Exec.output("uname").should eq "Linux"
22+
end
23+
24+
it "log to file" do
25+
tempfile = File.tempfile("test_", ".log")
26+
Exec.run("uname", output: File.open(tempfile.path, "a+"))
27+
28+
tempfile.gets_to_end.should eq "Linux\n"
29+
tempfile.delete
30+
end
31+
32+
it "list file" do
33+
Exec.each_line("ls spec").should eq ["exec_spec.cr\n", "spec_helper.cr\n"]
34+
end
35+
36+
it "list file with option" do
37+
Exec.each_line("ls spec", chomp: true).should eq ["exec_spec.cr", "spec_helper.cr"]
38+
end
39+
end

spec/spec_helper.cr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require "spec"
2+
require "../src/exec"

src/exec.cr

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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

Comments
 (0)