ruby-sh 2.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.overcommit.yml +26 -0
- data/.rspec +3 -0
- data/.rubocop +0 -0
- data/.rubocop.yml +10 -0
- data/.yardopts +2 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +10 -0
- data/Gemfile.lock +62 -0
- data/LICENSE.txt +21 -0
- data/README.md +383 -0
- data/Rakefile +12 -0
- data/lib/rubsh/argument.rb +47 -0
- data/lib/rubsh/command.rb +86 -0
- data/lib/rubsh/exceptions.rb +22 -0
- data/lib/rubsh/option.rb +73 -0
- data/lib/rubsh/running_command.rb +337 -0
- data/lib/rubsh/running_pipeline.rb +243 -0
- data/lib/rubsh/shell/env.rb +15 -0
- data/lib/rubsh/shell.rb +24 -0
- data/lib/rubsh/stream_reader.rb +39 -0
- data/lib/rubsh/version.rb +5 -0
- data/lib/rubsh.rb +12 -0
- data/rubsh.gemspec +38 -0
- data/sig/rubsh.rbs +4 -0
- metadata +73 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 91cf2506446079b3fe817363924b3c6449013525842e65458a29f4c0b7c35cb3
|
4
|
+
data.tar.gz: 4c7bf3f948195bfe2d64e9e368a3471dfa4b7eb43396c58775851b8c3b827516
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b5e584c74dc57d72549c232120997eac68237afb27281e6172ba0377d337ce826b1056a4735737807a985865a89aa450abefc79044a8d3df0794e0e4f6476ce4
|
7
|
+
data.tar.gz: 44d52e9f7484e79fae0c1502fdfa67c3571b2e75fb9bb3b9c66200136586927c4378cb9f4c55ea6605498154cfc3d1be9c59caba8ad0f054acbd59de2d8e6466
|
data/.overcommit.yml
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# Use this file to configure the Overcommit hooks you wish to use. This will
|
2
|
+
# extend the default configuration defined in:
|
3
|
+
# https://github.com/sds/overcommit/blob/master/config/default.yml
|
4
|
+
#
|
5
|
+
# At the topmost level of this YAML file is a key representing type of hook
|
6
|
+
# being run (e.g. pre-commit, commit-msg, etc.). Within each type you can
|
7
|
+
# customize each hook, such as whether to only run it on certain files (via
|
8
|
+
# `include`), whether to only display output if it fails (via `quiet`), etc.
|
9
|
+
#
|
10
|
+
# For a complete list of hooks, see:
|
11
|
+
# https://github.com/sds/overcommit/tree/master/lib/overcommit/hook
|
12
|
+
#
|
13
|
+
# For a complete list of options that you can use to customize hooks, see:
|
14
|
+
# https://github.com/sds/overcommit#configuration
|
15
|
+
#
|
16
|
+
# Uncomment the following lines to make the configuration take effect.
|
17
|
+
|
18
|
+
PreCommit:
|
19
|
+
RuboCop:
|
20
|
+
enabled: true
|
21
|
+
command: 'bin/rubocop'
|
22
|
+
|
23
|
+
PrePush:
|
24
|
+
RSpec:
|
25
|
+
enabled: true
|
26
|
+
command: 'bin/rspec'
|
data/.rspec
ADDED
data/.rubocop
ADDED
File without changes
|
data/.rubocop.yml
ADDED
data/.yardopts
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at johndoe@example.com. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
ruby-sh (2.1.4)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
ast (2.4.2)
|
10
|
+
diff-lcs (1.5.0)
|
11
|
+
parallel (1.22.1)
|
12
|
+
parser (3.1.2.0)
|
13
|
+
ast (~> 2.4.1)
|
14
|
+
rainbow (3.1.1)
|
15
|
+
rake (13.0.6)
|
16
|
+
regexp_parser (2.5.0)
|
17
|
+
rexml (3.2.5)
|
18
|
+
rspec (3.11.0)
|
19
|
+
rspec-core (~> 3.11.0)
|
20
|
+
rspec-expectations (~> 3.11.0)
|
21
|
+
rspec-mocks (~> 3.11.0)
|
22
|
+
rspec-core (3.11.0)
|
23
|
+
rspec-support (~> 3.11.0)
|
24
|
+
rspec-expectations (3.11.0)
|
25
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
26
|
+
rspec-support (~> 3.11.0)
|
27
|
+
rspec-mocks (3.11.1)
|
28
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
29
|
+
rspec-support (~> 3.11.0)
|
30
|
+
rspec-support (3.11.0)
|
31
|
+
rubocop (1.29.1)
|
32
|
+
parallel (~> 1.10)
|
33
|
+
parser (>= 3.1.0.0)
|
34
|
+
rainbow (>= 2.2.2, < 4.0)
|
35
|
+
regexp_parser (>= 1.8, < 3.0)
|
36
|
+
rexml (>= 3.2.5, < 4.0)
|
37
|
+
rubocop-ast (>= 1.17.0, < 2.0)
|
38
|
+
ruby-progressbar (~> 1.7)
|
39
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
40
|
+
rubocop-ast (1.18.0)
|
41
|
+
parser (>= 3.1.1.0)
|
42
|
+
rubocop-performance (1.13.3)
|
43
|
+
rubocop (>= 1.7.0, < 2.0)
|
44
|
+
rubocop-ast (>= 0.4.0)
|
45
|
+
ruby-progressbar (1.11.0)
|
46
|
+
standard (1.12.1)
|
47
|
+
rubocop (= 1.29.1)
|
48
|
+
rubocop-performance (= 1.13.3)
|
49
|
+
unicode-display_width (2.1.0)
|
50
|
+
|
51
|
+
PLATFORMS
|
52
|
+
ruby
|
53
|
+
x86_64-linux
|
54
|
+
|
55
|
+
DEPENDENCIES
|
56
|
+
rake (~> 13.0)
|
57
|
+
rspec (~> 3.0)
|
58
|
+
ruby-sh!
|
59
|
+
standard (~> 1.12)
|
60
|
+
|
61
|
+
BUNDLED WITH
|
62
|
+
2.3.8
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2022 John Doe
|
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
|
13
|
+
all 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
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,383 @@
|
|
1
|
+
# Rubsh
|
2
|
+
|
3
|
+
Rubsh (a.k.a. ruby-sh) - Inspired by [python-sh], allows you to call any program as if it were a function:
|
4
|
+
|
5
|
+
```ruby
|
6
|
+
require 'rubsh'
|
7
|
+
|
8
|
+
sh = Rubsh::Shell.new
|
9
|
+
print(sh.cmd('ifconfig').call_with('wlan0').stdout_data)
|
10
|
+
```
|
11
|
+
|
12
|
+
Output:
|
13
|
+
|
14
|
+
```text
|
15
|
+
wlan0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
|
16
|
+
inet 192.168.1.13 netmask 255.255.255.0 broadcast 192.168.1.255
|
17
|
+
inet6 fe80::7fab:2715:9e90:2061 prefixlen 64 scopeid 0x20<link>
|
18
|
+
inet6 240e:3b7:3278:9fa0:e48:24:7958:9128 prefixlen 64 scopeid 0x0<global>
|
19
|
+
ether 14:85:7f:08:5b:2e txqueuelen 1000 (Ethernet)
|
20
|
+
RX packets 6015867 bytes 7575465908 (7.0 GiB)
|
21
|
+
RX errors 0 dropped 0 overruns 0 frame 0
|
22
|
+
TX packets 2953567 bytes 391257693 (373.1 MiB)
|
23
|
+
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
|
24
|
+
```
|
25
|
+
|
26
|
+
Note that these aren't Ruby functions, these are running the binary commands on your system by dynamically resolving your $PATH, much like Bash does, and then wrapping the binary in a function. In this way, all the programs on your system are easily available to you from within Ruby.
|
27
|
+
|
28
|
+
When using this library you can:
|
29
|
+
|
30
|
+
* Call any program as if it were a function.
|
31
|
+
* Get an exception when exit code is not 0.
|
32
|
+
* Force terminate the process if it does not finish within the timeout.
|
33
|
+
* Always split the shell command into tokens, reduce command injection risk.
|
34
|
+
* etc.
|
35
|
+
|
36
|
+
|
37
|
+
## Table of Contents
|
38
|
+
|
39
|
+
* [Installation](#installation)
|
40
|
+
* [Usage](#usage)
|
41
|
+
* [Basic Syntax](#basic-syntax)
|
42
|
+
* [Passing Arguments](#passing-arguments)
|
43
|
+
* [Exit Codes](#exit-codes)
|
44
|
+
* [Redirection](#redirection)
|
45
|
+
* [Incremental Iteration](#incremental-iteration)
|
46
|
+
* [Background Processes](#background-processes)
|
47
|
+
* [Baking](#baking)
|
48
|
+
* [Subcommands](#subcommands)
|
49
|
+
* [Piping](#piping)
|
50
|
+
* [Reference](#reference)
|
51
|
+
* [Special Kwargs](#special-kwargs)
|
52
|
+
* [FAQ](#faq)
|
53
|
+
* [Why doesn’t `*` work as a command argument?](#why-doesnt--work-as-a-command-argument)
|
54
|
+
* [How do I execute a bash builtin?](#how-do-i-execute-a-bash-builtin)
|
55
|
+
* [How do I call a program that isn’t in $PATH?](#how-do-i-call-a-program-that-isnt-in-path)
|
56
|
+
* [How do I run a command and connect it to stdout and stdin?](#how-do-i-run-a-command-and-connect-it-to-stdout-and-stdin)
|
57
|
+
* [How do I order keyword arguments?](#how-do-i-order-keyword-arguments)
|
58
|
+
* [Development](#development)
|
59
|
+
* [Contributing](#contributing)
|
60
|
+
* [Acknowledgements](#acknowledgements)
|
61
|
+
* [License](#license)
|
62
|
+
* [Code Of Conduct](#code-of-conduct)
|
63
|
+
|
64
|
+
|
65
|
+
## Installation
|
66
|
+
|
67
|
+
Add this line to your application's Gemfile:
|
68
|
+
|
69
|
+
```ruby
|
70
|
+
gem 'ruby-sh', require: 'rubsh'
|
71
|
+
```
|
72
|
+
|
73
|
+
And then execute:
|
74
|
+
|
75
|
+
$ bundle install
|
76
|
+
|
77
|
+
Or install it yourself as:
|
78
|
+
|
79
|
+
$ gem install ruby-sh
|
80
|
+
|
81
|
+
|
82
|
+
## Usage
|
83
|
+
|
84
|
+
### Basic Syntax
|
85
|
+
|
86
|
+
```ruby
|
87
|
+
# Create a shell
|
88
|
+
sh = Rubsh::Shell.new
|
89
|
+
|
90
|
+
# Create a command, use `command`/`cmd`
|
91
|
+
cmd = sh.cmd("ls")
|
92
|
+
|
93
|
+
# Invoke a command, use `call`/`call_with`
|
94
|
+
result = cmd.call_with("-la")
|
95
|
+
|
96
|
+
# Print result
|
97
|
+
print result.stdout_data
|
98
|
+
```
|
99
|
+
|
100
|
+
### Passing Arguments
|
101
|
+
|
102
|
+
```ruby
|
103
|
+
sh.cmd("ls").call_with("-l", "/tmp", color: "always", human_readable: true)
|
104
|
+
# => ["/usr/bin/ls", "-l", "/tmp", "--color=always", "--human-readable"]
|
105
|
+
|
106
|
+
sh.cmd("curl").call_with("https://www.ruby-lang.org/", o: "page.html", silent: true)
|
107
|
+
# => ["/usr/bin/curl", "https://www.ruby-lang.org/", "-opage.html", "--silent"]
|
108
|
+
|
109
|
+
sh.cmd("git").call(:status, { v: true })
|
110
|
+
# => ["/usr/bin/git", "status", "-v"]
|
111
|
+
|
112
|
+
sh.cmd("git").call(:status, { v: true }, "--", ".")
|
113
|
+
# => ["/usr/bin/git", "status", "-v", "--", "."]
|
114
|
+
|
115
|
+
sh.cmd("git").call(:status, { v: proc{ true }, short: true }, "--", ".")
|
116
|
+
# => ["/usr/bin/git", "status", "-v", "--short", "--", "."]
|
117
|
+
|
118
|
+
sh.cmd("git").call(:status, { v: true }, v: false)
|
119
|
+
# => ["/usr/bin/git", "status"]
|
120
|
+
```
|
121
|
+
|
122
|
+
### Exit Codes
|
123
|
+
|
124
|
+
```ruby
|
125
|
+
# Successful
|
126
|
+
r = sh.cmd("ls").call_with("/")
|
127
|
+
r.exit_code # => 0
|
128
|
+
|
129
|
+
# a `CommandReturnFailureError` raised when run failure
|
130
|
+
begin
|
131
|
+
sh.cmd("ls").call_with("/some/non-existant/folder")
|
132
|
+
rescue Rubsh::Exceptions::CommandReturnFailureError => e
|
133
|
+
e.exit_code # => 2
|
134
|
+
end
|
135
|
+
|
136
|
+
# Treats as success use `:_ok_code`
|
137
|
+
r = sh.cmd("ls").call_with("/some/non-existant/folder", _ok_code: [0, 1, 2])
|
138
|
+
r = sh.cmd("ls").call_with("/some/non-existant/folder", _ok_code: 0..2)
|
139
|
+
r.exit_code # => 2
|
140
|
+
```
|
141
|
+
|
142
|
+
### Redirection
|
143
|
+
|
144
|
+
```ruby
|
145
|
+
# Filename
|
146
|
+
sh.cmd("ls").call_with(_out: "/tmp/dir_content")
|
147
|
+
sh.cmd("ls").call_with(_out: ["/tmp/dir_content", "w"])
|
148
|
+
sh.cmd("ls").call_with(_out: ["/tmp/dir_content", "w", 0600])
|
149
|
+
sh.cmd("ls").call_with(_out: ["/tmp/dir_content", File::WRONLY|File::EXCL|File::CREAT, 0600])
|
150
|
+
|
151
|
+
# File object
|
152
|
+
File.open("/tmp/dir_content", "w") { |f| sh.cmd("ls").call_with(_out: f) }
|
153
|
+
|
154
|
+
# `stdout_data` & `stderr_data`
|
155
|
+
r = sh.cmd("sh").call_with("-c", "echo out; echo err >&2")
|
156
|
+
r.stdout_data # => "out\n"
|
157
|
+
r.stderr_data # => "err\n"
|
158
|
+
|
159
|
+
# Redirects stderr and stderr to the same place use `_err_to_out`
|
160
|
+
r = sh.cmd("sh").call_with("-c", "echo out; echo err >&2", _err_to_out: true)
|
161
|
+
r.stdout_data # => "out\nerr\n"
|
162
|
+
r.stderr_data # => nil
|
163
|
+
|
164
|
+
# Read input from data
|
165
|
+
sh.cmd("cat").call_with(_in_data: "hello").stdout_data # => "hello"
|
166
|
+
|
167
|
+
# Read input from file
|
168
|
+
sh.cmd("cat").call_with(_in: "/some/existant/file")
|
169
|
+
```
|
170
|
+
|
171
|
+
### Incremental Iteration
|
172
|
+
|
173
|
+
```ruby
|
174
|
+
# By default, output is line-buffered, so the body of the loop will only run
|
175
|
+
# when your process produces a newline. You can change this by changing the
|
176
|
+
# buffer size of the command’s output with `_out_bufsize`/`_err_bufsize`.
|
177
|
+
tail = sh.cmd("tail")
|
178
|
+
tail.call_with("-f", "/var/log/some_log_file.log", _capture: ->(stdout, _stderr) {
|
179
|
+
print stdout
|
180
|
+
})
|
181
|
+
```
|
182
|
+
|
183
|
+
### Background Processes
|
184
|
+
|
185
|
+
```ruby
|
186
|
+
# Blocks
|
187
|
+
sh.cmd("sleep").call_with(3)
|
188
|
+
p "...3 seconds later"
|
189
|
+
|
190
|
+
# Doesn't block
|
191
|
+
r = sh.cmd("sleep").call_with(3, _bg: true)
|
192
|
+
p "prints immediately!"
|
193
|
+
r.wait()
|
194
|
+
p "...and 3 seconds later"
|
195
|
+
|
196
|
+
# Timeout
|
197
|
+
r = sh.cmd("sleep").call_with(30, _bg: true)
|
198
|
+
p "prints immediately!"
|
199
|
+
r.wait(timeout: 3)
|
200
|
+
p "...and 3 seconds later"
|
201
|
+
```
|
202
|
+
|
203
|
+
### Baking
|
204
|
+
|
205
|
+
```ruby
|
206
|
+
ll = sh.cmd("ls").bake("-l")
|
207
|
+
ll.call_with("/tmp") # => ["/usr/bin/ls", "-l", "/tmp"]
|
208
|
+
|
209
|
+
# Equivalent
|
210
|
+
sh.cmd("ls").call_with("-l", "/tmp")
|
211
|
+
|
212
|
+
# Calling whoami on a server. this is a lot to type out, especially if you wanted
|
213
|
+
# to call many commands (not just whoami) back to back on the same server resolves
|
214
|
+
# to "/usr/bin/ssh myserver.com -p 1393 whoami"
|
215
|
+
sh.cmd('ssh').call_with("myserver.com", "-p 1393", "whoami")
|
216
|
+
|
217
|
+
# Wouldn't it be nice to bake the common parameters into the ssh command?
|
218
|
+
myserver = sh.cmd('ssh').bake("myserver.com", p: 1393)
|
219
|
+
myserver.call_with('whoami')
|
220
|
+
myserver.call_with('pwd')
|
221
|
+
```
|
222
|
+
|
223
|
+
### Subcommands
|
224
|
+
|
225
|
+
```ruby
|
226
|
+
# Use `bake`
|
227
|
+
gst = sh.cmd("git").bake("status")
|
228
|
+
|
229
|
+
gst.call_with() # => ["/usr/bin/git", "status"]
|
230
|
+
gst.call_with("-s") # => ["/usr/bin/git", "status", "-s"]
|
231
|
+
```
|
232
|
+
|
233
|
+
### Piping
|
234
|
+
|
235
|
+
```ruby
|
236
|
+
# Run a series of commands connected by `_pipeline`
|
237
|
+
r = sh.pipeline(_in_data: "hello world") do |pipeline|
|
238
|
+
sh.cmd("cat").call_with(_pipeline: pipeline)
|
239
|
+
sh.cmd("wc").call_with("-c", _pipeline: pipeline)
|
240
|
+
end
|
241
|
+
r.stdout_data # => "11\n"
|
242
|
+
```
|
243
|
+
|
244
|
+
|
245
|
+
## Reference
|
246
|
+
|
247
|
+
### Special Kwargs
|
248
|
+
|
249
|
+
* `_in_data`:
|
250
|
+
* use: Specifies an argument for the process to use as its standard input data.
|
251
|
+
* default value: `nil`
|
252
|
+
* `_in`:
|
253
|
+
* use: Specifies an argument for the process to use as its standard input.
|
254
|
+
* default value: `nil`
|
255
|
+
* `_out`:
|
256
|
+
* use: Where to redirect STDOUT to.
|
257
|
+
* default value: `nil`
|
258
|
+
* `_err`:
|
259
|
+
* use: Where to redirect STDERR to.
|
260
|
+
* default value: `nil`
|
261
|
+
* `_err_to_out`:
|
262
|
+
* use: If true, duplicate the file descriptor bound to the process’s STDOUT also to STDERR.
|
263
|
+
* default value: `false`
|
264
|
+
* `_capture`:
|
265
|
+
* use: Iterates over STDOUT/STDERR.
|
266
|
+
* default value: `nil`
|
267
|
+
* `_bg`:
|
268
|
+
* use: Runs a command in the background. The command will return immediately, and you will have to run RunningCommand#wait on it to ensure it terminates.
|
269
|
+
* default value: `false`
|
270
|
+
* `_timeout`:
|
271
|
+
* use: How much time, in seconds, we should give the process to complete. If the process does not finish within the timeout, it will be terminated.
|
272
|
+
* default value: `nil`
|
273
|
+
* `_env`:
|
274
|
+
* use: A dictionary defining the only environment variables that will be made accessible to the process. If not specified, the calling process’s environment variables are used.
|
275
|
+
* default value: `nil`
|
276
|
+
* `_cwd`:
|
277
|
+
* use: Current working directory of the process.
|
278
|
+
* default value: `nil`
|
279
|
+
* `_ok_code`:
|
280
|
+
* use: Some misbehaved programs use exit codes other than 0 to indicate success. Set to treats as success.
|
281
|
+
* default value: `[0]`
|
282
|
+
* `_no_out`:
|
283
|
+
* use: Disables STDOUT being internally stored. This is useful for commands that produce huge amounts of output that you don’t need, that would otherwise be hogging memory if stored internally by Rubsh.
|
284
|
+
* default value: `false`
|
285
|
+
* `_no_err`:
|
286
|
+
* use: Disables STDERR being internally stored. This is useful for commands that produce huge amounts of output that you don’t need, that would otherwise be hogging memory if stored internally by Rubsh.
|
287
|
+
* default value: `false`
|
288
|
+
* `_out_bufsize`:
|
289
|
+
* use: The STDOUT buffer size. nil for unbuffered, 0 for line buffered, anything else for a buffer of that amount.
|
290
|
+
* default value: `0`
|
291
|
+
* `_err_bufsize`:
|
292
|
+
* use: The STDERR buffer size. nil for unbuffered, 0 for line buffered, anything else for a buffer of that amount.
|
293
|
+
* default value: `0`
|
294
|
+
* `_long_sep`:
|
295
|
+
* use: This is the character(s) that separate a program’s long argument’s key from the value.
|
296
|
+
* default value: `"="`
|
297
|
+
* `_long_prefix`:
|
298
|
+
* use: This is the character(s) that prefix a long argument for the program being run. Some programs use single dashes, for example, and do not understand double dashes.
|
299
|
+
* default value: `"--"`
|
300
|
+
* `_pipeline`:
|
301
|
+
* use: Specifies the :pipeline.
|
302
|
+
* default value: `nil`
|
303
|
+
|
304
|
+
|
305
|
+
## FAQ
|
306
|
+
|
307
|
+
### Why doesn’t `*` work as a command argument?
|
308
|
+
|
309
|
+
Glob expansion is a feature of a shell, like Bash, and is performed by the shell before passing the results to the program to be exec'd. Because Rubsh is not a shell, but rather tool to execute programs directly, we do not handle glob expansion like a shell would.
|
310
|
+
|
311
|
+
### How do I execute a bash builtin?
|
312
|
+
|
313
|
+
```ruby
|
314
|
+
sh = Rubsh::Shell.new
|
315
|
+
rawsh = sh.cmd('bash').bake('-c')
|
316
|
+
print(rawsh.call_with('echo Hello').stdout_data) # => "Hello\n"
|
317
|
+
```
|
318
|
+
|
319
|
+
### How do I call a program that isn’t in $PATH?
|
320
|
+
|
321
|
+
Use absolute binpath
|
322
|
+
|
323
|
+
```ruby
|
324
|
+
sh = Rubsh::Shell.new
|
325
|
+
sh.cmd('/path/to/command').call()
|
326
|
+
```
|
327
|
+
|
328
|
+
OR Use `Rubsh::Shell::Env#path`
|
329
|
+
|
330
|
+
```ruby
|
331
|
+
sh = Rubsh::Shell.new
|
332
|
+
sh.env.path << "/dir/to/command/"
|
333
|
+
sh.cmd('command').call()
|
334
|
+
```
|
335
|
+
|
336
|
+
### How do I run a command and connect it to stdout and stdin?
|
337
|
+
|
338
|
+
Use `_capture` special kwargs.
|
339
|
+
|
340
|
+
### How do I order keyword arguments?
|
341
|
+
|
342
|
+
Typically this question gets asked when a user is trying to execute something like the following commandline:
|
343
|
+
|
344
|
+
```sh
|
345
|
+
my-command --arg1=val1 arg2 --arg3=val3
|
346
|
+
```
|
347
|
+
|
348
|
+
Use:
|
349
|
+
|
350
|
+
```ruby
|
351
|
+
sh = Rubsh::Shell.new
|
352
|
+
sh.cmd('my-command').call_with({ arg1: "val1" }, "args2", { arg3: "val3" })
|
353
|
+
```
|
354
|
+
|
355
|
+
|
356
|
+
## Development
|
357
|
+
|
358
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
359
|
+
|
360
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
361
|
+
|
362
|
+
|
363
|
+
## Contributing
|
364
|
+
|
365
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/souk4711/rubsh. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/souk4711/rubsh/blob/main/CODE_OF_CONDUCT.md).
|
366
|
+
|
367
|
+
|
368
|
+
## Acknowledgements
|
369
|
+
|
370
|
+
* Special thanks to [python-sh].
|
371
|
+
|
372
|
+
|
373
|
+
## License
|
374
|
+
|
375
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
376
|
+
|
377
|
+
|
378
|
+
## Code of Conduct
|
379
|
+
|
380
|
+
Everyone interacting in the Rubsh project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/souk4711/rubsh/blob/main/CODE_OF_CONDUCT.md).
|
381
|
+
|
382
|
+
|
383
|
+
[python-sh]:https://github.com/amoffat/sh
|
data/Rakefile
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
module Rubsh
|
2
|
+
class Argument
|
3
|
+
# @overload initialize(name)
|
4
|
+
# @param name [String, Symbol, #to_s]
|
5
|
+
#
|
6
|
+
# @overload initialize(name, value)
|
7
|
+
# @param name [String, Symbol, #to_s]
|
8
|
+
# @param value [nil, Boolean, Numeric, String, Symbol, Proc]
|
9
|
+
def initialize(*args)
|
10
|
+
@name, @value = args[0].to_s, args[1]
|
11
|
+
@is_positional = args.length < 2
|
12
|
+
end
|
13
|
+
|
14
|
+
# @return [void]
|
15
|
+
def value=(value)
|
16
|
+
raise ::ArgumentError, "cannot assign a new value for positional argument" if @is_positional
|
17
|
+
@value = value
|
18
|
+
end
|
19
|
+
|
20
|
+
# @return [nil, String, Array<String>]
|
21
|
+
def compile(long_sep: "=", long_prefix: "--")
|
22
|
+
return compile_positional_argument(@name) if @is_positional
|
23
|
+
compile_option_argument(@name, @value, long_sep: long_sep, long_prefix: long_prefix)
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def compile_positional_argument(name)
|
29
|
+
name
|
30
|
+
end
|
31
|
+
|
32
|
+
def compile_option_argument(name, value, long_sep:, long_prefix:)
|
33
|
+
value = value.call if value.respond_to?(:call)
|
34
|
+
return if value.nil?
|
35
|
+
return if value.is_a?(::FalseClass)
|
36
|
+
|
37
|
+
if name.length == 1 # short option
|
38
|
+
return format("-%s", name) if value.is_a?(::TrueClass)
|
39
|
+
format("-%s%s", name, value)
|
40
|
+
else # long option
|
41
|
+
return format("%s%s", long_prefix, name) if value.is_a?(::TrueClass)
|
42
|
+
return [format("%s%s", long_prefix, name), value.to_s] if long_sep.nil?
|
43
|
+
format("%s%s%s%s", long_prefix, name, long_sep, value)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|