backticks 0.3.1 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rspec +2 -0
- data/.travis.yml +5 -2
- data/Gemfile +4 -0
- data/README.md +4 -2
- data/backticks.gemspec +2 -0
- data/lib/backticks.rb +20 -13
- data/lib/backticks/cli.rb +9 -9
- data/lib/backticks/command.rb +2 -0
- data/lib/backticks/runner.rb +28 -8
- data/lib/backticks/version.rb +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 156e6dd6cd59ca1201a48b9679e1f6151caf6b43
|
4
|
+
data.tar.gz: 225c905201c500af8cde4a7561490befaf797caa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b7230c03f69e71a6aeadb1cd0f3d43f0a12ad166231d0aecaea8c9c41bf8373e3cb5e3fbdfaf8ddb08be35c4e0632404edafb4b9de36e55213e6166f19134a1d
|
7
|
+
data.tar.gz: bb4bfa7199f80179fba4147379667b9e4e80ad2b8d3208e4d072953aaffbc2d42e4c1c8f52ce46059bec9c0b978600c3661b0650c23f2b7330da1fac7d9ddd8f
|
data/.rspec
CHANGED
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -3,6 +3,8 @@
|
|
3
3
|
Backticks is a powerful, intuitive OOP wrapper for invoking command-line processes and
|
4
4
|
interacting with them.
|
5
5
|
|
6
|
+
 [](https://coveralls.io/github/xeger/backticks?branch=master)
|
7
|
+
|
6
8
|
"Powerful" comes from features that make Backticks especially well suited for time-sensitive
|
7
9
|
or record/playback applications:
|
8
10
|
- Uses [pseudoterminals](https://en.wikipedia.org/wiki/Pseudoterminal) for realtime stdout/stdin
|
@@ -59,7 +61,7 @@ puts output
|
|
59
61
|
|
60
62
|
# The hard way. Allows customized behavior; returns a Command object that
|
61
63
|
# allows you to interact with the running command.
|
62
|
-
command = Backticks::Runner.new(interactive:true).
|
64
|
+
command = Backticks::Runner.new(interactive:true).run('ls', R:true, '*.rb')
|
63
65
|
command.join
|
64
66
|
puts "Exit status: #{command.status.to_i}. Output:"
|
65
67
|
puts command.captured_output
|
@@ -93,7 +95,7 @@ accordingly:
|
|
93
95
|
```ruby
|
94
96
|
require 'io/console'
|
95
97
|
# In IRB, call raw! on same line as command; IRB prompt uses raw I/O
|
96
|
-
STDOUT.raw! ; Backticks::Runner.new(interactive:true).
|
98
|
+
STDOUT.raw! ; Backticks::Runner.new(interactive:true).run('vi').join
|
97
99
|
```
|
98
100
|
|
99
101
|
### Literally Overriding Ruby's Backticks
|
data/backticks.gemspec
CHANGED
@@ -19,6 +19,8 @@ Gem::Specification.new do |spec|
|
|
19
19
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
20
|
spec.require_paths = ['lib']
|
21
21
|
|
22
|
+
spec.required_ruby_version = Gem::Requirement.new("~> 2.0")
|
23
|
+
|
22
24
|
spec.add_development_dependency 'bundler', '~> 1.10'
|
23
25
|
spec.add_development_dependency 'rake', '~> 10.0'
|
24
26
|
spec.add_development_dependency 'rspec'
|
data/lib/backticks.rb
CHANGED
@@ -5,31 +5,38 @@ require_relative 'backticks/runner'
|
|
5
5
|
require_relative 'backticks/ext'
|
6
6
|
|
7
7
|
module Backticks
|
8
|
-
# Run a command; return a Command object that
|
9
|
-
# the running process.
|
8
|
+
# Run a command with default invocation options; return a Command object that
|
9
|
+
# can be used to interact with the running process.
|
10
10
|
#
|
11
|
-
# @param [
|
11
|
+
# @param [Array] sugar list of command words and options
|
12
12
|
# @return [Backticks::Command] a running command
|
13
|
-
|
14
|
-
|
13
|
+
# @see Backticks::Runner#run for a better description of sugar
|
14
|
+
# @see Backticks::Runner for more control over process invocation
|
15
|
+
def self.new(*sugar)
|
16
|
+
Backticks::Runner.new.run(*sugar)
|
15
17
|
end
|
16
18
|
|
17
|
-
# Run a command;
|
19
|
+
# Run a command with default invocation options; wait for to exit, then return
|
20
|
+
# its output. Populate $? with the command's status before returning.
|
18
21
|
#
|
19
|
-
# @param [
|
22
|
+
# @param [Array] sugar list of command words and options
|
20
23
|
# @return [String] the command's output
|
21
|
-
|
22
|
-
|
24
|
+
# @see Backticks::Runner#run for a better description of sugar
|
25
|
+
# @see Backticks::Runner for more control over process invocation
|
26
|
+
def self.run(*sugar)
|
27
|
+
command = self.new(*sugar)
|
23
28
|
command.join
|
24
29
|
command.captured_output
|
25
30
|
end
|
26
31
|
|
27
|
-
# Run a command; return
|
32
|
+
# Run a command; return whether it succeeded or failed.
|
28
33
|
#
|
29
|
-
# @param [
|
34
|
+
# @param [Array] sugar list of command words and options
|
30
35
|
# @return [Boolean] true if the command succeeded; false otherwise
|
31
|
-
|
32
|
-
|
36
|
+
# @see Backticks::Runner#run for a better description of sugar
|
37
|
+
# @see Backticks::Runner for more control over process invocation
|
38
|
+
def self.system(*sugar)
|
39
|
+
command = self.new(*sugar)
|
33
40
|
command.join
|
34
41
|
$?.success?
|
35
42
|
end
|
data/lib/backticks/cli.rb
CHANGED
@@ -10,14 +10,14 @@ module Backticks
|
|
10
10
|
# golang flags, and most Java utilities. It's a great choice of default
|
11
11
|
# CLI.
|
12
12
|
module Getopt
|
13
|
-
# Translate a series
|
14
|
-
#
|
13
|
+
# Translate a series Ruby positional and keyword arguments into command-
|
14
|
+
# parameters consisting of words and options.
|
15
15
|
#
|
16
16
|
# Each positional argument can be a Hash, an Array, or another object.
|
17
17
|
# They are handled as follows:
|
18
18
|
# - Hash is translated to a sequence of options; see #options
|
19
19
|
# - Array is appended to the command line as a sequence of words
|
20
|
-
# - other objects are turned into a
|
20
|
+
# - other objects are turned into a string with #to_s and appended to the command line as a single word
|
21
21
|
#
|
22
22
|
# @return [Array] list of String words and options
|
23
23
|
#
|
@@ -26,10 +26,10 @@ module Backticks
|
|
26
26
|
#
|
27
27
|
# @example install your favorite gem
|
28
28
|
# parameters('gem', 'install', no_document:true, 'backticks')
|
29
|
-
def self.parameters(*
|
29
|
+
def self.parameters(*sugar)
|
30
30
|
argv = []
|
31
31
|
|
32
|
-
|
32
|
+
sugar.each do |item|
|
33
33
|
case item
|
34
34
|
when Array
|
35
35
|
# list of words to append to argv
|
@@ -46,7 +46,7 @@ module Backticks
|
|
46
46
|
argv
|
47
47
|
end
|
48
48
|
|
49
|
-
# Translate Ruby
|
49
|
+
# Translate Ruby keyword arguments into command-line parameters using a
|
50
50
|
# notation that is compatible with traditional Unix getopt. Command lines
|
51
51
|
# generated by this method are also mostly compatible with the following:
|
52
52
|
# - GNU getopt
|
@@ -70,12 +70,12 @@ module Backticks
|
|
70
70
|
# Unix-like parameters.
|
71
71
|
#
|
72
72
|
# @return [Array] list of String command-line options
|
73
|
-
def self.options(
|
73
|
+
def self.options(kwargs={})
|
74
74
|
flags = []
|
75
75
|
|
76
76
|
# Transform opts into golang flags-style command line parameters;
|
77
77
|
# append them to the command.
|
78
|
-
|
78
|
+
kwargs.each do |kw, arg|
|
79
79
|
if kw.length == 1
|
80
80
|
if arg == true
|
81
81
|
# true: boolean flag
|
@@ -104,4 +104,4 @@ module Backticks
|
|
104
104
|
end
|
105
105
|
end
|
106
106
|
end
|
107
|
-
end
|
107
|
+
end
|
data/lib/backticks/command.rb
CHANGED
data/lib/backticks/runner.rb
CHANGED
@@ -33,10 +33,22 @@ module Backticks
|
|
33
33
|
|
34
34
|
# Create an instance of Runner.
|
35
35
|
# @param [#parameters] cli object used to convert Ruby method parameters into command-line parameters
|
36
|
-
def initialize(
|
37
|
-
|
38
|
-
|
39
|
-
|
36
|
+
def initialize(options={})
|
37
|
+
options = {
|
38
|
+
:buffered => false,
|
39
|
+
:cli => Backticks::CLI::Getopt,
|
40
|
+
:interactive => false,
|
41
|
+
}.merge(options)
|
42
|
+
|
43
|
+
@buffered = options[:buffered]
|
44
|
+
@cli = options[:cli]
|
45
|
+
@interactive = options[:interactive]
|
46
|
+
end
|
47
|
+
|
48
|
+
# @deprecated
|
49
|
+
def command(*sugar)
|
50
|
+
warn 'Backticks::Runner#command is deprecated; please call #run instead'
|
51
|
+
run(*sugar)
|
40
52
|
end
|
41
53
|
|
42
54
|
# Run a command whose parameters are expressed using some Rubyish sugar.
|
@@ -50,15 +62,23 @@ module Backticks
|
|
50
62
|
#
|
51
63
|
# @see Backticks::CLI::Getopt for option-Hash format information
|
52
64
|
#
|
53
|
-
# @param [Array]
|
65
|
+
# @param [Array] sugar list of command words and options
|
54
66
|
#
|
55
67
|
# @return [Command] the running command
|
56
68
|
#
|
57
69
|
# @example Run docker-compose with complex parameters
|
58
|
-
#
|
59
|
-
def
|
60
|
-
|
70
|
+
# run('docker-compose', {file: 'joe.yml'}, 'up', {d:true}, 'mysvc')
|
71
|
+
def run(*sugar)
|
72
|
+
run_without_sugar(@cli.parameters(*sugar))
|
73
|
+
end
|
61
74
|
|
75
|
+
# Run a command whose argv is specified in the same manner as Kernel#exec,
|
76
|
+
# with no Rubyish sugar.
|
77
|
+
#
|
78
|
+
# @param [Array] argv command to run; argv[0] is program name and the
|
79
|
+
# remaining elements are parameters and flags
|
80
|
+
# @return [Command] the running command
|
81
|
+
def run_without_sugar(argv)
|
62
82
|
if self.buffered
|
63
83
|
run_buffered(argv)
|
64
84
|
else
|
data/lib/backticks/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: backticks
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tony Spataro
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-02-
|
11
|
+
date: 2016-02-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -85,9 +85,9 @@ require_paths:
|
|
85
85
|
- lib
|
86
86
|
required_ruby_version: !ruby/object:Gem::Requirement
|
87
87
|
requirements:
|
88
|
-
- - "
|
88
|
+
- - "~>"
|
89
89
|
- !ruby/object:Gem::Version
|
90
|
-
version: '0'
|
90
|
+
version: '2.0'
|
91
91
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
92
92
|
requirements:
|
93
93
|
- - ">="
|