subshell_command 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6b9e241f2b624b8c996b0244c10fca264bbdc8b3
4
+ data.tar.gz: be8cfb27054a7768479f25be8262847ad0e75eb0
5
+ SHA512:
6
+ metadata.gz: db2b64961d7ce148c2fadec3692415cf4205e7ac1c0c0b5f054571031a11d51baf75b4474be65c07cf1156d02a1a639c39d32b226b583881fc5fcc2ccef2416d
7
+ data.tar.gz: 9ec394754a459c78b5bc07d080c85a6dbbf8c459f8b997cde254952da4291fbfc7960f6bfde3636b5c62b2dd207cb000db569da970ff9546a843daf86270a6cf
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.0
5
+ before_install: gem install bundler -v 1.14.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in subshell_command.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Alan Skorkin
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.
@@ -0,0 +1,80 @@
1
+ # SubshellCommand
2
+
3
+ If, like me, you always forget when and how (and why) to use backticks or popen or whatever when trying to execute a shell command from within a ruby process, this should hopefully make life a little bit easier.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'subshell_command'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install subshell_command
20
+
21
+ ## Usage
22
+
23
+ Simplest form:
24
+
25
+ ```ruby
26
+ result = SubshellCommand.execute("ls -al")
27
+ if result.success?
28
+ puts result.stdout_value
29
+ else
30
+ puts result.stderr_value
31
+ end
32
+ ```
33
+
34
+ But we can do a little better with a block:
35
+
36
+ ```ruby
37
+ SubshellCommand.execute("ls -al") do |o|
38
+ o.cmd = "pwd" # we can override the command
39
+ o.redirect_stderr_to_stdout = true # we can pipe stderr to stdout
40
+ o.current_directory = "/" # we can override the working directory
41
+ o.env = { # we can provide some extra env vars
42
+ FOO: "bar"
43
+ }
44
+ o.on_success = ->(r) do # we can provide a success callback
45
+ puts r.stdout_value
46
+ end
47
+ o.on_failure = ->(r) do # we can provide a failure callback
48
+ puts r.stderr_value
49
+ end
50
+ end
51
+ ```
52
+
53
+ All of the above options have sensible defaults, so you can get away with:
54
+
55
+ ```ruby
56
+ SubshellCommand.execute("ls -al") do |o|
57
+ o.on_success = ->(result) do
58
+ puts result.stdout_value
59
+ end
60
+ end
61
+ ```
62
+
63
+ If all you care about is doing something when the command succeeds.
64
+
65
+ Also all the code for this is in one file, which is on purpose, so if you'd rather not add an extra gem, just grab the file, dump it into your project and off you go.
66
+
67
+ ## Development
68
+
69
+ 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.
70
+
71
+ 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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
72
+
73
+ ## Contributing
74
+
75
+ Bug reports and pull requests are welcome on GitHub at https://github.com/skorks/subshell_command.
76
+
77
+
78
+ ## License
79
+
80
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "subshell_command"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,141 @@
1
+ require "subshell_command/version"
2
+ require "open3"
3
+
4
+ module SubshellCommand
5
+ class Command
6
+ attr_reader :command_string
7
+ attr_reader :env_hash, :current_directory
8
+ attr_reader :success_callback, :failure_callback
9
+ attr_reader :redirect_stderr_to_stdout
10
+
11
+ def initialize(command_string)
12
+ @command_string = command_string
13
+ @env_hash = {}
14
+ @current_directory = Dir.pwd
15
+ @success_callback = ->{}
16
+ @failure_callback = ->{}
17
+ @redirect_stderr_to_stdout = false
18
+ end
19
+
20
+ def cmd=(command_string)
21
+ @command_string = command_string
22
+ end
23
+
24
+ def env=(env_hash)
25
+ @env_hash = env_hash
26
+ end
27
+
28
+ def current_directory=(directory_string)
29
+ @current_directory = directory_string
30
+ end
31
+
32
+ def on_success=(callback)
33
+ @success_callback = callback
34
+ end
35
+
36
+ def on_failure=(callback)
37
+ @failure_callback = callback
38
+ end
39
+
40
+ def redirect_stderr_to_stdout=(value)
41
+ @redirect_stderr_to_stdout = value
42
+ end
43
+ end
44
+
45
+ class Result
46
+ attr_accessor :success, :stdout_value, :stderr_value
47
+
48
+ def initialize
49
+ @success = nil
50
+ @stdout_value = nil
51
+ @stderr_value = nil
52
+ end
53
+
54
+ def success?
55
+ @success
56
+ end
57
+ end
58
+
59
+ class StandardOutputStreamsExecutor
60
+ attr_reader :command_object, :result
61
+
62
+ def initialize(command_object, result)
63
+ @command_object = command_object
64
+ @result = result
65
+ end
66
+
67
+ def execute
68
+ Open3.popen3(
69
+ command_object.env_hash,
70
+ command_object.command_string,
71
+ chdir: command_object.current_directory,
72
+ ) do |stdin, stdout, stderr, wait_thr|
73
+ exit_status = wait_thr.value
74
+ result.stdout_value = stdout.read
75
+ result.stderr_value = stderr.read
76
+ result.success = exit_status.success?
77
+ end
78
+ end
79
+ end
80
+
81
+ class CombinedOutputStreamsExecutor
82
+ attr_reader :command_object, :result
83
+
84
+ def initialize(command_object, result)
85
+ @command_object = command_object
86
+ @result = result
87
+ end
88
+
89
+ def execute
90
+ Open3.popen2e(
91
+ command_object.env_hash,
92
+ command_object.command_string,
93
+ chdir: command_object.current_directory,
94
+ ) do |stdin, output_streams, wait_thr|
95
+ exit_status = wait_thr.value
96
+ output_value = output_streams.read
97
+ result.stdout_value = output_value
98
+ result.stderr_value = output_value
99
+ result.success = exit_status.success?
100
+ end
101
+ end
102
+ end
103
+
104
+ class CommandExecutor
105
+ attr_reader :command_object
106
+
107
+ EXECUTORS = {
108
+ true => CombinedOutputStreamsExecutor,
109
+ false => StandardOutputStreamsExecutor,
110
+ }
111
+
112
+ def initialize(command_object)
113
+ @command_object = command_object
114
+ end
115
+
116
+ def execute
117
+ result = Result.new
118
+ EXECUTORS[command_object.redirect_stderr_to_stdout].new(command_object, result).execute
119
+ execute_callbacks(result)
120
+ result
121
+ end
122
+
123
+ private
124
+
125
+ def execute_callbacks(result)
126
+ if result.success?
127
+ command_object.success_callback.call(result)
128
+ else
129
+ command_object.failure_callback.call(result)
130
+ end
131
+ end
132
+ end
133
+
134
+ class << self
135
+ def execute(command_string = nil, &block)
136
+ command = Command.new(command_string)
137
+ block.call(command) if block_given?
138
+ CommandExecutor.new(command).execute
139
+ end
140
+ end
141
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "subshell_command"
7
+ spec.version = "0.1.0"
8
+ spec.authors = ["Alan Skorkin"]
9
+ spec.email = ["alan@skorks.com"]
10
+
11
+ spec.summary = %q{Simpler way to execute shell commands from within ruby processes}
12
+ spec.description = %q{If, like me, you always forget when and how (and why) to
13
+ use backticks or popen or whatever when trying to execute a shell command from
14
+ within a ruby process, this should hopefully make life a little bit easier.}
15
+ spec.homepage = "https://github.com/skorks/subshell_command"
16
+ spec.license = "MIT"
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
19
+ f.match(%r{^(test|spec|features)/})
20
+ end
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.14"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec", "~> 3.0"
28
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: subshell_command
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Alan Skorkin
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-04-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: |-
56
+ If, like me, you always forget when and how (and why) to
57
+ use backticks or popen or whatever when trying to execute a shell command from
58
+ within a ruby process, this should hopefully make life a little bit easier.
59
+ email:
60
+ - alan@skorks.com
61
+ executables: []
62
+ extensions: []
63
+ extra_rdoc_files: []
64
+ files:
65
+ - ".gitignore"
66
+ - ".rspec"
67
+ - ".travis.yml"
68
+ - Gemfile
69
+ - LICENSE.txt
70
+ - README.md
71
+ - Rakefile
72
+ - bin/console
73
+ - bin/setup
74
+ - lib/subshell_command.rb
75
+ - subshell_command.gemspec
76
+ homepage: https://github.com/skorks/subshell_command
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.6.8
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Simpler way to execute shell commands from within ruby processes
100
+ test_files: []